This question is very simple, and I expect the answer will also be very simple.
Is there a way that I can return the string corresponding to the name I have chosen to bind to an instance of an object? For example, if I have written the following line of code:
tesla = Car(fuel='electric')
I would like some function getname()
that I can call roughly as follows:
[In] tesla.*getname*()
[Out] 'tesla'
getname
need not be called as a method of the object, I am also happy to call it as:
[In] *getname*(tesla)
[Out] 'tesla'
I'm sure there must be some function that accomplishes this goal, but I have yet to find it. All I have found relating to the topic is the __name__
attribute of an object, but this seems to return the name of the class of the object (in this example, 'Car'
), not the name bound to that object in the current environment.