Getting the name of an instance

Mark McEahern marklists at mceahern.com
Wed Jan 16 17:42:32 EST 2002


Short answer:

What would you expect in the following scenario?

  p = MyClass()
  q = p
  p = None
  func(q)

I'd be curious to see an example of how you'd use this "getting the name of
an instance."

By the way, this topic seems to come up every once in a while.  Here's the
latest regurgitation:

http://groups.google.com/groups?hl=en&threadm=mailman.1009999443.29330.pytho
n-list%40python.org&rnum=21&prev=/groups%3Fq%3Dgroup:comp.lang.python%2Bauth
or:mceahern%26start%3D20%26hl%3Den%26selm%3Dmailman.1009999443.29330.python-
list%2540python.org%26rnum%3D21

[watch for wrapping]

Cheers,

// mark

> -----Original Message-----
> From: python-list-admin at python.org
> [mailto:python-list-admin at python.org]On Behalf Of Tal Linzen
> Sent: Wednesday, January 16, 2002 1:14 PM
> To: python-list at python.org
> Subject: Getting the name of an instance
>
>
> Hi.
>
> Suppose I have:
> p = MyClass()
>
> I'm looking for a function that works like:
> func(p) = 'p'
> (meaning, retrieves the actual name of the variable).
>
> The best I could come up with is:
> def func(p):
>     return globals().keys()[globals().values().index(p)]
>
> Surely there's a simpler way, something like the __name__
> method in module objects.
>
>
> --
> http://mail.python.org/mailman/listinfo/python-list
>
>





More information about the Python-list mailing list