Python论坛  - 讨论区

标题:[python-chinese] 方法__del__是自动调用吗?

2007年11月25日 星期日 17:18

lijie hipertic在126.com
星期日 十一月 25 17:18:37 HKT 2007

¿´Á˼òÃ÷python½Ì³ÌµÄÒ»¸ö³ÌÐò£ºÈçÏ£º

Àý11.4 ʹÓÃÀàÓë¶ÔÏóµÄ±äÁ¿

#!/usr/bin/python
# Filename: objvar.py

class Person:
    '''Represents a person.'''
    population = 0

    def __init__(self, name):
        '''Initializes the person's data.'''
        self.name = name
        print '(Initializing %s)' % self.name

        # When this person is created, he/she
        # adds to the population
        Person.population += 1

    def __del__(self):
        '''I am dying.'''
        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):
        '''Greeting by the person.

        Really, that's all it does.'''
        print 'Hi, my name is %s.' % self.name

    def howMany(self):
        '''Prints the current population.'''
        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()

Àý×Ó¸ø³öµÄÔËÐнá¹ûÊÇ£º

$ python objvar.py
(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.
There are still 1 people left.
Swaroop says bye.
I am the last one.

¶øÎÒÔËÐÐÕâ¶Î³ÌÐòµÄʱºò£¬½á¹ûÈçÏ£¬Ã»ÓÐÉϱߴòбÌåµÄÊä³ö£º
(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.

ÕâÊÇΪʲôÄØ£¿Ô­Àý×ÓºÃÏñÈÏΪ·½·¨__del__ÊÇ×Ô¶¯µ÷Óõģ¬µ«ÊÇÎÒµÄʵÑé½á¹û²»ÊÇÕâÑùµÄ
-------------- 下一部分 --------------
Ò»¸öHTML¸½¼þ±»ÒƳý...
URL: http://python.cn/pipermail/python-chinese/attachments/20071125/7e3026d9/attachment.html 

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

2007年11月26日 星期一 19:40

R Y digitalwit在gmail.com
星期一 十一月 26 19:40:51 HKT 2007

我这里运行的结果是:

---------- Compile  ----------
(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.
There are still 1 people left.
Swaroop says bye.
I am the last one.

---------------------------------
Output completed


可能是什么原因使__del__应该被调用,却没有被调用;因为引用计数为0,对象才被清除。

On 11/25/07, lijie <hipertic at 126.com> wrote:
> 看了简明python教程的一个程序:如下:
>
> 例11.4 使用类与对象的变量
>
> #!/usr/bin/python
> # Filename: objvar.py
>
> class Person:
>     '''Represents a person.'''
>     population = 0
>
>     def __init__(self, name):
>         '''Initializes the person's data.'''
>         self.name = name
>         print '(Initializing %s)' % self.name
>
>         # When this person is created, he/she
>         # adds to the population
>         Person.population += 1
>
>     def __del__(self):
>         '''I am dying.'''
>         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):
>         '''Greeting by the person.
>
>         Really, that's all it does.'''
>         print 'Hi, my name is %s.' % self.name
>
>     def howMany(self):
>         '''Prints the current population.'''
>         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()
>
> 例子给出的运行结果是:
>
> $ python objvar.py
> (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.
> There are still 1 people left.
> Swaroop says bye.
> I am the last one.
>
> 而我运行这段程序的时候,结果如下,没有上边打斜体的输出:
> (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.
>
> 这是为什么呢?原例子好像认为方法__del__是自动调用的,但是我的实验结果不是这样的
>

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

2007年11月27日 星期二 00:56

Cyril.Liu terry6394在gmail.com
星期二 十一月 27 00:56:13 HKT 2007

¿ÉÄÜÄã¶Ô __XXX__Ò»Àà·½·¨"×Ô¶¯µ÷ÓÃ"µÄÀí½âÓеãÎó½â¡£
Èç¹ûÄãÊìϤOOP£¬ÎÒ¾õµÃÕâ¸öÀà·½·¨Äã´ó¿ÉÒÔÈÏΪÊÇ "¶à̬" ÐеÄÁé»îÔËÓã¨Õâ˵δ±ØÕýÈ·£¬ÎÒÖ»ÊǾõµÃÕâÑù˵¿ÉÄܶÔÊìϤOOPµÄÈËÀ´Êé¸üÈÝÒ×Àí½â£©£º¾ÍÈç
__str__·½·¨£¬Èç¹ûÄãµÄpersion¶ÔÏóÀïʵÏÖÁËËû£¬ÄÇôÄãÔÚʹÓÃprint personObj ÕâÑùµÄ·½·¨µÄʱºò¾Í»á±»×Ô¶¯µ÷Óã¬ÆäЧ¹ûµÈͬÓÚ
print personObj.__str__()
ÄÇô£¬¶ÔÓÚÄã__del__µÄÎÊÌ⣬ҪÆä"×Ô¶¯Ö´ÐÐ"Ò²¾ÍÊÇÔÚ
Ö´ÐÐ del swaroop µÄʱºò __del__·½·¨»á×Ô¶¯±»µ÷Óá£
µÈͬÓÚ´úÂ룺
swaroop.__del__()



On Nov 26, 2007 7:40 PM, R Y <digitalwit at gmail.com> wrote:

> ÎÒÕâÀïÔËÐеĽá¹ûÊÇ£º
>
> ---------- Compile  ----------
> (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.
> There are still 1 people left.
> Swaroop says bye.
> I am the last one.
>
> ---------------------------------
> Output completed
>
>
> ¿ÉÄÜÊÇʲôԭÒòʹ__del__Ó¦¸Ã±»µ÷Óã¬È´Ã»Óб»µ÷Óã»ÒòΪÒýÓüÆÊýΪ0£¬¶ÔÏó²Å±»Çå³ý¡£
>
> On 11/25/07, lijie <hipertic at 126.com> wrote:
> > ¿´Á˼òÃ÷python½Ì³ÌµÄÒ»¸ö³ÌÐò£ºÈçÏ£º
> >
> > Àý11.4 ʹÓÃÀàÓë¶ÔÏóµÄ±äÁ¿
> >
> > #!/usr/bin/python
> > # Filename: objvar.py
> >
> > class Person:
> >     '''Represents a person.'''
> >     population = 0
> >
> >     def __init__(self, name):
> >         '''Initializes the person's data.'''
> >         self.name = name
> >         print '(Initializing %s)' % self.name
> >
> >         # When this person is created, he/she
> >         # adds to the population
> >         Person.population += 1
> >
> >     def __del__(self):
> >         '''I am dying.'''
> >         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):
> >         '''Greeting by the person.
> >
> >         Really, that's all it does.'''
> >         print 'Hi, my name is %s.' % self.name
> >
> >     def howMany(self):
> >         '''Prints the current population.'''
> >         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()
> >
> > Àý×Ó¸ø³öµÄÔËÐнá¹ûÊÇ£º
> >
> > $ python objvar.py
> > (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.
> > There are still 1 people left.
> > Swaroop says bye.
> > I am the last one.
> >
> > ¶øÎÒÔËÐÐÕâ¶Î³ÌÐòµÄʱºò£¬½á¹ûÈçÏ£¬Ã»ÓÐÉϱߴòбÌåµÄÊä³ö£º
> > (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.
> >
> > ÕâÊÇΪʲôÄØ£¿Ô­Àý×ÓºÃÏñÈÏΪ·½·¨__del__ÊÇ×Ô¶¯µ÷Óõģ¬µ«ÊÇÎÒµÄʵÑé½á¹û²»ÊÇÕâÑùµÄ
> >
> _______________________________________________
> python-chinese
> Post: send python-chinese at lists.python.cn
> Subscribe: send subscribe to python-chinese-request at lists.python.cn
> Unsubscribe: send unsubscribe to  python-chinese-request at lists.python.cn
> Detail Info: http://python.cn/mailman/listinfo/python-chinese




-- 
About Cyril.Liu
-----------------------------------
Cyril
ÊÇÒ»¸ö³ÌÐòÔ±,
ÏÖÔÚÊǸöÇî¹âµ°,
Ëû³£³£¸ú×Ô¼ºËµ:"Î҆ÕÒ»‚€ÓÐÀíÏë†þ΁ţ×Ð×Ð"
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://python.cn/pipermail/python-chinese/attachments/20071127/55d23c64/attachment.html 

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

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

    你的回复:

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

    Zeuux © 2025

    京ICP备05028076号