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

前端 未结 6 1870
傲寒
傲寒 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:46

    The call

    e.extractAll("th")
    

    for a regular method extractAll() is indeed equivalent to

    Extractor.extractAll(e, "th")
    

    These two calls are treated the same in all regards, including the error messages you get.

    If you don't need to pass the instance to a method, you can use a staticmethod:

    @staticmethod
    def extractAll(tag):
        ...
    

    which can be called as e.extractAll("th"). But I wonder why this is a method on a class at all if you don't need to access any instance.

提交回复
热议问题