Python throws TypeError: ClassName.method() missing 1 required positional argument: 'self' when you call a regular instance method directly on the class itself (MyClass.method()) instead of on an instantiated object (obj = MyClass(); obj.method()), because Python only injects the instance (self) automatically when a method is accessed through an instance.
Basic Example
class Calculator:
def __init__(self, base: int = 0) -> None:
self.base = base
def add(self, value: int) -> int:
return self.base + value
# WRONG: Calling an instance method directly on the class (no instance created)
# Calculator.add(5)
# -> TypeError: Calculator.add() missing 1 required positional argument: 'self'
# (Python treats `5` as `self` and sees `value` is missing!)
# FIX 1 (Recommended): Instantiate the class with `()` first, then call the method
calc = Calculator(10)
print(calc.add(5)) # Output: 15
# FIX 2: If the method does NOT use `self`, decorate it with @staticmethod or @classmethod
class MathUtils:
@staticmethod
def add(a: int, b: int) -> int:
return a + b
print(MathUtils.add(10, 5)) # Output: 15
Key Details & Edge Cases
- Forgetting parentheses
()when instantiating a class (obj = MyClassinstead ofobj = MyClass()):- Assigning
calc = Calculatorbindscalcto the class object itself, not an instance. Callingcalc.add(5)then fails withmissing 1 required positional argument: 'self'. Always include()when constructing an object (calc = Calculator()).
- Assigning
- How Python’s descriptor protocol binds
selfunder the hood:- Every function inside a
classbody is a non-data descriptor. Accessingcalc.addon an instance invokesfunction.__get__(calc, Calculator), returning a bound method that automatically prependscalcasself. - Accessing
Calculator.addon the class invokesfunction.__get__(None, Calculator), returning the raw unbound function whereselfis just an ordinary first positional parameter.
- Every function inside a
- Misleading error count when passing multiple arguments (
Calculator.add(10, 5)):- If you call
Calculator.add(10, 5), Python assignsself = 10andvalue = 5without raisingTypeErrorimmediately—and then crashes inside the method body withAttributeError: 'int' object has no attribute 'base'. Conversely, callingCalculator.add(10)passesself = 10and complains thatvalue(notself) is missing!
- If you call
- Forgetting
selfin the method definition (def add(value):):- The opposite mistake—defining
def add(value):withoutselfand callingcalc.add(5)—raisesTypeError: Calculator.add() takes 1 positional argument but 2 were givenbecause Python automatically suppliescalcas argument #1 and5as argument #2.
- The opposite mistake—defining
- Passing an unbound method as a callback (
button.on_click(MyClass.handler)):- Passing
MyClass.handlerinstead ofself.handler(orinstance.handler) into GUI frameworks, threads, ormap()passes an unbound function that lacksselfwhen invoked later. Always pass the bound methodinstance.handler.
- Passing
- Decorators that strip descriptor binding: Custom decorators implemented as classes without
__get__can prevent Python from bindingselfeven when called on an instance. Decorate wrapper functions with@functools.wraps(func)so method binding works normally.
Common Causes & Fixes Reference
| Mistaken Pattern | Why It Happens | Clean Fix |
|---|---|---|
MyClass.do_work() |
Called directly on the class instead of an instance | Instantiate first: obj = MyClass(); obj.do_work() |
obj = MyClass; obj.do_work() |
Missing () during instantiation—obj is still the class |
Add parentheses: obj = MyClass() |
MyClass.helper(x) (no self needed) |
Method doesn’t touch instance state, but lacks decorator | Add (or if cls is needed) |
register_callback(MyClass.on_event) |
Passed unbound class attribute instead of bound instance method | Pass self.on_event or instance.on_event |
super().__init__(self) |
Manually passing self to a bound super() method |
Omit self: super().__init__() |
You must log in or register to comment.