The error TypeError: got multiple values for argument in Python happens quite commonly for those new to Python class methods. Find out the reason and solution below.
TypeError: got multiple values for argument In Python
Reproduce The Error
Let’s say you want to create a class for Q&A websites with some properties such as the name and URL:
class Site: def __init__(self, name, URL): self.name = name self.URL = URL site1 = Site("ITTutoria", "https://ittutoria.net") print(site1.name) print(site1.URL)
Suppose we want to have a method that can print out a custom message using the name and URL of our site:
def visit(str1, str2): print(str1, self.name, str2, self.URL)
This may look totally normal. But if we invoke the method on an instance of the class, it will end up with a TypeError exception:
site1.visit(str1 = "Please visit us", str2 = "at")
TypeError: Site.visit() got multiple values for argument 'str1'
So, what has gone wrong here? In short, the method results in errors because its declaration lacks the self parameter.
self In Python Classes
As you may already notice, self is a must-have first parameter for methods of Python classes. But what is that?
This might surprise you, but self isn’t a keyword in Python. Developers use it to explicitly represent the instance of the class in methods, including __init__. Without self, Python can’t distinguish between instance variables and other variables.
That said, why do we need to repeat every method of a class? Why can’t we just make a keyword that requires no further declaration?
This proposal did get some attention in the Python community in the past. But Guido van Rossum, Python creator, rejected this idea for various reasons.
Remember that Site.visit and site1.visit are different:
>>> type(Site.visit) <class 'function'> >>> type(site1.visit) <class 'method'>
This means when you access a method using the name of the class, you are actually using a function.
When you invoke a method of an instance (such as site1.visit), what happens under the hood is that it passes itself as the first argument to the Site.visit function. In particular, site1.visit(str1, str2) equals Site.visit(site1, str1, str2). Need to get the class name of an object in Python? Follow this guide.
You don’t need to use the self word since this is just a convention. Other names work too, but this practice is discouraged. Doing so may affect your code’s readability and make it harder to debug and maintain.
When self is omitted, this behavior is no longer applicable. In our case, when we call site1.visit, it will become something like this:
Site.visit(str1 = site1, str1 = "Please visit us", str2 = "at")
There are two values of the str1 argument, which leads to the TypeError exception indicating multiple values for that argument.
Fix The Error
After adding the self parameter to the visit() method, you can invoke it without issues:
class Site: def __init__(self, name, URL): self.name = name self.URL = URL def visit(self, str1, str2): print(str1, self.name, str2, self.URL) site1 = Site("ITTutoria", "https://ittutoria.net") site1.visit(str1 = "Please visit us", str2 = "at")
Please visit us ITTutoria at https://ittutoria.net
The error TypeError: got multiple values for argument in Python occurs when you forget to add the self parameter in a class method. This parameter is required to pass the instance itself to the class function whenever you call the method.