TypeError: method () takes 1 positional argument, but 2 are given

If I have a class ...

class MyClass: def method(arg): print(arg) 

... which I use to create an object ...

 my_object = MyClass() 

... on which I call method("foo") like that ...

 >>> my_object.method("foo") Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: method() takes exactly 1 positional argument (2 given) 

... why does Python tell me that I gave it two arguments when I only gave one?

+196
python methods arguments self
May 29 '14 at 23:27
source share
6 answers

In Python, this is:

 my_object.method("foo") 

... is syntactic sugar that the interpreter translates behind the scenes in:

 MyClass.method(my_object, "foo") 

... which, as you can see, really has two arguments - it's just that the first one is implicit from the perspective of the caller.

This is due to the fact that most methods do some work with the object they are called on, so there must be some way for this object to be specified inside the method. By convention, this first argument is called self inside the method definition:

 class MyNewClass: def method(self, arg): print(self) print(arg) 

If you call method("foo") on an instance of MyNewClass , it works as expected:

 >>> my_new_object = MyNewClass() >>> my_new_object.method("foo") <__main__.MyNewClass object at 0x29045d0> foo 

Sometimes (but not often) you really do not need the object to which your method is attached, in which case you can decorate a method with the built-in staticmethod() function to say this:

 class MyOtherClass: @staticmethod def method(arg): print(arg) 

... in this case, you do not need to add the self argument to the method definition, and it still works:

 >>> my_other_object = MyOtherClass() >>> my_other_object.method("foo") foo 
+273
May 29 '14 at 23:27
source share

Something else needs to be considered when this type of error occurs:

I came across this error message and found this message helpful. It turns out that in my case, I redefined init (), where there was an inheritance of objects.

The inherited example is quite long, so I will move on to a simpler example that does not use inheritance:

 class MyBadInitClass: def ___init__(self, name): self.name = name def name_foo(self, arg): print(self) print(arg) print("My name is", self.name) class MyNewClass: def new_foo(self, arg): print(self) print(arg) my_new_object = MyNewClass() my_new_object.new_foo("NewFoo") my_bad_init_object = MyBadInitClass(name="Test Name") my_bad_init_object.name_foo("name foo") 

Result:

 <__main__.MyNewClass object at 0x033C48D0> NewFoo Traceback (most recent call last): File "C:/Users/Orange/PycharmProjects/Chapter9/bad_init_example.py", line 41, in <module> my_bad_init_object = MyBadInitClass(name="Test Name") TypeError: object() takes no parameters 

PyCharm did not notice this typo. Also Notepad ++ (other editors / IDE might).

Of course, this "takes no parameters" TypeError, it is not much different from "got two" while waiting for one, in terms of initializing the object in Python.

Topic addressing: the overload initializer will be used if it is syntactically correct, but if it is not ignored and the built-in will be used instead. The object will not wait / handle this, and the error will be reset.

In case of a sytax: error, the fix is โ€‹โ€‹simple, just edit the custom init statement:

 def __init__(self, name): self.name = name 
+12
Jan 04 '16 at 4:33
source share

This happens when you do not specify the number of parameters that __init__() or any other method is looking for.

For example:

 class Dog: def __init__(self): print("IN INIT METHOD") def __unicode__(self,): print("IN UNICODE METHOD") def __str__(self): print("IN STR METHOD") obj=Dog("JIMMY",1,2,3,"WOOF") 

When you run the above program, it produces the following error:

TypeError: __init __ () takes 1 positional argument but 6 were given

How can we get rid of this thing?

Just pass parameters what the __init__() method is looking for

 class Dog: def __init__(self, dogname, dob_d, dob_m, dob_y, dogSpeakText): self.name_of_dog = dogname self.date_of_birth = dob_d self.month_of_birth = dob_m self.year_of_birth = dob_y self.sound_it_make = dogSpeakText def __unicode__(self, ): print("IN UNICODE METHOD") def __str__(self): print("IN STR METHOD") obj = Dog("JIMMY", 1, 2, 3, "WOOF") print(id(obj)) 
+7
Feb 03 '17 at 3:45
source share

New to Python, I had this problem when I used the Python ** function incorrectly. Trying to call this definition from somewhere:

 def create_properties_frame(self, parent, **kwargs): 

problem with a call without a double star:

 self.create_properties_frame(frame, kw_gsp) 

TypeError: create_properties_frame () takes 2 positional arguments but 3 were given

The solution is to add ** to the argument:

 self.create_properties_frame(frame, **kw_gsp) 
+3
Jul 28 '19 at 9:43
source share

In simple words.

In Python, you must add the self argument as the first argument to all defined methods in the classes:

 class MyClass: def method(self, arg): print(arg) 

Then you can use your method according to your intuition:

 >>> my_object = MyClass() >>> my_object.method("foo") foo 

This should solve your problem :)

For a better understanding, you can also read the answers to this question: what is the purpose of yourself?

+2
Jun 19 '19 at 9:44
source share

Pass the cls parameter to @classmethod to solve this problem.

 @classmethod def test(cls): return '' 
-2
Jun 10 '18 at 23:52
source share



All Articles