Can I pass class method as and a default argument to another class method

前端 未结 3 953
耶瑟儿~
耶瑟儿~ 2021-01-05 11:38

I want to pass class method as and a default argument to another class method, so that I can reuse the method as a @classmethod:

@classmethod
cl         


        
3条回答
  •  无人及你
    2021-01-05 11:52

    You should write it like this:

    class Foo(object):
        @classmethod
        def func1(cls, x):
            print x
        def func2(self, afunc=None):
            if afunc is None:
                afunc = self.func1
            afunc(4)
    

    Though it would be helpful if you gave a little more info on what you are trying to do. There is probably a more elegant way to do this without classmethods.

提交回复
热议问题