Editor

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

Broken
def greet(name):
print("Hello, " + name + "!")
greet()
Output
Traceback (most recent call last):
  File "<exec>", line 4, in <module>
TypeError: greet() missing 1 required positional argument: 'name'

The fix

Fixed
def greet(name):
print("Hello, " + name + "!")
greet("Ada")
Output
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"):.

Try fixing it yourself in the editor

2. Creating an object without the values `__init__` needs

This raises the error

Broken
class Dog:
def __init__(self, name):
self.name = name
rex = Dog()
print(rex.name)
Output
Traceback (most recent call last):
  File "<exec>", line 5, in <module>
TypeError: Dog.__init__() missing 1 required positional argument: 'name'

The fix

Fixed
class Dog:
def __init__(self, name):
self.name = name
rex = Dog("Rex")
print(rex.name)
Output
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.

Try fixing it yourself in the editor

3. Calling a method on the class instead of an object

This raises the error

Broken
class Dog:
def bark(self):
print("Woof!")
Dog.bark()
Output
Traceback (most recent call last):
  File "<exec>", line 5, in <module>
TypeError: Dog.bark() missing 1 required positional argument: 'self'

The fix

Fixed
class Dog:
def bark(self):
print("Woof!")
rex = Dog()
rex.bark()
Output
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'.

Try fixing it yourself in the editor

When you see this error, check

  • Compare the call with the def line: 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.

All Python errors · How to read a traceback · Step through your code