Method to give name of class instance?

Daniel Klein danielk at aracnet.com
Sun Apr 29 21:11:12 EDT 2001


On Mon, 30 Apr 2001 00:22:53 GMT, timothyr at timothyr.com (Timothy L. Robertson)
wrote:

>Hi Everyone,
>
>I've been enjoying learning Python very much, but have hit a little
>snag.  I want a way to find out the name of a class instance from a
>method.  eg.
>
>>>>classinstance=MyClass()
>>>>classinstance.myname()
>'classinstance'
>
>Is there a way to do this?

If you want it to have a name, you're going to have to give it one yourself...

class MyClass:
    def __init__(self, myname):
        self.myname = myname

>>>classinstance=MyClass('classinstance')
>>>classinstance.myname()
>>>'classinstance'

Daniel Klein
Portland, OR USA



More information about the Python-list mailing list