Here is a little new Python question about instance variables.
Consider the following Python 2.7 class definition:
class Foo(object):
a = 1
def __init__(self):
self.b = 2
def __repr__(self):
return "%s" % self.__dict__
Now, when I create an instance Foo, it Foo.__dict__contains b, but not a.
>>> x=Foo()
>>> x
{'b': 2}
>>> dir(x)
['__class__', '__delattr__', '__dict__', '__doc__', '__format__',
'__getattribute__', '__hash__', '__init__', '__module__', '__new__',
'__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__',
'__str__', '__subclasshook__', '__weakref__', 'a', 'b']
>>> x.__dict__
{'b': 2}
And here I thought that I understand the Python Path very well.
What is the difference between x.aand x.b? As far as I can tell, they are both instance variables.
: , Python , Foo.a , . Hm... , , x.a, x - , - Foo.a:
>>> y=Foo()
>>> y.a = 2
>>> y
{'a': 2, 'b': 2}
>>> x
{'b': 2}
>>> x.a
1
>>> z=Foo()
>>> z
{'b': 2}
>>> z.a
1
>>> Foo.a
1
>>> x.a
1
>>> y.a
2
, Foo.a Foo, Foo.a:
>>> Foo.a=999
>>> x.a
999
>>> y.a
2