Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

python

For the error:

TypeError: takes exactly 1 argument (2 given) 

With the following class method:

def extractAll(tag):    ... 

and calling it:

e.extractAll("th") 

The error seems very odd when I'm giving it 1 argument, the method should take only 1 argument, but it's saying I'm not giving it 1 argument....I know the problem can be fixed by adding self into the method prototype but I wanted to know the reasoning behind the error.

Am I getting it because the act of calling it via e.extractAll("th") also passes in self as an argument? And if so, by removing the self in the call, would I be making it some kind of class method that can be called like Extractor.extractAll("th")?

like image 658
funk-shun Avatar asked Feb 05 '11 20:02

funk-shun


1 Answers

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.

like image 92
Sven Marnach Avatar answered Sep 28 '22 03:09

Sven Marnach