Python
TypeError
programming error
self argument
object-oriented programming

Why do I get TypeError Missing 1 required positional argument 'self'?

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Introduction

The Python error TypeError: missing 1 required positional argument: 'self' occurs when you call an instance method on the class itself instead of on an instance. For example, MyClass.method() instead of MyClass().method(). Python instance methods expect the first argument to be the object instance (conventionally named self), which is automatically passed when calling the method on an instance. Calling it on the class directly skips this automatic binding, causing the error.

The Error

python
1class Dog:
2    def __init__(self, name):
3        self.name = name
4
5    def bark(self):
6        return f"{self.name} says Woof!"
7
8# WRONG — calling on the class, not an instance
9Dog.bark()
10# TypeError: Dog.bark() missing 1 required positional argument: 'self'
11
12# CORRECT — calling on an instance
13dog = Dog("Rex")
14dog.bark()  # "Rex says Woof!"

When you call dog.bark(), Python translates it to Dog.bark(dog) behind the scenes. Calling Dog.bark() passes no arguments, so self is missing.

Common Cause 1: Forgetting to Create an Instance

python
1class Calculator:
2    def __init__(self):
3        self.result = 0
4
5    def add(self, x):
6        self.result += x
7        return self
8
9# WRONG — calling method on class
10Calculator.add(5)
11# TypeError: Calculator.add() missing 1 required positional argument: 'self'
12
13# CORRECT — create instance first
14calc = Calculator()
15calc.add(5)
16print(calc.result)  # 5

Common Cause 2: Missing Parentheses in Constructor

python
1class UserService:
2    def get_user(self, user_id):
3        return {"id": user_id, "name": "Alice"}
4
5# WRONG — forgot () after class name
6service = UserService  # This is the class itself, not an instance!
7service.get_user(1)
8# TypeError: UserService.get_user() missing 1 required positional argument: 'self'
9
10# CORRECT
11service = UserService()  # Note the parentheses
12service.get_user(1)  # {'id': 1, 'name': 'Alice'}

Common Cause 3: Passing Class Instead of Instance to a Function

python
1class Logger:
2    def log(self, message):
3        print(f"[LOG] {message}")
4
5def process_data(logger):
6    logger.log("Processing started")
7
8# WRONG — passing the class
9process_data(Logger)
10# TypeError: Logger.log() missing 1 required positional argument: 'self'
11
12# CORRECT — passing an instance
13process_data(Logger())

Common Cause 4: Method as Callback Without Binding

python
1class Button:
2    def __init__(self, label):
3        self.label = label
4
5    def on_click(self):
6        print(f"{self.label} clicked!")
7
8btn = Button("Submit")
9
10# WRONG — unbound reference in some frameworks
11# callback = Button.on_click  # Unbound method
12
13# CORRECT — bound method from instance
14callback = btn.on_click
15callback()  # "Submit clicked!"

When to Use @staticmethod or @classmethod

If a method does not need access to the instance, use @staticmethod:

python
1class MathUtils:
2    @staticmethod
3    def add(a, b):
4        return a + b
5
6    @classmethod
7    def from_string(cls, expression):
8        a, b = expression.split("+")
9        return cls.add(int(a), int(b))
10
11# staticmethod — can be called on class directly
12MathUtils.add(3, 5)  # 8 — no self needed
13
14# classmethod — receives the class, not the instance
15MathUtils.from_string("3+5")  # 8
DecoratorFirst ParameterCallable On
(none)self (instance)Instance only
@classmethodcls (class)Class or instance
@staticmethod(none)Class or instance

Debugging the Error

python
1class Service:
2    def process(self, data):
3        return len(data)
4
5# Check what you're calling the method on
6obj = Service  # Forgot ()
7print(type(obj))  # <class 'type'> — it's the class, not an instance!
8
9obj = Service()
10print(type(obj))  # <class 'Service'> — correct, it's an instance

The Error with init

python
1class User:
2    def __init__(self, name):
3        self.name = name
4
5# WRONG — calling __init__ on the class
6User.__init__("Alice")
7# TypeError: User.__init__() missing 1 required positional argument: 'self'
8
9# CORRECT — use the class constructor
10user = User("Alice")
11# Python calls User.__init__(new_instance, "Alice") internally

Common Pitfalls

  • Variable shadowing the class: If you have User = User("admin"), subsequent User("new") calls fail because User is now an instance, not the class. Use distinct names for classes and instances.
  • Forgetting parentheses in decorator patterns: @app.route vs @app.route() — missing parentheses can cause the decorator to pass the function as self to the wrong method.
  • Calling instance methods in list comprehensions: [MyClass.method(x) for x in data] fails. Use [instance.method(x) for x in data] or create the instance first.
  • Confusing @staticmethod need: If your method does not use self, add @staticmethod. Without it, calling MyClass.method() always raises the missing self error even though the method body does not use self.
  • Inheritance with super(): Calling super().method() is correct. Calling ParentClass.method() without passing self explicitly raises this error. Always use super() for parent method calls.

Summary

  • The error occurs when calling an instance method on the class instead of on an instance
  • Always create an instance with MyClass() before calling instance methods
  • Use @staticmethod for methods that do not need self, and @classmethod for methods that need the class
  • Check type(obj) to verify you have an instance, not the class itself
  • Python translates obj.method() to Class.method(obj) — calling Class.method() directly omits the instance

Course illustration
Course illustration

All Rights Reserved.