Interesting 'takes exactly 1 argument (2 given)' Python error

前端 未结 6 1868
傲寒
傲寒 2020-12-02 12:19

For the error:

TypeError: takes exactly 1 argument (2 given)

With the following class method:

def extractAll(tag):
   ...
<         


        
6条回答
  •  青春惊慌失措
    2020-12-02 12:45

    Summary (Some examples of how to define methods in classes in python)

    #!/usr/bin/env python   # (if running from bash)
    
    class Class1(object):
    
        def A(self, arg1):
            print arg1
            # this method requires an instance of Class1   
            # can access self.variable_name, and other methods in Class1
    
        @classmethod
        def B(cls, arg1):
            cls.C(arg1)
            # can access methods B and C in Class1 
    
        @staticmethod
        def C(arg1):
            print arg1
            # can access methods B and C in Class1 
            # (i.e. via Class1.B(...) and Class1.C(...))
    

    Example

    my_obj=Class1()
    
    my_obj.A("1")
    # Class1.A("2") # TypeError: method A() must be called with Class1 instance
    
    my_obj.B("3")
    Class1.B("4")
    my_obj.C("5")
    Class1.C("6")`
    

提交回复
热议问题