Interesting takes exactly 1 argument (2 given) Python error

Interesting takes exactly 1 argument (2 given) Python error

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 dont 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 dont need to access any instance.

If a non-static method is member of a class, you have to define it like that:

def Method(self, atributes..)

So, I suppose your e is instance of some class with implemented method that tries to execute and has too much arguments.

Interesting takes exactly 1 argument (2 given) Python error

Am I getting it because the act of calling it via e.extractAll(th) also passes in self as an argument?

Yes, thats precisely it. If you like, the first parameter is the object name, e that you are calling it with.

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)?

Not quite. A classmethod needs the @classmethod decorator, and that accepts the class as the first paramater (usually referenced as cls). The only sort of method that is given no automatic parameter at all is known as a staticmethod, and that again needs a decorator (unsurprisingly, its @staticmethod). A classmethod is used when its an operation that needs to refer to the class itself: perhaps instantiating objects of the class; a staticmethod is used when the code belongs in the class logically, but requires no access to class or instance.

But yes, both staticmethods and classmethods can be called by referencing the classname as you describe: Extractor.extractAll(th).

Leave a Reply

Your email address will not be published. Required fields are marked *