Login | Register For Free | Help
Search for: (Advanced)

Mailing List Archive: Python: Python

Can "self" crush itself?

 

 

Python python RSS feed   Index | Next | Previous | View Threaded


n00m at narod

Nov 24, 2009, 8:57 PM

Post #1 of 19 (1034 views)
Permalink
Can "self" crush itself?

Why does "h" instance stay alive?

class Moo:
cnt = 0
def __init__(self, x):
self.x = x
self.__class__.cnt += 1
if self.__class__.cnt > 2:
self.crush_me()
def crush_me(self):
print 'Will self be crushed?'
self = None

f = Moo(1)
g = Moo(2)
h = Moo(3)

print f
print g
print h


=============== RESTART ====

Will self be crushed?
<__main__.Moo instance at 0x00CC9260>
<__main__.Moo instance at 0x00CC9468>
<__main__.Moo instance at 0x00CC94B8>
--
http://mail.python.org/mailman/listinfo/python-list


ben+python at benfinney

Nov 24, 2009, 9:12 PM

Post #2 of 19 (1007 views)
Permalink
Re: Can "self" crush itself? [In reply to]

n00m <n00m [at] narod> writes:

> def crush_me(self):
> print 'Will self be crushed?'
> self = None

As with any function, the parameter is bound to a *local* name, in this
case the name ‘self’. Whatever you rebind ‘self’ to inside the function,
the binding is lost once the function exits. None of this affects any
other bindings the same object might retain from outside the function.

It's exactly the same behaviour as this:

>>> def frobnicate(foo):
... print "Entered ‘frobnicate’"
... foo = None
... print "Leaving ‘frobnicate’"
...
>>> bar = "syzygy"
>>> print bar
syzygy
>>> frobnicate(bar)
Entered ‘frobnicate’
Leaving ‘frobnicate’
>>> print bar
syzygy

The only difference with an instance method is how Python determines
what object to bind to the local name ‘self’. That still doesn't change
the fact that it's a local name inside that function.

--
\ “Read not to contradict and confute, nor to believe and take |
`\ for granted … but to weigh and consider.” —Francis Bacon |
_o__) |
Ben Finney
--
http://mail.python.org/mailman/listinfo/python-list


n00m at narod

Nov 24, 2009, 9:45 PM

Post #3 of 19 (1005 views)
Permalink
Re: Can "self" crush itself? [In reply to]

> Whatever you rebind self to inside the function...


Seems you are right! Thanks, Ben, for the lesson :-)


class Moo:
cnt = 0
def __init__(self, x):
self.x = x
self.__class__.cnt += 1
if self.__class__.cnt > 2:
self.crush_me()
def crush_me(self):
print 'Will self be crushed?'
self = None
print self

f = Moo(1)
g = Moo(2)
h = Moo(3)
print '================='
print h



Will self be crushed?
None
=================
<__main__.Moo instance at 0x00CC9468>
--
http://mail.python.org/mailman/listinfo/python-list


n00m at narod

Nov 25, 2009, 1:46 AM

Post #4 of 19 (1003 views)
Permalink
Re: Can "self" crush itself? [In reply to]

Then how can we destroy the 3rd instance,
right after its creation and from inside
class Moo code?

class Moo:
cnt = 0
def __init__(self, x):
self.x = x
self.__class__.cnt += 1
if self.__class__.cnt > 2:
print id(self)
## 13406816
## in what dict is this ID?
## and can we delete it from there?

## ???


f = Moo(1)
g = Moo(2)
h = Moo(3)
print h

--
http://mail.python.org/mailman/listinfo/python-list


clp2 at rebertia

Nov 25, 2009, 1:58 AM

Post #5 of 19 (1003 views)
Permalink
Re: Can "self" crush itself? [In reply to]

On Wed, Nov 25, 2009 at 1:46 AM, n00m <n00m [at] narod> wrote:
> Then how can we destroy the 3rd instance,
> right after its creation and from inside
> class Moo code?

Why would you want to do that in the first place? It's strange to say the least.
If you want to prevent an instance being created in the first place,
you can override __new__().

Cheers,
Chris
--
http://blog.rebertia.com
--
http://mail.python.org/mailman/listinfo/python-list


ben+python at benfinney

Nov 25, 2009, 2:04 AM

Post #6 of 19 (1001 views)
Permalink
Re: Can "self" crush itself? [In reply to]

n00m <n00m [at] narod> writes:

> Then how can we destroy the 3rd instance, right after its creation and
> from inside class Moo code?

Normally, one binds whatever references one needs, and lets the garbage
collector clean them up once they fall out of scope. If the references
are living beyond their usefulness, that's probably a sign that your
code isn't modular enough; short, focussed functions might help. But
this is all diagnosis without seeing the symptoms.

Perhaps it's beyond time that you explained what you're trying to
achieve that you think “destroy an instance” will help.

--
\ “I bought a self learning record to learn Spanish. I turned it |
`\ on and went to sleep; the record got stuck. The next day I |
_o__) could only stutter in Spanish.” —Steven Wright |
Ben Finney
--
http://mail.python.org/mailman/listinfo/python-list


