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

前端 未结 3 919
耶瑟儿~
耶瑟儿~ 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:50

    The way you are trying wont work, because Foo isnt defined yet.

    class Foo:  
      @classmethod
      def func1(cls, x):
        print 'something: ', cls, x
    
    def func2(cls, a_func=Foo.func1):
     a_func('test')
    
    Foo.func2 = classmethod(func2)
    
    Foo.func2()
    

提交回复
热议问题