Python论坛  - 讨论区

标题:[python-chinese] 对象析构的一个问题

2006年12月07日 星期四 17:28

hyz hyzmick在gmail.com
星期四 十二月 7 17:28:14 HKT 2006

СµÜ¿ªÊ¼Ñ§Ï°Pythonû¶à¾Ã¡£ÔÚʵÑéÒ»¸öÀý×ÓµÄʱºò¶Ô¶ÔÏóµÄÎö¹¹Ê±»úÒÔ¼°Îö¹¹²úÉúµÄЧ¹û²»ÊǺÜÃ÷°×~´úÂëÈçÏ£º

class Person:
    population = 0
    def __init__(self, name):
        self.name = name
        print '(Initializing %s)' % self.name
        Person.population += 1
    def __del__(self):
        print '%s says bye.' % self.name
        Person.population -= 1
        if Person.population == 0:
            print 'I am the last one.'
        else:
            print 'There are still %d people 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 persons here.' % Person.population
swaroop = Person('Swaroop')
swaroop.sayHi()
swaroop.howMany()

kalam = Person('Abdul Kalam')
kalam.sayHi()
kalam.howMany()

swaroop.sayHi()
swaroop.howMany()
kalam.__del__()
kalam.sayHi()
kalam.howMany()
swaroop.sayHi()
swaroop.howMany()
ÆäÊä³öµÄ½á¹ûÊÇ£º

(Initializing Swaroop)
Hi, my name is Swaroop.
I am the only person here.
(Initializing Abdul Kalam)
Hi, my name is Abdul Kalam.
We have 2 persons here.
Hi, my name is Swaroop.
We have 2 persons here.
Abdul Kalam says bye.    {__del__()º¯Êýrun}
There are still 1 people left.
Hi, my name is Abdul Kalam.  {ÕâÀïµÄKalam²»ÊÇSaybyeÁËô£¿Ôõô»¹ÄÜ˵»°ÄØ£¿}
I am the only person here.
Hi, my name is Swaroop.
I am the only person here.
-------------- 下一部分 --------------
Ò»¸öHTML¸½¼þ±»ÒƳý...
URL: http://python.cn/pipermail/python-chinese/attachments/20061207/fe386920/attachment.htm 

[导入自Mailman归档:http://www.zeuux.org/pipermail/zeuux-python]

2006年12月07日 星期四 17:41

tocer tocer.deng在gmail.com
星期四 十二月 7 17:41:35 HKT 2006

把 kalam.__del__() 这句改成

del kalam

应该就可以了,系统执行这句时,会自动调用 __del__ 方法,你这种调用方式使得Python
无法删除 kalam 对象,只是执行了此方法而已,不是真正的解构。

hyz wrote::
> 小弟开始学习Python没多久。在实验一个例子的时候对对象的析构时机以及析构产 
> 生的效果不是很明白~代码如下:
> 
> class Person:
>    population = 0
>    def __init__(self, name):
>        self.name = name
>        print '(Initializing %s)' % self.name
>        Person.population += 1
>    def __del__(self):
>        print '%s says bye.' % self.name
>        Person.population -= 1
>        if Person.population == 0:
>            print 'I am the last one.'
>        else:
>            print 'There are still %d people 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 persons here.' % Person.population
> swaroop = Person('Swaroop')
> swaroop.sayHi()
> swaroop.howMany()
> 
> kalam = Person('Abdul Kalam')
> kalam.sayHi()
> kalam.howMany()
> 
> swaroop.sayHi()
> swaroop.howMany()
> kalam.__del__()
> kalam.sayHi()
> kalam.howMany()
> swaroop.sayHi()
> swaroop.howMany()
> 其输出的结果是:
> 
> (Initializing Swaroop)
> Hi, my name is Swaroop.
> I am the only person here.
> (Initializing Abdul Kalam)
> Hi, my name is Abdul Kalam.
> We have 2 persons here.
> Hi, my name is Swaroop.
> We have 2 persons here.
> Abdul Kalam says bye.    {__del__()函数run}
> There are still 1 people left.
> Hi, my name is Abdul Kalam.  {这里的Kalam不是Saybye了么?怎么还能说话呢?}
> I am the only person here.
> Hi, my name is Swaroop.
> I am the only person here.
> 
> 
> ------------------------------------------------------------------------
> 
> _______________________________________________
> python-chinese
> Post: send python-chinese在lists.python.cn
> Subscribe: send subscribe to python-chinese-request在lists.python.cn
> Unsubscribe: send unsubscribe to  python-chinese-request在lists.python.cn
> Detail Info: http://python.cn/mailman/listinfo/python-chinese

-- 
Vim 中文 Google 论坛 http://groups.google.com/group/Vim-cn

[导入自Mailman归档:http://www.zeuux.org/pipermail/zeuux-python]

如下红色区域有误,请重新填写。

    你的回复:

    请 登录 后回复。还没有在Zeuux哲思注册吗?现在 注册 !

    Zeuux © 2025

    京ICP备05028076号