TryBugHunt

TypeError: missing 1 required positional argument

TypeError: missing 1 required positional argument in Python means you called a function with fewer values than it needs. The message names the parameter that got nothing. If that name is 'self', you called a method on the class instead of an object.

PythonPattern: Type errors

How do I fix TypeError: missing 1 required positional argument?

TypeError: missing 1 required positional argument means you called a function with fewer values than its def line asks for. The message ends with the parameter that got nothing. Add that value to the call, or give the parameter a default. If the missing name is 'self', create an object first: Dog().bark(), not Dog.bark().

  1. 1. Read the parameter nameThe name at the end of the message, like 'height', is the value your call left out.
  2. 2. Compare def and callCount the parameters in the def line and the values in your call. Add the missing one.
  3. 3. Check for selfIf it says 'self', you called the method on the class. Write d = Dog() with brackets, then d.bark().

What does TypeError: missing 1 required positional argument mean?

A function defined as def area(width, height) needs two values every time. Call it with one and Python stops, telling you which parameter is missing, for example "area() missing 1 required positional argument: 'height'". When the missing one is self, it means the method was called on the class (Dog.bark()) rather than on an object made from it (Dog().bark()).

Why does TypeError: missing 1 required positional argument happen?

1. Forgetting an argument in the call

Each parameter without a default needs a value.

Breaks

def area(width, height):
    return width * height

print(area(5))

Works

def area(width, height):
    return width * height

print(area(5, 3))

2. Calling a method on the class, not an object

self is filled in automatically only when you call the method on an object.

Breaks

class Dog:
    def bark(self):
        print("Woof")

Dog.bark()

Works

class Dog:
    def bark(self):
        print("Woof")

Dog().bark()

3. Forgetting the brackets when creating an object

d = Dog stores the class itself. d.bark() is then the same as Dog.bark().

Breaks

d = Dog
d.bark()

Works

d = Dog()
d.bark()

4. A parameter that should be optional

If callers often leave it out, give it a default value.

Breaks

def greet(name, greeting):
    return greeting + ", " + name

greet("Ada")

Works

def greet(name, greeting="Hello"):
    return greeting + ", " + name

greet("Ada")

How to find it in your own code

Checklist: 1) Read the parameter name at the end of the message. 2) Open the def line and count its parameters. 3) Count the values in your call; add the missing one. 4) If the missing name is self, make sure you created an object with ClassName() and call the method on that object.

Practise type errors

None of the challenges raises this exact error yet. These practise the same kind of bug.

Still not sure why yours breaks?

Paste it into the visualizer and watch it run line by line, with every variable at every step. Free, and it runs in your browser.

Related error guides

View all type errors →