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
Use Python's vars() to Print an Object's Attributes The dir() function, as shown above, prints all of the attributes of a Python object.
An instance/object attribute is a variable that belongs to one (and only one) object. Every instance of a class points to its own attributes variables. These attributes are defined within the __init__ constructor.
Try dir(self)
. It will include all attributes, not only "data".
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()
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__ ]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With