When you call a method associated with an object, there is one positional argument that is supplied by default: self. If you forget to include “self” when you define a method, you’ll encounter an error that says “takes 1 positional argument but 2 were given”.
In this article, we discuss this error and why it is raised. We walk through an example of this error to help you figure out how to solve it in your code.
takes 1 positional argument but 2 were given
Python passes an argument called “self” into every method in an object. “self” is similar to “this” in JavaScript. The “self” argument stores information about the values in an object.
“self” is passed into methods by default because most methods rely on the values that have been assigned to an object in some way.
All methods defined in a class must have an argument called “self”. If you specify an argument other than “self” without including “self”, Python will return the “takes 1 positional argument but 2 were given” error.
This is because Python passes “self” into a method by default and so it expects there is room for “self” in addition to any other arguments that a method should receive.
An Example Scenario
Write a class that stores information on television show characters. Start by declaring our class and defining a constructor that stores the values we pass into our class:
class Character: def __init__(self, character_name, real_name, show): self.character_name = character_name self.real_name = real_name self.show = show
Write a function that lets us change the value of “show”:
def change_show(show): self.show = show print(show)
This method changes the value of “show” in our class and prints out the value of “show” to the console. To test out this method, we have to define an instance of our object and call the change_show()
method:
sam_malone = Character("Sam Malone", "Ted Danson", "") sam_malone.change_show("Cheers")
Our “sam_malone” object is initialized with the following values:
- Character Name: Sam Malone
- Real Name: Ted Danson
- Show: [Empty]
We have left the value of “show” empty because we’re going to add it in later. Next, we use the change_show()
method to set the value of “show” to “Cheers”.
Run our code:
Traceback (most recent call last): File "main.py", line 11, in <module> sam_malone.change_show("Cheers") TypeError: change_show() takes 1 positional argument but 2 were given
Our code does not execute successfully.
The Solution
In our code, we have not passed “self” into our change_show()
method.
Python passes “self” as a parameter every time you call a method of an object. In our above code, Python is executing:
sam_malone.change_show("Cheers")
Another way of representing this is:
Character.change_show(sam_malone, "Cheers")
This shows that our object, “sam_malone”, is actually passed into our method. To solve this error, we have to specify “self” as an argument in our class:
def change_show(self, show): self.show = show print(show)
This tells the change_show()
method to expect two arguments: “self” and “show”. “self” is the object on which the method will be executed. “show” is the name of the television show with which a character is associated.
Run our code again with our new function:
Cheers
Our code prints the value of “show” to the console successfully. Every time we reference “show” in our class after we have called change_show()
, the value associated with “show” is “Cheers”.
Conclusion
The “takes 1 positional argument but 2 were given” error is raised when you try to pass an argument through a method in a class without also specifying “self” as an argument.
You solve this error by adding “self” as an argument to all the methods in a class.
Now you’re ready to solve this error in your code like a professional coder!
About us: Career Karma is a platform designed to help job seekers find, research, and connect with job training programs to advance their careers. Learn about the CK publication.
With respect !!!