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

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

This is annoying:

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

    @staticmethod
    def bar():
        MyClass.foo()
4条回答
  •  迷失自我
    2021-01-01 11:46

    You 'variable-ize' the class name. This will not remove, but shorten the name.

    class MyClass:
        @staticmethod
        def foo():
            print "hi"
    
        @staticmethod
        def bar():
            c.foo()
    
    c = MyClass
    

提交回复
热议问题