how to find the attributes of an object in python code example
Example 1: python get attributes of object
getattr(object, 'attribute_name')
Example 2: see attributes of object python
>>> class new_class():
... def __init__(self, number):
... self.multi = int(number) * 2
... self.str = str(number)
...
>>> a = new_class(2)
>>> a.__dict__
{'multi': 4, 'str': '2'}
>>> a.__dict__.keys()
dict_keys(['multi', 'str'])
Example 3: how to view all attributes and methods of an object python
class Person:
def __init__(self,name,age):
self.name = name
self.age = age
def printInfo(self):
print("name: {}, age: {}".format(self.name,self.age))
p1 = Person('Tom',24)
print(p1.__dict__)