2007年11月25日 星期日 17:18
¿´Á˼òÃ÷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
2007年11月26日 星期一 19:40
我这里运行的结果是: ---------- 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__是自动调用的,但是我的实验结果不是这样的 >
2007年11月27日 星期二 00:56
¿ÉÄÜÄã¶Ô __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
Zeuux © 2025
京ICP备05028076号