get instance name inside of member function

Steve Holden sholden at
Thu Jan 9 15:30:57 CET 2003

"Ralf Oberländer" <ralf.oberlaender at> wrote in message
news:3E1D869F.2050809 at
> Hi gurus,
> is it possible to get the name of the instance inside a member function:
> class a:
>      def inst_name():
>          pass
> inst=a
I think you probably mean

    inst = a()


> so that
> inst.inst_name()
> results in the string "inst"
> I need it for logging

Define "need". Consider the following code:

    class a:

    inst1 = inst2 = a()

Is the name of the instance "inst1" or "inst2" here? Names are actually
references to values in Python, so values don't actually "have" names - they
"are bound" to names (or names are bound to values: different authors use
ther terminology in slightly different ways).

You would be a lot better off *generating* a name in the __init__ function,
I would have thought, but perhaps you can convince me you really "need" "the
name" of the instance.

Steve Holden                        
Python Web Programming       
Bring your musical instrument to PyCon!

More information about the Python-list mailing list