November 29th, 2012, 04:26 AM
Get the name of an instance of a class
is it possible to get the name of an instance of a class?
For example if i do something like that:
B = car(<Parameters>)
I want to appear on console:
"You just created a "car" with the name "B"..."
The first part is pretty easy i just had to create a variable "type"
in the class with a strin containing "car" and then do the print command in the __init__ function, but how can i access the name of the actual object? self.__name__ is not working for me.
Is there a way or is it really impossible? Your help is appreciated
November 29th, 2012, 10:32 AM
Nope. "B" is not the name of your instance in any reasonable sense; it's the name of a variable that refers to your instance. You can assign the same instance to different variables, and they will all refer to the same data:
C = B
D = B
# Now what's the "name"? "B", "C", or "D"? All refer to the same data.
November 29th, 2012, 10:49 AM
You could (if you're in control of the class definition) create an instance attribute, say, "label", which you can pass into the constructor.
November 29th, 2012, 11:14 AM
self . __class__ . __name__
>>> (3.14) . __class__ . __name__
>>> 2 .__class__.__name__
File "<stdin>", line 1
SyntaxError: invalid syntax
[/code] are essential for python code and Makefiles!
November 30th, 2012, 05:03 AM