List attributes of an object [duplicate] List attributes of an object [duplicate] python python

List attributes of an object [duplicate]

>>> 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'])

You may also find pprint helpful.

dir(instance)# or (same value)instance.__dir__()# orinstance.__dict__

Then you can test what type is with type() or if is a method with callable().

All previous answers are correct, you have three options for what you are asking

  1. dir()

  2. vars()

  3. __dict__

>>> dir(a)['__class__', '__delattr__', '__dict__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__le__', '__lt__', '__module__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', 'multi', 'str']>>> vars(a){'multi': 4, 'str': '2'}>>> a.__dict__{'multi': 4, 'str': '2'}