It seems like something
is an object of a custom class already (if this is a built-in class, you can subclass it). Just add an instance attribute name
, assign it when creating and use later:
In [1]: class MyClass:
...: def __init__(self, name):
...: self.name = name
...: def function(self):
...: print 'function called on', self.name
...:
In [2]: myVar = MyClass('Joe')
In [3]: myVar2 = MyClass('Ben')
In [4]: myVar.function()
function called on Joe
In [5]: myVar2.function()
function called on Ben
P.S. I know this is not exactly what you are asking, but I think this is better than actually trying to use the variable name. The question mentioned in the comment above has a very nice answer explaining why.