Python: possible to call static method from within class without qualifying the name

前端 未结 4 1241
伪装坚强ぢ
伪装坚强ぢ 2021-01-01 11:07

This is annoying:

class MyClass:
    @staticmethod
    def foo():
        print \"hi\"

    @staticmethod
    def bar():
        MyClass.foo()
4条回答
  •  慢半拍i
    慢半拍i (楼主)
    2021-01-01 12:05

    Not possible. It is a question of language design. Compare that to C++, where both this (the same as Python self; in Python you have to write self.var, in C++ you may write just var, not this->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.

提交回复
热议问题