Who am I: can a class instance determine its own name?
Remco Gerlich
scarblac at pino.selwerd.nl
Thu Mar 8 09:52:28 EST 2001
Tim CHURCHES <TCHUR at doh.health.nsw.gov.au> wrote in comp.lang.python:
> 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 = 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)?
An instance may have no name, one name, or many names.
print Foo().whoami() # No name
FooBar = [Foo()]*3 # Name? FooBar[0], FooBar[1] and FooBar[2]...
a = b = c = Foo() # a, b and c
So it's not possible. What do you need it for?
--
Remco Gerlich
More information about the Python-list
mailing list