mercado mercado wrote:
In Python, is it possible for an instance method to know the name of the
class which is calling it?
Well, the class name is in __class__ so changing
'print x' to
'print __class__'
will show foo in both cases, which is in fact the name of the class
which is calling it.
Of course, what you want is the name of the thing chained in front of
that. There are and are not ways of getting to that. Dig into the
traceback module.
But I'd pass a parameter to foo's __init__ such as foo('bar') and
foo('again') and it'd always work.
HTH,
Emile
For example, in the sample below, I would
like for the someMethod method to print the name of the class calling it
("bar" in the first case, "again" in the second).
---------------------------------------
class foo():
def someMethod(self):
print x
class bar():
def __init__(self):
f = foo()
f.someMethod()
class again():
def __init__(self):
f = foo()
f.someMethod()
bar()
again()
---------------------------------------
------------------------------------------------------------------------
--
http://mail.python.org/mailman/listinfo/python-list
--
http://mail.python.org/mailman/listinfo/python-list