如下代码,我在最后是修改了类属性 还是定义了一个变量???
class Person(object):
__count = 0
def __init__(self, name):
Person.__count=Person.__count+1
self.name =name
print Person.__count
p1 = Person('Bob')
p2 = Person('Alice')
Person.__count=5
print Person.__count
class Person(object):
__count = 0
def __init__(self, name):
Person.__count=Person.__count+1
self.name =name
print Person.__count
p1 = Person('Bob')
p2 = Person('Alice')
Person.__count=5
print Person.__count
2015-05-27
class Person(object):
__count = 0
def __init__(self, name):
Person.__count=Person.__count+1
self.name =name
print Person.__count
p1 = Person('Bob') #1
p2 = Person('Alice') #2
Person.__count=5
print Person.__count #5
p3 = Person('ooo') #3 还是没有变化
print ', '.join(['%s:%s' % item for item in Person.__dict__.items()])
#__count:5, __module__:__main__, _Person__count:3, __dict__:<attribute '__dict__' of 'Person' objects>, __weakref__:<attribute '__weakref__' of 'Person' objects>, __doc__:None, __init__:<function __init__ at 0x013A8830>
#看到2个属性__count:5, _Person__count:3,想必楼主明白了:)
举报