Here\'s a bit of a newbie Python question about instance variables.
Consider the following Python 2.7 class definition:
class Foo(object):
a = 1
a
is not an instance attribute, it's a class attribute.
May this help you further?
>>> class X(object):
def __getattribute__(self, name):
print name
return object.__getattribute__(self, name)
>>> l = dir(X())
__dict__
__members__
__methods__
__class__
>>> l
['__class__', '__delattr__', '__dict__', '__doc__', '__format__', '__getattribute__', '__hash__', '__init__', '__module__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__']
Your a
isn't an instance variable. You defined it as part of the class.
>>> class Foo(object):
... a = 1
...
>>> Foo.a
1
If you want an instance variable you should put it inside the __init__
method, because this method is called when your object is created.