Getting the name of an instance

Daniel Klein danielk at
Thu Jan 17 14:48:06 CET 2002

You shouldn't have to do this. What problem are you trying to solve?


On Wed, 16 Jan 2002 23:13:51 +0200, "Tal Linzen" <pachy at>

>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.

More information about the Python-list mailing list