Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Does python hate cathy?

Reply
Thread Tools

Does python hate cathy?

 
 
Edward A. Falk
Guest
Posts: n/a
 
      03-25-2008

Interestingly, if you change

swaroop = Person('Swaroop', 'M')
swaroop.sayHi()
swaroop.howMany()
kalam = Person('Abdul Kalam', 'M')
kalam.sayHi()
kalam.howMany()
cathy = Person('Catherine', 'F')
cathy.sayHi()
cathy.howMany()
swaroop.sayHi()
swaroop.howMany()

to

def main():
swaroop = Person('Swaroop', 'M')
swaroop.sayHi()
swaroop.howMany()
kalam = Person('Abdul Kalam', 'M')
kalam.sayHi()
kalam.howMany()
cathy = Person('Catherine', 'F')
cathy.sayHi()
cathy.howMany()
swaroop.sayHi()
swaroop.howMany()
return 0


if __name__ == "__main__":
sys.exit(main())


The problem goes away. (This is a good coding practice in any event.)

As others have pointed out, the order in which local variables are deleted
is undefined. It looks to me as if the class Person got deleted before
Catherine did.

Adding

del kalam
del cathy
del swaroop

to the end of the program did fix the problem, presumably by forcing
kalam, cathy, and swaroop from being deleted before Person.

However, Adding

self.myclass = Person

to the __init__() method didn't stop the problem. I thought it might, because
then each of the objects would have held a reference to Person. Actually, I would
have thought they'd hold a reference by merely existing, so is this not a bug?

--
-Ed Falk, http://www.velocityreviews.com/forums/(E-Mail Removed)
http://thespamdiaries.blogspot.com/
 
Reply With Quote
 
 
 
 
Gabriel Genellina
Guest
Posts: n/a
 
      03-25-2008
En Tue, 25 Mar 2008 02:09:58 -0300, Edward A. Falk <(E-Mail Removed)>
escribió:

> However, Adding
>
> self.myclass = Person
>
> to the __init__() method didn't stop the problem. I thought it might,
> because
> then each of the objects would have held a reference to Person.
> Actually, I would
> have thought they'd hold a reference by merely existing, so is this not
> a bug?


Yes, they already have a reference (self.__class__). The problem is that
__del__ references Person by *name*, and that name is reset to None (like
all other module globals) as part of the interpreter shutdown sequence.
Using self.__class__ would avoid this particular bug, but in general, it's
better not to rely on __del__ at all.

See http://bugs.python.org/issue1513802 and
http://bugs.python.org/issue1717900

--
Gabriel Genellina

 
Reply With Quote
 
 
 
 
Patrick Mullen
Guest
Posts: n/a
 
      03-25-2008
It seems like this is mostly a non-issue. The original code actually
works correctly (of course the updated versions that solve the
exception problem are probably better). The only thing that is going
haywire is the interpreter shutdown process. I think it is going a
bit overboard to "consider __del__ harmful" because it might throw an
exception as the program is quitting. It is important to not rely on
__del__ too much but there can be some designs where you need to know
when something is gone, and you don't know exactly where or when
something is deleted.

Then again, I can count the number of times I have ever needed __del__
with no fingers (never used it!). Still, quite interesting to
explore.
 
Reply With Quote
 
Edward A. Falk
Guest
Posts: n/a
 
      03-25-2008
In article <(E-Mail Removed)>,
Patrick Mullen <(E-Mail Removed)> wrote:

>Then again, I can count the number of times I have ever needed __del__
>with no fingers (never used it!). Still, quite interesting to
>explore.


I used it once, for an object that had a doubly-linked list.
The __del__() method walked the list, setting all the elements'
prev/next pointers to None to make sure the elements of the list would
get garbage-collected.

--
-Ed Falk, (E-Mail Removed)
http://thespamdiaries.blogspot.com/
 
Reply With Quote
 
