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

Steve Holden sholden at holdenweb.com
Thu Mar 8 03:53:25 EST 2001


This question recurs so frequently I would appreciate knowing of any
omprovement which could be made to FAQ entry 4.97: How can my code discover
the name of an object?

This tries to address the question in a helpful manner.

still-wishing-i-was-at-the-conference-ly y'rs  - steve

"Tim CHURCHES" <TCHUR at doh.health.nsw.gov.au> wrote in message
news:mailman.984034150.9635.python-list at python.org...
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)?

Tim Churches
Sydney, Australia
(where, due to the Coriolis effect, the Python prompt does indeed look like
this: <<< - or maybe its because we are upside-down)










More information about the Python-list mailing list