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

后端 未结 4 1829
遇见更好的自我
遇见更好的自我 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

    From what you're saying, is this along the line of what you're looking for? I'm not sure there is a way to do exactly what you're saying that is "built in"

    class Foo(object):
        def __init__(self, a=None, b=None):
            self.a
            self.b
    
        def Foo(self):
            if self.a is None and self.b is None:
                form = CreationForm()
            else: 
                form = EditingForm()
            return form
    

提交回复
热议问题