I've always wondered if when you define a variable, you can somehow get the name you defined it with, and manipulate it as a string. I am trying to make some code more efficient in doing this.
I looked this up and didn't seem to hit the spot with what I found. I found some code that can retrieve the name of the class itself, but not the name of the created object.
class A:
@classmethod
def getname(cls):
return cls.__name__
def usename(self):
return self.getname()
>> example = A()
>> print (example.usename())
A
I thought I would get a result looking like this:
example
It being literally the name of the object, not the class.
Is there a way of doing this? If there is, I would really appreciate it.
PS: if this question turns out to be a duplicate, sorry, would love it if ya'll point me to the answer :) and not delete this.