Can a method be used as either a staticmethod or instance method?

后端 未结 4 1790
遇见更好的自我
遇见更好的自我 2021-01-18 20:08

I\'d like to be able to do this:

class A(object):
    @staticandinstancemethod
    def B(self=None, x, y):
        print self is None and \"static\" or \"ins         


        
4条回答
  •  夕颜
    夕颜 (楼主)
    2021-01-18 20:54

    The answer to your question is no, you can't do that.

    What I would do, since Python also supports regular functions, is define a function outside that class, then call that function from a normal method. The caller can decide what which one is needed.

提交回复
热议问题