9

I would like to display the attributes of a give object and was wondering if there was a python function for it. For example if I had an object from the following class:

class Antibody():

    def __init__(self,toSend):

        self.raw = toSend
        self.pdbcode = ''
        self.year = ''

Could I get an output that looks something like this or something similar:

['self.raw','self.pdbcode','self.year']

thanks

4 Answers 4

15

Try dir(self). It will include all attributes, not only "data".

Sign up to request clarification or add additional context in comments.

Comments

8

The following method prints ['self.pdbcode', 'self.raw', 'self.year'] for an instance of your class:

class Antibody():
    ...
    def get_fields(self):
        ret = []
        for nm in dir(self):
           if not nm.startswith('__') and not callable(getattr(self, nm)):
              ret.append('self.' + nm)
        return ret

a = Antibody(0)
print a.get_fields()

1 Comment

thanks a lot this is just what I need. Thanks for the full explanation
1

Like this

class Antibody:
    def __init__(self,toSend):
        self.raw = toSend
        self.pdbcode = ''
        self.year = ''
    def attributes( self ):
        return [ 'self.'+name for name in self.__dict__ ]

1 Comment

Thanks I'm voting this up as well for brevity!
0

a = Antibody(0)

map(lambda attr: 'self.%s'%(attr), filter(lambda attr: not callable(getattr(a, attr)), a.__dict__))

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.