This is annoying:
class MyClass:
@staticmethod
def foo():
print "hi"
@staticmethod
def bar():
MyClass.foo()
Is there a way to make this work without naming MyClass in the call? i.e. so I can just say foo()
on the last line?
There is no way to use
foo
and get what you want. There is no implicit class scope, sofoo
is either a local or a global, neither of which you want.You might find classmethods more useful:
This way, at least you don't have to repeat the name of the class.
Not possible. It is a question of language design. Compare that to C++, where both
this
(the same as Pythonself
; in Python you have to writeself.var
, in C++ you may write justvar
, notthis->var
) and own class are used by default in member functions, and you will probably see that sometimes that's good and sometimes that's annoying. The only thing possible is to get used to that feature.You can do something hacky by making a module level function
foo
and then adding it to the class namespace withstaticmethod
: