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.
Both the Vars() and dict methods will work for the example the OP posted, but they won't work for "loosely" defined objects like:
To print all non-callable attributes, you can use the following function:
Every object has a
__dict__
variable containing all the variables and its values in it.Try this
You can also test if an object has a specific variable with:
Your example shows "instance variables", not really class variables.
Look in
hi_obj.__class__.__dict__.items()
for the class variables, along with other other class members like member functions and the containing module.Class variables are shared by all instances of the class.
Use vars()
Suggest
In otherwords, it essentially just wraps __dict__