修改类属性 分类: divide into python 2013-11-06 16:48 236人阅读 评论(0) 收藏
例子一:
一下是修改类属性,
class counter:
count = 0
def __init__(self):
self.__class__.count+=1 #此时类属性被类和所有类实例共享
print counter.count # 0
c=counter()
print c.count # 1
d=counter()
print d.count #2
print counter.count #2
例子二:
class counter(object):
count=0
class_co=0
def __init__(self):
self.count+=1 #实例所拥有
counter.class_co+=1
print counter.count,counter.class_co # 0 0
c=counter()
print c.count,c.class_co # 1 1
a=counter()
print a.count,c.class_co # 1 2
版权声明:本文为博主原创文章,未经博主允许不得转载。