Marc 'BlackJack' Rintsch
Guest
Posts: n/a
 
      03-25-2008
On Tue, 25 Mar 2008 14:58:51 +0000, Edward A. Falk wrote:

> In article <(E-Mail Removed)>,
> Patrick Mullen <(E-Mail Removed)> wrote:
>
>>Then again, I can count the number of times I have ever needed __del__
>>with no fingers (never used it!). Still, quite interesting to
>>explore.

>
> I used it once, for an object that had a doubly-linked list.
> The __del__() method walked the list, setting all the elements'
> prev/next pointers to None to make sure the elements of the list would
> get garbage-collected.


Without the `__del__()` the elements would get garbage collected just
fine. If you don't want to wait until the garbage collector in CPython
detects the cycle, you can use `weakref`\s for one of the two "pointers"
in each element.

Ciao,
Marc 'BlackJack' Rintsch
 
Reply With Quote
 
Hrvoje Niksic
Guest
Posts: n/a
 
      03-25-2008
Marc 'BlackJack' Rintsch <(E-Mail Removed)> writes:

> On Tue, 25 Mar 2008 14:58:51 +0000, Edward A. Falk wrote:
>
>> In article <(E-Mail Removed)>,
>> Patrick Mullen <(E-Mail Removed)> wrote:
>>
>>>Then again, I can count the number of times I have ever needed __del__
>>>with no fingers (never used it!). Still, quite interesting to
>>>explore.

>>
>> I used it once, for an object that had a doubly-linked list.
>> The __del__() method walked the list, setting all the elements'
>> prev/next pointers to None to make sure the elements of the list would
>> get garbage-collected.

>
> Without the `__del__()` the elements would get garbage collected just
> fine.


It gets even worse than that: the __del__ method actually *prevents*
objects that participate in cycles from getting garbage-collected.
Python's GC doesn't deallocate objects that define __del__. (The way
to handle those is to grep for them in gc.garbage, break the cycles in
a way that works for the application -- e.g. by removing the prev and
next references -- and finally del gc.garbage[:].)
 
Reply With Quote
 
castironpi@gmail.com
Guest
Posts: n/a
 
      03-25-2008
On Mar 23, 8:05*pm, Tim Chase <(E-Mail Removed)> wrote:
> > When I run this script, I got the following exception:
> > Exception exceptions.AttributeError: "'NoneType' object has no
> > attribute 'population'" in <bound method Person.__del__ of
> > <__main__.Person instance at 0xb7d8ac6c>> ignored

>
> > To to newcomer like me, this message doesn't make much sense. What
> > seems weird to me is that, if I change the variable cathy to something
> > else, like cath, or even cat, then the script will finish gracefully.
> > Why "cathy" is not liked?!!

>
> > My python is of version 2.5.1, on Ubuntu.

>
> When I first read this, I thought you were crazy. *I ran the
> code, and you're not. *I tried under versions 2.3, 2.4, and 2.5
> on my Debian box, and got the same results. *Neither "cathy" nor
> "ca" works, *but "c", "cat", "cath", "cath_y" and "cathy_" all
> work just fine.
>
> Even more infuriating is that if I try and use pdb to debug,
> Swaroop drops into pdb, but Abdul and Cathy both error out.
> [snip]


> Does Python hate Cathy?


Does Pothon?
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Why does every one hate Microsoft Dana Computer Security 125 03-01-2007 02:50 AM
D-Link DSL 504T - I hate my router - No, I really really hate it!!! Simon Harding Computer Support 7 01-03-2006 09:11 PM
Does Firefox hate Debian or...? lets.monroe@gmail.com HTML 5 10-24-2005 09:43 PM
Who is Art Bell and why does everyone hate him so much? Tony Spadaro Digital Photography 14 06-22-2004 06:25 AM
Re: HENRY POSNER does he hate olives? BandHPhoto Digital Photography 4 08-27-2003 05:21 AM



Advertisments