|
|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
- class NewClass(object):
- num_count = 0
- def __init__(self,name):
- self.name = name
- <font color="#ff0000"> self.__class__.num_count</font> += 1
- print name,NewClass.num_count
- def __del__(self):
- <font color="#ff0000"> self.__class__.num_count</font> -= 1
- print "Del",self.name,<font color="#ff0000">self.__class__.num_count</font>
- def test():
- print "aa"
-
- aa = NewClass("Hello")
- bb = NewClass("World")
- cc = NewClass("aaaa")
-
- print "Over"
复制代码 输出结果:- Hello 1
- World 2
- aaaa 3
- Over
- Del Hello 2
- Del World 1
- Del aaaa 0
复制代码 为什么把NewClass 换成 self.__class__. 就可以直接输出del的结果,没换的话会报错 Exception AttributeError: "'NoneType' object has no attribute 'num_count'" in <bound method NewClass.__del__ of <__main__.NewClass object at 0x01AF1970>> ignored
|
|