Who am I: can a class instance determine its own name?

Gregory Jorgensen gregj at pobox.com
Thu Mar 8 02:17:17 EST 2001


Class instances don't have names. Names are bound to class instances. Think
about this:

x = foo()
y = x

x and y are both bound to the same foo instance. What is the name of the
instance of foo that both x and y are bound to? The question doesn't make sense.
Not every reference has a name bound to it, an object can have different names
in different scopes, etc.


In article <mailman.984034150.9635.python-list at python.org>, Tim CHURCHES says...
>
>This is probably an elementary question and the answer is probably writ =
>large in multiple places in the Python documentation, but...
>
>...can an instance of a class determine the name of the variable to which =
>it is assigned? For example:
>
>###########################
>class Foo:
>    def whoami(self):
>         return "You are a Foo() but I do not know your name"
>
>FooBar =3D Foo()
>
>print FooBar.whoami()
>###########################
>
>How does one define the method whoami() so that it returns "FooBar"? This =
>sort of navel gazing is formally called introspection, I think (therefore =
>I am)?

Greg Jorgensen
Deschooling Society
Portland, Oregon, USA
gregj at pobox.com



More information about the Python-list mailing list