n00m at narod

Nov 25, 2009, 3:42 AM

Post #7 of 19 (902 views)
Permalink
Re: Can "self" crush itself? [In reply to]

> Why would you want to do that in the first place?

I don't know... :-)
As Schoepenhauer put it:
The man can do what he wants to do but he can't want to want
what he wants to do

--
http://mail.python.org/mailman/listinfo/python-list


aahz at pythoncraft

Nov 25, 2009, 1:38 PM

Post #8 of 19 (996 views)
Permalink
Re: Can "self" crush itself? [In reply to]

In article <mailman.965.1259143133.2873.python-list [at] python>,
Chris Rebert <clp2 [at] rebertia> wrote:
>
>If you want to prevent an instance being created in the first place,
>you can override __new__().

Or just raise an exception in __init__(), which I think is more common
practice.
--
Aahz (aahz [at] pythoncraft) <*> http://www.pythoncraft.com/

The best way to get information on Usenet is not to ask a question, but
to post the wrong information.
--
http://mail.python.org/mailman/listinfo/python-list


n00m at narod

Nov 25, 2009, 4:56 PM

Post #9 of 19 (994 views)
Permalink
Re: Can "self" crush itself? [In reply to]

> Or just raise an exception in __init__(),..

Then we are forced to handle this exception outside of class code.
It's Ok. Never mind.
--------------------

Next thing.
I can't understand why we can get __name__, but not __dict__,
on the module level?


print __name__
print __dict__


>>> ===================================== RESTART ====
>>>
__main__

Traceback (most recent call last):
File "D:\Python25\zewrt.py", line 19, in <module>
print __dict__
NameError: name '__dict__' is not defined

--
http://mail.python.org/mailman/listinfo/python-list


tjreedy at udel

Nov 25, 2009, 5:09 PM

Post #10 of 19 (992 views)
Permalink
Re: Can "self" crush itself? [In reply to]

n00m wrote:
>> Or just raise an exception in __init__(),..
>
> Then we are forced to handle this exception outside of class code.
> It's Ok. Never mind.
> --------------------
>
> Next thing.
> I can't understand why we can get __name__, but not __dict__,
> on the module level?
>
>
> print __name__
> print __dict__

If the global namespace contained itself, as a dict, there would be an
infinite loop.

--
http://mail.python.org/mailman/listinfo/python-list


steven at REMOVE

Nov 25, 2009, 6:06 PM

Post #11 of 19 (992 views)
Permalink
Re: Can "self" crush itself? [In reply to]

On Wed, 25 Nov 2009 20:09:25 -0500, Terry Reedy wrote:

> n00m wrote:
>>> Or just raise an exception in __init__(),..
>>
>> Then we are forced to handle this exception outside of class code. It's
>> Ok. Never mind.
>> --------------------
>>
>> Next thing.
>> I can't understand why we can get __name__, but not __dict__, on the
>> module level?
>>
>>
>> print __name__
>> print __dict__
>
> If the global namespace contained itself, as a dict, there would be an
> infinite loop.


Why would that be a problem? Any time you do this:

>>> g = globals()


you create such a recursive reference:

>>> globals()['g']['g']['g']['g'] is globals() is g
True


Yes, there's a tiny bit extra work needed when bootstrapping the
processes, and when exiting, but I don't see why it's a big deal. Whether
it's necessary or useful is another story.



--
Steven
--
http://mail.python.org/mailman/listinfo/python-list


n00m at narod

Nov 25, 2009, 6:39 PM

Post #12 of 19 (989 views)
Permalink
Re: Can "self" crush itself? [In reply to]

aaah... globals()...
Then why "self" not in globals()?

class Moo:
cnt = 0
def __init__(self, x):
self.__class__.cnt += 1
if self.__class__.cnt < 3:
self.x = x
else:
print id(self)
for item in globals().items():
print item

f = Moo(1)
g = Moo(2)
h = Moo(3)


