Say I have the following class defined with the method foo
:
class MyClass:
def foo(self):
print "My name is %s" % __name__
Now when I call foo()
I expect/want to see this printed out
My name is foo
However I get
My name is __main__
And if I was to put the class definition into a module called FooBar
I would get
My name is FooBar
However if I do
m = MyClass()
print m.foo.__name__
I get exactly what I want which is
My name is foo
Can someone please help explain why __name__
refers to the module and not the method name ?
Is there an easy way to get the method name?
Many thanks