python对象释放顺序

看了一篇文章,里面提到一个python初始化对象及释放对象的顺序问题,其人测试的饶有兴致,我看了看,发现是随机释放顺序,虽然没什么用,但是挺有意思的。

最近在学习python,看的是《简明python教程》,写的很是通俗易懂。

在一个类和对象的变量的例子中,看到代码运行结果突然想到在python中对象的生命周期的问题,代码运行结果:

 

# !/usr/bin/env python
#
coding=utf-8
#
filename :objvar.py
class  Person:
    population 
=  0
    
def   __init__ (self,name):
        self.name 
=  name
        
print ( " initializing %s " % self.name)
        Person.population 
+=   1
    
def   __del__ (self):
        
print ( " %s say bye " % self.name)
        Person.population 
-= 1
        
if  Person.population  ==  0 :
            
print  ( " I'm the last one " )
        
else :
            
print  ( " There are still %d person left " % Person.population)
    
def  SayHi(self):
        
print  ( " Hi,my name is %s " % self.name)
    
def  HowMany(self):
        
if  Person.population  ==   1 :
            
print ( " I am the only Person here " )
        
else :
            
print  ( " We have %d person here " % Person.population)
            
swaroop 
=  Person( " Swaroop " # initializing Swaroop
swaroop.SayHi()      # Hi,my name is Swaroop
swaroop.HowMany()    # I am the only Person here

kalam 
=  Person( " Kalam " # initializing Kalam
kalam.SayHi()    # Hi,my name is Kalam
kalam.HowMany()  # We have 2 person here

swaroop.SayHi() 
# Hi,my name is Swaroop
swaroop.HowMany()    #
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值