Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

class method with no arguments produces TypeError

Tags:

python

This code:

class testclass:
    def __init__(self,x,y):
        self.x = x
        self.y = y
        self.test()

    def test():
        print('test')

if __name__ == '__main__':
    x = testclass(2,3)

yields:

Error:
TypeError:test() takes no argument(1 given)

I'm calling the test function without any parameter, why does the error say that I have given one?

like image 439
user1050619 Avatar asked Sep 28 '12 02:09

user1050619


People also ask

How to fix “class takes no arguments (1 given) ” – TypeError?

This is a post to explain How To Fix Python TypeError – “Class Takes No Arguments (1 Given)”. This error occurs due to incorrect use of self parameter. The errors happens because the func () is a method within a Class. As such the first argument is expected to be a “self” parameter.

How do I fix object takes no arguments in Python?

Python TypeError: object () takes no arguments Solution. The arguments a class object accepts are passed through a function called __init__ (). If you misspell this function in your class declaration, you’ll encounter a “TypeError: object () takes no arguments” error when you run your code.

What causes “object takes no arguments” error?

The “TypeError: object () takes no arguments” error can also be caused by improper indentation. If you have spelled the __init__ method correctly, check to make sure you use consistent spaces and tabs in your class. 81% of participants stated they felt more confident about their tech job prospects after attending a bootcamp.

Why method X in Class Y cannot be applied to given types?

Whenever a method invocation doesn’t match the corresponding method signature, the method X in class Y cannot be applied to given types error is raised. This error is triggered by a method call attempted with the wrong number, type, and/or order of arguments. Arguments are the actual data values passed to the method’s parameters during invocation.


2 Answers

You call the methods as self.test(). You should mentally translate that to test(self) to find out how the call will be "received" in the function's definition. Your definition of test however is simply def test(), which has no place for the self to go, so you get the error you observed.

Why is this the case? Because Python can only look up attributes when specifically given an object to look in (and looking up attributes includes method calls). So in order for the method to do anything that depends on which object it was invoked on, it needs to receive that object somehow. The mechanism for receiving it is for it to be the first argument.

It is possible to tell Python that test doesn't actually need self at all, using the staticmethod decorator. In that case Python knows the method doesn't need self, so it doesn't try to add it in as the first argument. So either of the following definitions for test will fix your problem:

def test(self):
    print('test')

OR:

@staticmethod
def test():
    print('test')

Note that this is only to do with methods invoked on objects (which always looks like some_object.some_method(...)). Normal function invocation (looking like function(...)) has nothing "left of the dot", so there is no self, so it won't be automatically passed.

like image 103
Ben Avatar answered Sep 28 '22 17:09

Ben


Pass self to your test method:

def test(self):
    print('test')

You need to do this because Python explicitly passes a parameter referring to the instantiated object as the first parameter. It shouldn't be omitted, even if there are no arguments to the method (because of the error specified).

like image 27
Waleed Khan Avatar answered Sep 28 '22 19:09

Waleed Khan