>>> ===================================== RESTART ====
>>>
13407336
('g', <__main__.Moo instance at 0x00CC9260>)
('f', <__main__.Moo instance at 0x00CC9440>)
('__builtins__', <module '__builtin__' (built-in)>)
('Moo', <class __main__.Moo at 0x00CCC060>)
('__name__', '__main__')
('__doc__', None)
>>>
--
http://mail.python.org/mailman/listinfo/python-list


steven at REMOVE

Nov 25, 2009, 7:33 PM

Post #13 of 19 (989 views)
Permalink
Re: Can "self" crush itself? [In reply to]

On Wed, 25 Nov 2009 18:39:09 -0800, n00m wrote:

> aaah... globals()...
> Then why "self" not in globals()?
>
> class Moo:
> cnt = 0
> def __init__(self, x):
> self.__class__.cnt += 1


Because it isn't a global, it's a local -- it is defined inside a class.
Inside functions and classes, names you create are local, not global,
unless you declare them global.




--
Steven
--
http://mail.python.org/mailman/listinfo/python-list


mwilson at the-wire

Nov 25, 2009, 7:36 PM

Post #14 of 19 (989 views)
Permalink
Re: Can "self" crush itself? [In reply to]

n00m wrote:

>
> aaah... globals()...
> Then why "self" not in globals()?
>
> class Moo:
> cnt = 0
> def __init__(self, x):
> self.__class__.cnt += 1
> if self.__class__.cnt < 3:
> self.x = x
> else:
> print id(self)
> for item in globals().items():
> print item
>
> f = Moo(1)
> g = Moo(2)
> h = Moo(3)

Because self is not in globals().

It's defined as a local symbol in Moo.__init__ , supplied to that function
as the first parameter.

Mel.


--
http://mail.python.org/mailman/listinfo/python-list


greg.ewing at canterbury

Nov 25, 2009, 11:04 PM

Post #15 of 19 (988 views)
Permalink
Re: Can "self" crush itself? [In reply to]

n00m wrote:

> I can't understand why we can get __name__, but not __dict__,
> on the module level?

For much the same reason that you can see your own
feet but (unless you look in a mirror) you can't
see your own eyes.

--
Greg
--
http://mail.python.org/mailman/listinfo/python-list


n00m at narod

Nov 26, 2009, 12:43 AM

Post #16 of 19 (902 views)
Permalink
Re: Can "self" crush itself? [In reply to]

Ok ok
Of course, it's a local name; -- just my silly slip.
And seems it belongs to no dict[]...
Just an internal volatile elf
--
http://mail.python.org/mailman/listinfo/python-list


f.guerrieri at gmail

Nov 26, 2009, 2:06 AM

Post #17 of 19 (985 views)
Permalink
Re: Can "self" crush itself? [In reply to]

On Thu, Nov 26, 2009 at 8:04 AM, Gregory Ewing
<greg.ewing [at] canterbury>wrote:

> n00m wrote:
>
> I can't understand why we can get __name__, but not __dict__,
>> on the module level?
>>
>
> For much the same reason that you can see your own
> feet but (unless you look in a mirror) you can't
> see your own eyes.
>


+1 QOTW

Francesco


lie.1296 at gmail

Nov 26, 2009, 4:12 AM

Post #18 of 19 (983 views)
Permalink
Re: Can "self" crush itself? [In reply to]

n00m wrote:
> Ok ok
> Of course, it's a local name; -- just my silly slip.
> And seems it belongs to no dict[]...
> Just an internal volatile elf

Local names are not implemented as dict, but rather as sort of an array
in the compiler. The name resolution of locals is compile time and
doesn't use dictionary, that's why local is much faster than globals.
--
http://mail.python.org/mailman/listinfo/python-list


aahz at pythoncraft

Nov 26, 2009, 8:47 AM

Post #19 of 19 (974 views)
Permalink
Re: Can "self" crush itself? [In reply to]

In article <7ace3de0-4588-4b7d-9848-d97298717597 [at] z41g2000yqz>,
n00m <n00m [at] narod> wrote:
>> Or just raise an exception in __init__(),..
>
>Then we are forced to handle this exception outside of class code.

Absolutely! That's the whole point. If you can't construct the class,
you *should* raise an error. The only other workable option is to leave
the target set to None, which is uglier because you don't track the
error.
--
Aahz (aahz [at] pythoncraft) <*> http://www.pythoncraft.com/

The best way to get information on Usenet is not to ask a question, but
to post the wrong information.
--
http://mail.python.org/mailman/listinfo/python-list

Python python RSS feed   Index | Next | Previous | View Threaded
 
 


Interested in having your list archived? Contact Gossamer Threads
 
  Web Applications & Managed Hosting Powered by Gossamer Threads Inc.