get instance name inside of member function

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


"Ralf Oberländer" <ralf.oberlaender at emsys.de> wrote in message
news:3E1D869F.2050809 at emsys.de...
> 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()

right?

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

Define "need". Consider the following code:

    class a:
        pass

    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.

regards
-----------------------------------------------------------------------
Steve Holden                                  http://www.holdenweb.com/
Python Web Programming                 http://pydish.holdenweb.com/pwp/
Bring your musical instrument to PyCon!    http://www.python.org/pycon/
-----------------------------------------------------------------------







More information about the Python-list mailing list