Whre is my object ??

Steven Taschuk staschuk at telusplanet.net
Tue Jun 3 19:57:23 EDT 2003


Quoth King Kuta:
> Ok, a little riddle for he Python community.

The main riddle seems to be to figure out what mental model you're
operating under; many of your specific questions just don't make
sense.  (Details below, after the walk-through of the interactive
session.)

> Follow this code (interactive):
> 
> >>> class marco:
> ...     pass
> ...

Creates an object (a class object, to be specific) and binds the
name 'marco' to that object.

> >>> objlist[0]=marco

Places a reference to that same class object in the first spot in
objlist (which I assume to have been created prior to the snippet
posted).

> >>> del marco

Unbinds the name 'marco', so it no longer refers to anything.  The
class object it used to refer to is still accessible via objlist,
so that object doesn't get destroyed.

  [...]
> >>> marco
  [...]
> '''exceptions.NameError : name 'marco' is not defined'''

Indeed; you unbound this name.

> Evidently "marco" object does not exist in the utermost scope (__builtin__
> ?)
> but it exit "inside" the list object and can be not only referenced but
> assignd too.
> So where is my object? (apart from the obvious mem location at 0x023824D8)
> Does it have a scope in which it exist ?

It's very hard to tell what you want to know.  You seem to think
that scopes are some kind of location in which objects reside; but
this is not so.

Understand first that the name 'marco' and the object to which it
refers at any given moment are different things.

Whether the *object* exists or does not exist depends on whether
Python's garbage collection mechanisms have decided to collect the
object.  This has nothing (directly) to do with scopes.

Whether the *name* is bound or not bound to anything depends on
previous assignment statements, del statements, etc., in scopes
which are relevant to the code at hand.  This has everything to do
with scopes.

Objects don't have locations other than their addresses in memory
(or, in context, their positions in lists and the like).

Also, __builtin__ isn't a scope, it's a module.

> Do list "create" a scope?

No.  A list does, however, hold references to objects, and thereby
keeps the referred-to objects alive.

> Is it another riddle on identifiers?

*Are* there riddles on identifiers?

-- 
Steven Taschuk                          staschuk at telusplanet.net
"Its force is immeasurable.  Even Computer cannot determine it."
                           -- _Space: 1999_ episode "Black Sun"





More information about the Python-list mailing list