TypeError: greet() missing 1 required positional argument: 'name'
What it means
You called a function with fewer values than it needs. The message names the function and the parameters that got no value. Every parameter without a default value must receive one in the call.
1. Calling a function without its argument
This raises the error
Traceback (most recent call last): File "<exec>", line 4, in <module> TypeError: greet() missing 1 required positional argument: 'name'
The fix
Hello, Ada!
The call now passes a value for name. If the argument should be optional, give it a default instead: def greet(name="friend"):.
2. Creating an object without the values `__init__` needs
This raises the error
Traceback (most recent call last): File "<exec>", line 5, in <module> TypeError: Dog.__init__() missing 1 required positional argument: 'name'
The fix
Rex
Dog() runs __init__, which needs a name, so the message says Dog.__init__() missing 1 required positional argument: 'name'. Python fills in self for you, but every other parameter needs a value.
3. Calling a method on the class instead of an object
This raises the error
Traceback (most recent call last): File "<exec>", line 5, in <module> TypeError: Dog.bark() missing 1 required positional argument: 'self'
The fix
Woof!
Python passes the object as self only when you call the method on an object. Dog.bark() has no object, so the message says missing 1 required positional argument: 'self'.
When you see this error, check
- Compare the call with the
defline: count the parameters and the values you pass. - Give optional parameters a default value, like
def greet(name="friend"):. - Create an object first, with
Dog(), and call methods on it, not on the class. - If the missing argument is
self, check the method is called on an object, not the class.
Related errors
All Python errors · How to read a traceback · Step through your code