Can a Python instance get the name of the variable which is used to access the object?
This example code shows what I need:
foo=MyClass()
foo.get_name() --> 'foo'
bar=foo
bar.get_name() --> 'bar'
I know that this is black magic and not clean code. I just want to know if it is possible.
I know that bar.__name__
returns the name, but I need it inside an own method.
How can get_name()
be implemented?
This is not a duplicate of questions which answer is __name__