Determining names of instances

rturpin at my-deja.com rturpin at my-deja.com
Mon Jan 15 18:20:30 EST 2001


In article <e7L86.554$KD3.238565 at typhoon.aracnet.com>,
  "Daniel Klein" <DanielK at jBASE.com> wrote:
> The question is, is there a way for the object 'foo'
> to know that its name is 'foo'?

Sure. Search the namespace dictionaries for which names
are bound to the the object. When it comes to introspection
few languages beat Python. But (a) this is inefficient,
(b) it is questionable programming, likely a mistake
unless you are writing special debug code, or playing
games with the implementation, (c) other names may be
bound to the same object, including names in currently
inactive namespaces, and (d) the binding changes. Consider
the following code:

  global bar
  foo = Foo()
  bar = foo
  return

Afer the assignment to "bar", the names "foo" and "bar"
reference exactly the same object. Which name do you want?
After the return, the binding to "foo" disappears, while
the binding to "bar" remains.

Russell



Sent via Deja.com
http://www.deja.com/



More information about the Python-list mailing list