Whre is my object ??
Erik Max Francis
max at alcyone.com
Tue Jun 3 18:00:04 EDT 2003
King Kuta wrote:
> Ok, a little riddle for he Python community.
>
> Follow this code (interactive):
>
> >>> class marco:
> ... pass
> ...
> >>> objlist[0]=marco
> >>> del marco
You delete the original name.
"C:\PROGRA~1\PYTHON~1.2\Lib\site-packages\wxPython\tools\boa\ExternalLi
> b\Pyt
> honInterpreter.py", line 69, in push
> exec code in self.locals
> File "<console>", line 1, in ?
> '''exceptions.NameError : name 'marco' is not defined'''
So it's not surprising that it's deleted.
> So where is my object? (apart from the obvious mem location at
> 0x023824D8)
It's in objectlist[0], right where you put it. In Python, variables
aren't really unique objects themselves, they're just bindings (def and
class statements really just construct the relevant function/class
objects and bind them to the provided name; there's nothing inherently
special about function/class names). The object is the thing that's
unique, not the variable.
>>> a = object() # just some unique object, doesn't matter what
>>> a
<object object at 0x81175b8>
>>> b = a # now a and b are both bound to the same thing
>>> del a # now destroy the a binding
>>> b # now b references what was once referenced by a
<object object at 0x81175b8>
>>> a # and a is gone
Traceback (most recent call last):
File "<stdin>", line 1, in ?
NameError: name 'a' is not defined
--
Erik Max Francis && max at alcyone.com && http://www.alcyone.com/max/
__ San Jose, CA, USA && 37 20 N 121 53 W && &tSftDotIotE
/ \ And we are all mortal.
\__/ John F. Kennedy
More information about the Python-list
mailing list