2005年08月29日 星期一 20:12
不奇怪,
由于GC的存在Python中__del__调用本来就在很多时候是不可预测的.
不过这个例子里面还是很明显的, 在
zhgx = Person('zhgx');
zhgx.SayHi();
zhgx.HowMany();
后 zhgx 就没用了, 所以就自动清理掉了, 因此zhgx是在
bazaari前面del的.
这个顺序应该是在程序编译成字节码的时候就确定了.
Lee在IDEL里面是另一个情况, IDEL是解释器,
不当不能确定zhgx在以后是否会再用到,
所以zhgx不会自动del.
不知道解释清楚没~~,
我的建议是初学的时候不必太纠缠于这些细节.
python的好处就是不用自己做内存管理,
就用他的自动功能吧, 很舒服.
--- Albert Lee <hanzhupeng at gmail.com>写道:
> 确实很奇怪,我把 __del__ 方法加了两行:
> def __del__(self):
> '''I am dying.'''
> print '%s says bye.' % self.name;
> print '---'
> print dir(Person)
>
> Person.population -= 1;
> if Person.population == 0:
> print 'I am the last one.'
> else:
> print 'There are still %d people left.' %
> Person.population;
>
> print dir(Person)
>
> 结果,在 cmd 下独立运行,两次的结果完全不同
>
> 而在 idle 下,用
> del zhgx
> del bazaari
> 的结果,就是正常的:
>
> >>>
> (Initializing Bazaari)
> Hello, my name is Bazaari
> I am the only person here.
> (Initializing zhgx)
> Hello, my name is zhgx
> We have 2 person here.
> Hello, my name is Bazaari
> zhgx says bye.
> ---
> ['HowMany', 'SayHi', '__del__', '__doc__',
> '__init__', '__module__',
> 'population']
> There are still 1 people left.
> Bazaari says bye.
> ---
> ['HowMany', 'SayHi', '__del__', '__doc__',
> '__init__', '__module__',
> 'population']
> I am the last one.
> >>>
>
>
>
> On 8/29/05, 广星 <guangxing at ict.ac.cn> wrote:
> > 在学习Python,根据例子练习程序。
> > 在类与对象的方法这一章中遇到了如下的问题,
> > 困惑不已向大家请教。
> >
> >
>
第一个问题:为什么在点击RUN的时候会报如下的提示呢?到底是不是错误》如果不是,到底是怎么回事呢?
> > Exception exceptions.AttributeError: "'NoneType'
> object has no attribute 'population'" in > method Person.__del__ of <__main__.Person instance
> at 0x009335D0>> ignored
> >
> >
> 第二个问题:脚本执行的结果如下,为什么会冒出来"zhgx
> says
>
bye."呢?为什么在新生成对象的实例的时候自动调用了__del__呢?
> > (Initializing Bazaari)
> > Hello, my name is Bazaari
> > I am the only person here.
> > (Initializing zhgx)
> > Hello, my name is zhgx
> > We have 2 person here.
> > Hello, my name is Bazaari
> > zhgx says bye.
> > There are still 1 people left.
> > Bazaari says bye.
> >
> >
>
*************************************************************************************************************************
> >
>
***************************************************源代码****************************************************************
> >
>
*************************************************************************************************************************
> > #!/usr/bin/env python
> > #Boa:PyApp:main
> >
> > modules ={}
> >
> > class Person:
> > '''Represents a person.'''
> > population = 0;
> >
> > def __init__(self, name):
> > '''Initializes the person's data.'''
> > self.name = name;
> > print '(Initializing %s)' % self.name
> >
> > 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 'Hello, 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 person here.' %
> Person.population;
> >
> > bazaari = Person('Bazaari');
> > bazaari.SayHi();
> > bazaari.HowMany();
> >
> > zhgx = Person('zhgx');
> > zhgx.SayHi();
> > zhgx.HowMany();
> >
> > bazaari.SayHi();
> >
>
*************************************************************************************************************************
> >
>
***************************************************源代码****************************************************************
> >
>
*************************************************************************************************************************
> >
> >
> > 广星
> > guangxing at ict.ac.cn
> > 2005-08-29
> >
> > _______________________________________________
> > python-chinese list
> > python-chinese at lists.python.cn
> > http://python.cn/mailman/listinfo/python-chinese
> >
> >
> >
> > _______________________________________________
> python-chinese list
> python-chinese at lists.python.cn
> http://python.cn/mailman/listinfo/python-chinese
>
==========================================================
A student of Math in Soochow University in China.
Intrested in Math, Python, Octave, Lisp, Maxima, Prolog and .NET/MONO.
Blog: http://blog.sina.com.cn/blog/1142604745
Wiki: http://wiki.woodpecker.org.cn/moin/Hoxide
___________________________________________________________
雅虎免费G邮箱-No.1的防毒防垃圾超大邮箱
http://cn.mail.yahoo.com
2005年08月29日 星期一 22:37
多谢解释,现在明白啦!
> 不奇怪,
>由于GC的存在Python中__del__调用本来就在很多时候是不可预测的.
>不过这个例子里面还是很明显的, 在
> zhgx = Person('zhgx');
> zhgx.SayHi();
> zhgx.HowMany();
>后 zhgx 就没用了, 所以就自动清理掉了, 因此zhgx是在
>bazaari前面del的.
>这个顺序应该是在程序编译成字节码的时候就确定了.
>Lee在IDEL里面是另一个情况, IDEL是解释器,
>不当不能确定zhgx在以后是否会再用到,
>所以zhgx不会自动del.
可是要是我在后面还会用到zhgx怎么办呢?
系统自动把他删除了,岂不是会造成内存访问错误?
>
>不知道解释清楚没~~,
>我的建议是初学的时候不必太纠缠于这些细节.
>python的好处就是不用自己做内存管理,
>就用他的自动功能吧, 很舒服.
>
>
>--- Albert Lee <hanzhupeng at gmail.com>写道:
>
>> 确实很奇怪,我把 __del__ 方法加了两行:
>> def __del__(self):
>> '''I am dying.'''
>> print '%s says bye.' % self.name;
>> print '---'
>> print dir(Person)
>>
>> Person.population -= 1;
>> if Person.population == 0:
>> print 'I am the last one.'
>> else:
>> print 'There are still %d people left.' %
>> Person.population;
>>
>> print dir(Person)
>>
>> 结果,在 cmd 下独立运行,两次的结果完全不同
>>
>> 而在 idle 下,用
>> del zhgx
>> del bazaari
>> 的结果,就是正常的:
>>
>> >>>
>> (Initializing Bazaari)
>> Hello, my name is Bazaari
>> I am the only person here.
>> (Initializing zhgx)
>> Hello, my name is zhgx
>> We have 2 person here.
>> Hello, my name is Bazaari
>> zhgx says bye.
>> ---
>> ['HowMany', 'SayHi', '__del__', '__doc__',
>> '__init__', '__module__',
>> 'population']
>> There are still 1 people left.
>> Bazaari says bye.
>> ---
>> ['HowMany', 'SayHi', '__del__', '__doc__',
>> '__init__', '__module__',
>> 'population']
>> I am the last one.
>> >>>
>>
>>
>>
>> On 8/29/05, 广星 <guangxing at ict.ac.cn> wrote:
>> > 在学习Python,根据例子练习程序。
>> > 在类与对象的方法这一章中遇到了如下的问题,
>> > 困惑不已向大家请教。
>> >
>> >
>>
>第一个问题:为什么在点击RUN的时候会报如下的提示呢?到底是不是错误》如果不是,到底是怎么回事呢?
>> > Exception exceptions.AttributeError: "'NoneType'
>> object has no attribute 'population'" in >> method Person.__del__ of <__main__.Person instance
>> at 0x009335D0>> ignored
>> >
>> >
>> 第二个问题:脚本执行的结果如下,为什么会冒出来"zhgx
>> says
>>
>bye."呢?为什么在新生成对象的实例的时候自动调用了__del__呢?
>> > (Initializing Bazaari)
>> > Hello, my name is Bazaari
>> > I am the only person here.
>> > (Initializing zhgx)
>> > Hello, my name is zhgx
>> > We have 2 person here.
>> > Hello, my name is Bazaari
>> > zhgx says bye.
>> > There are still 1 people left.
>> > Bazaari says bye.
>> >
>> >
>>
>*************************************************************************************************************************
>> >
>>
>***************************************************源代码****************************************************************
>> >
>>
>*************************************************************************************************************************
>> > #!/usr/bin/env python
>> > #Boa:PyApp:main
>> >
>> > modules ={}
>> >
>> > class Person:
>> > '''Represents a person.'''
>> > population = 0;
>> >
>> > def __init__(self, name):
>> > '''Initializes the person's data.'''
>> > self.name = name;
>> > print '(Initializing %s)' % self.name
>> >
>> > 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 'Hello, 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 person here.' %
>> Person.population;
>> >
>> > bazaari = Person('Bazaari');
>> > bazaari.SayHi();
>> > bazaari.HowMany();
>> >
>> > zhgx = Person('zhgx');
>> > zhgx.SayHi();
>> > zhgx.HowMany();
>> >
>> > bazaari.SayHi();
>> >
>>
>*************************************************************************************************************************
>> >
>>
>***************************************************源代码****************************************************************
>> >
>>
>*************************************************************************************************************************
>> >
>> >
>> > 广星
>> > guangxing at ict.ac.cn
>> > 2005-08-29
>> >
>> > _______________________________________________
>> > python-chinese list
>> > python-chinese at lists.python.cn
>> > http://python.cn/mailman/listinfo/python-chinese
>> >
>> >
>> >
>> > _______________________________________________
>> python-chinese list
>> python-chinese at lists.python.cn
>> http://python.cn/mailman/listinfo/python-chinese
>>
>
>
>==========================================================
>
>A student of Math in Soochow University in China.
>Intrested in Math, Python, Octave, Lisp, Maxima, Prolog and .NET/MONO.
>
>Blog: http://blog.sina.com.cn/blog/1142604745
>Wiki: http://wiki.woodpecker.org.cn/moin/Hoxide
>
>
>
>
>
>
>___________________________________________________________
>雅虎免费G邮箱-中国第一绝无垃圾邮件骚扰超大邮箱
>http://cn.mail.yahoo.com
>
>_______________________________________________
>python-chinese list
>python-chinese at lists.python.cn
>http://python.cn/mailman/listinfo/python-chinese
>
= = = = = = = = = = = = = = = = = = = =
致
礼!
guangxing
guangxing at ict.ac.cn
2005-08-29
2005年08月30日 星期二 09:17
之前我看了一些关于Python具有强大的自省能力的文章,对Python的自省能力有了一定的了解,现在我想编写这样一个函数,把类对象的所有数据成员的“<名称,值>”打印出来,由于时间比较紧迫,我自己对Python这方面的了解还不多,请大虾们能够指点指点,谢谢! -------------- next part -------------- An HTML attachment was scrubbed... URL: http://lists.exoweb.net/pipermail/python-chinese/attachments/20050830/77abadbe/attachment.htm
2005年08月30日 星期二 09:28
在 05-8-30,xu.shengyong<zjxushengyong at hotmail.com> 写道: > > 之前我看了一些关于Python具有强大的自省能力的文章,对Python的自省能力有了一定的了解,现在我想编写这样一个函数,把类对象的所有数据成员的"<名称,值>"打印出来,由于时间比较紧迫,我自己对Python这方面的了解还不多,请大虾们能够指点指点,谢谢! a.__dict__就是。 -- I like python! My Donews Blog: http://www.donews.net/limodou
2005年08月30日 星期二 10:51
谢谢limodou大虾,:),刚才我用你介绍的方法可以把一个任意类型对象的所有数据成员的"<名称,值>"对打印出来,但是如果我还想知道数据成员的类型,即打印的是"<名称,类型,值>",是否能够实现这种功能?谢谢!
>>> class Var:
... pass
...
>>> var = Var()
>>> var.name = "esuncn"
>>> var.type = "str"
>>> var.mx = 123
>>> var.__dict__
{'type': 'str', 'name': 'esuncn', 'mx': 123}
----- Original Message -----
From: "limodou" <limodou at gmail.com>
To: <python-chinese at lists.python.cn>
Sent: Tuesday, August 30, 2005 9:28 AM
Subject: Re: [python-chinese] 请教:关于Python的“自省”能力???
>在 05-8-30,xu.shengyong<zjxushengyong at hotmail.com> 写道:
>>
>> 之前我看了一些关于Python具有强大的自省能力的文章,对Python的自省能力有了一定的了解,现在我想编写这样一个函数,把类对象的所有数据成员的"<名称,值>"打印出来,由于时间比较紧迫,我自己对Python这方面的了解还不多,请大虾们能够指点指点,谢谢!
>
> a.__dict__就是。
>
>
> --
> I like python!
> My Donews Blog: http://www.donews.net/limodou
>
--------------------------------------------------------------------------------
> _______________________________________________
> python-chinese list
> python-chinese at lists.python.cn
> http://python.cn/mailman/listinfo/python-chinese
>
2005年08月30日 星期二 10:53
dir , type , dis.dis On 8/30/05, xu.shengyong <zjxushengyong at hotmail.com> wrote: > > > 谢谢limodou大虾,:),刚才我用你介绍的方法可以把一个任意类型对象的所有数据成员的"<名称,值>"对打印出来,但是如果我还想知道数据成员的类型,即打印的是"<名称,类型,值>",是否能够实现这种功能?谢谢! > > >>> class Var: > ... pass > ... > >>> var = Var() > >>> var.name <http://var.name> = "esuncn" > >>> var.type = "str" > >>> var.mx <http://var.mx> = 123 > >>> var.__dict__ > {'type': 'str', 'name': 'esuncn', 'mx': 123} > > > > ----- Original Message ----- > From: "limodou" <limodou at gmail.com> > To: <python-chinese at lists.python.cn> > Sent: Tuesday, August 30, 2005 9:28 AM > Subject: Re: [python-chinese] 请教:关于Python的"自省"能力??? > > > >在 05-8-30,xu.shengyong<zjxushengyong at hotmail.com> 写道: > >> > >> > 之前我看了一些关于Python具有强大的自省能力的文章,对Python的自省能力有了一定的了解,现在我想编写这样一个函数,把类对象的所有数据成员的"<名称,值>"打印出来,由于时间比较紧迫,我自己对Python这方面的了解还不多,请大虾们能够指点指点,谢谢! > > > > a.__dict__就是。 > > > > > > -- > > I like python! > > My Donews Blog: http://www.donews.net/limodou > > > > > > -------------------------------------------------------------------------------- > > > > _______________________________________________ > > python-chinese list > > python-chinese at lists.python.cn > > http://python.cn/mailman/listinfo/python-chinese > > > _______________________________________________ > python-chinese list > python-chinese at lists.python.cn > http://python.cn/mailman/listinfo/python-chinese > > > -------------- next part -------------- An HTML attachment was scrubbed... URL: http://lists.exoweb.net/pipermail/python-chinese/attachments/20050830/707f3a90/attachment.htm
2005年08月30日 星期二 11:13
在 05-8-30,xu.shengyong<zjxushengyong at hotmail.com> 写道: > 谢谢limodou大虾,:),刚才我用你介绍的方法可以把一个任意类型对象的所有数据成员的"<名称,值>"对打印出来,但是如果我还想知道数据成员的类型,即打印的是"<名称,类型,值>",是否能够实现这种功能?谢谢! 使用type(obj)即可。 -- I like python! My Donews Blog: http://www.donews.net/limodou
Zeuux © 2025
京ICP备05028076号