
python __del__调用,返回类属性和对象属性的差别 50
麻烦各位大神帮我看一下这三段代码的输出之间的差别第一段代码:classInstCt(object):count=0#countisclassattrcount是一个类属性...
麻烦各位大神帮我看一下这三段代码的输出之间的差别
第一段代码:
class InstCt(object):
count = 0 # count is class attr count 是一个类属性
def __init__(self): # increment count 增加 count
InstCt.count += 1
def __del__(self): # decrement count 减少 count
InstCt.count -= 1
def howMany(self): # return count 返回 count
return InstCt.count
a = InstCt()
b = InstCt()
print b.howMany()
print a.howMany()
del b
print a.howMany()
del a
print InstCt.count
第二段代码:
class InstCt(object):
count = 0 # count is class attr count 是一个类属性
def __init__(self): # increment count 增加 count
self.count += 1
def __del__(self): # decrement count 减少 count
self.count -= 1
def howMany(self): # return count 返回 count
return self.count
a = InstCt()
c=a
b = InstCt()
print b.howMany()
print a.howMany()
del b
print a.howMany()
del a
print InstCt.count
第三段代码:
class InstCt(object):
count = 0 #
def __init__(self):
InstCt.count += 1
def __del__(self):
InstCt.count -= 1
def howMany(self):
return InstCt.count
a = InstCt()
c=a
b = InstCt()
print a.howMany()
print c.howMany()
print b.howMany()
del b
print a.howMany()
del a
print InstCt.count
主要我不明白以下情况:
1> 第一段代码和第二段代码就只是改了InstCt.count和self.count,为什么会使得结果不同?
2> 第三段代码中,c=a应该是增加了一次引用计数(删除a和b,再print InstCt.count,此时输出为1,也证明了这一点),那为什么前面a.howMany()、b.howMany()、c.howMany()都是2而不是3呢? 展开
第一段代码:
class InstCt(object):
count = 0 # count is class attr count 是一个类属性
def __init__(self): # increment count 增加 count
InstCt.count += 1
def __del__(self): # decrement count 减少 count
InstCt.count -= 1
def howMany(self): # return count 返回 count
return InstCt.count
a = InstCt()
b = InstCt()
print b.howMany()
print a.howMany()
del b
print a.howMany()
del a
print InstCt.count
第二段代码:
class InstCt(object):
count = 0 # count is class attr count 是一个类属性
def __init__(self): # increment count 增加 count
self.count += 1
def __del__(self): # decrement count 减少 count
self.count -= 1
def howMany(self): # return count 返回 count
return self.count
a = InstCt()
c=a
b = InstCt()
print b.howMany()
print a.howMany()
del b
print a.howMany()
del a
print InstCt.count
第三段代码:
class InstCt(object):
count = 0 #
def __init__(self):
InstCt.count += 1
def __del__(self):
InstCt.count -= 1
def howMany(self):
return InstCt.count
a = InstCt()
c=a
b = InstCt()
print a.howMany()
print c.howMany()
print b.howMany()
del b
print a.howMany()
del a
print InstCt.count
主要我不明白以下情况:
1> 第一段代码和第二段代码就只是改了InstCt.count和self.count,为什么会使得结果不同?
2> 第三段代码中,c=a应该是增加了一次引用计数(删除a和b,再print InstCt.count,此时输出为1,也证明了这一点),那为什么前面a.howMany()、b.howMany()、c.howMany()都是2而不是3呢? 展开
3个回答
展开全部
1、InstCt.count为类属性,self.count为实例属性,修改类属性和修改实例属性,影响的并不是同一个对象,结果自然不一致
2、c=a是创建一个a对象的引用,这里不会再次对类属性进行更改,当你执行del a的时候,基于这个对象的引用a被删除,但是该实例化对象依然存在,可以通过c去访问。
这个地方说起来有点麻烦,可以简单理解成a和c是指向同一个InstCt实例的指针,当对象的引用计数为0的时候虚拟机才会回收这段内存
2、c=a是创建一个a对象的引用,这里不会再次对类属性进行更改,当你执行del a的时候,基于这个对象的引用a被删除,但是该实例化对象依然存在,可以通过c去访问。
这个地方说起来有点麻烦,可以简单理解成a和c是指向同一个InstCt实例的指针,当对象的引用计数为0的时候虚拟机才会回收这段内存
展开全部
(1)在第一段中InstCt.count 这个InstCt是指向类本身 所以调用2次后InstCt.count增加到2
第二段中self.count self指向的是实例本身,即这里的a,b 所以每次调用时候count初始值是0
导致结果不同
(2)第三个理解错了,当你使用c=a时候,这里是c=a,并不是c复制a中的内容。这与shell有点不同。c=a,指向的地址是同一个,你可以使用id(c) id(a) 来测试。并没有新增加。 所以当你del a以后,其实这个变量还是在的,存在与c中。当你最后print InstCt.count 以后 这个变量才自动消除。
这就是为什么第一个del a以后 得到0 第三个 del a以后还是1的原因。
第二段中self.count self指向的是实例本身,即这里的a,b 所以每次调用时候count初始值是0
导致结果不同
(2)第三个理解错了,当你使用c=a时候,这里是c=a,并不是c复制a中的内容。这与shell有点不同。c=a,指向的地址是同一个,你可以使用id(c) id(a) 来测试。并没有新增加。 所以当你del a以后,其实这个变量还是在的,存在与c中。当你最后print InstCt.count 以后 这个变量才自动消除。
这就是为什么第一个del a以后 得到0 第三个 del a以后还是1的原因。
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
我的理解是这样的:
1> InstCt.count会统计该类的调用次数,而self.count只会同意某个实例的调用次数。
2> c=a 其实并没有再创建一个实例,这只是添加了一个指针(可能不是很准确)指向a指向的object(也就是InstCt实例),这个可以用print a/c得到证实。所以计数仍然是2。
print a
print c
实例是一样的:
<__main__.InstCt object at 0x7fa764c25610>
<__main__.InstCt object at 0x7fa764c25610>
1> InstCt.count会统计该类的调用次数,而self.count只会同意某个实例的调用次数。
2> c=a 其实并没有再创建一个实例,这只是添加了一个指针(可能不是很准确)指向a指向的object(也就是InstCt实例),这个可以用print a/c得到证实。所以计数仍然是2。
print a
print c
实例是一样的:
<__main__.InstCt object at 0x7fa764c25610>
<__main__.InstCt object at 0x7fa764c25610>
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询
广告 您可能关注的内容 |