37,722
社区成员
发帖
与我相关
我的任务
分享
import gc
class Person:
count=0
def __init__(self,name):
self.name=name
self.myname=name # 必须指明self.对象变量名,否则视为函数局部变量
Person.count+=1 # 必须用类名.类变量名
print 'Init! name=%s, count=' % name,Person.count
def __del__(self):
Person.count-=1
print 'Del name=%s, count=' % self.myname,Person.count
gc.set_debug(gc.DEBUG_STATS | gc.DEBUG_LEAK)
swaroop = Person('Swaroop')
swaroo1p = Person('Swaro2op') # swaroo2p不出错,swaroo1p出错。郁闷啊
kalam = Person('Abdul Kalam')
class P:
pc=0
def __init__(self,n):
self.n=n
P.pc+=1
print n,'P.pc:',P.pc
def __del__(self):
P.pc-=1
print self.n,'__del__:',P.pc
p1=P('a')
p2=P('b')
p3=P('c')