I'm not sure how self.w += 1 will create an instance variables instead of just increment class variable or raise an exception for missing instance variable?
这可能有助于您理解它:
>>> class Foo:
... x = 1
... def __init__(self):
... print 'self =', self
... print 'self x =', self.x
... print 'foo x =', Foo.x
... print self.x is Foo.x
... Foo.x = 2
... self.x = 3
... print 'self x =', self.x
... print 'foo x =', Foo.x
...
>>> a = Foo()
self = <__main__.Foo instance at 0x1957c68>
self x = 1
foo x = 1
True
self x = 2
foo x = 3
>>> print a
<__main__.Foo instance at 0x1957c68>