Fix Python – How to get instance variables in Python?


Asked By – Chris Bunch

Is there a built-in method in Python to get an array of all a class’ instance variables? For example, if I have this code:

class hi:
  def __init__(self):
    self.ii = "foo"
    self.kk = "bar"

Is there a way for me to do this:

>>> mystery_method(hi)
["ii", "kk"]

Edit: I originally had asked for class variables erroneously.

Now we will see solution for issue: How to get instance variables in Python?


Every object has a __dict__ variable containing all the variables and its values in it.

Try this

>>> hi_obj = hi()
>>> hi_obj.__dict__.keys()


dict_keys(['ii', 'kk'])

This question is answered By – cnu

This answer is collected from stackoverflow and reviewed by FixPython community admins, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0