TypeError: __init__() takes 1 positional argument but 3 were given
时间: 2023-10-16 16:16:48 浏览: 154
This error message is raised when you are trying to create an instance of a class with more arguments than the constructor method (__init__()) is expecting.
For example, if you have a class named "Person" that takes only one argument in its constructor method, but you pass three arguments while creating an instance of that class, you will get this error.
Here's an example:
```
class Person:
def __init__(self, name):
self.name = name
person1 = Person("John", 25, "Male")
```
In the above example, the Person class expects only one argument, which is the name of the person. However, while creating an instance of that class, we are passing three arguments - name, age, and gender. Hence, we get the TypeError: __init__() takes 1 positional argument but 3 were given error.
To fix this error, you need to make sure that the number of arguments you pass while creating an instance of a class matches the number of arguments expected by the constructor method.
阅读全文