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?
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.
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.
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.
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.
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.
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).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With