Why does Python raise "TypeError: method() takes 1 positional argument but 2 were given"? | A1Lab@MZeeshanM to PythonEnglish • 4 days ago

Why does Python raise "TypeError: method() takes 1 positional argument but 2 were given"?

message-square
0

Python raises TypeError: method() takes 1 positional argument but 2 were given when you call a method on a class instance (obj.method(arg)) whose definition is missing self as its first parameter (e.g., def method(arg): instead of def method(self, arg):)—because Python automatically passes the instance obj as the implicit first positional argument alongside your explicit argument arg.

Basic Example

class Calculator:
    # ❌ Broken: Missing `self` parameter (only accepts 1 positional argument)
    def double_bad(x):
        return x * 2

    # ✅ Fix 1: Include `self` as the first parameter for instance methods
    def double_ok(self, x):
        return x * 2

    # ✅ Fix 2: Decorate with `@staticmethod` if the method does not use `self` or `cls`
    @staticmethod
    def double_static(x):
        return x * 2


calc = Calculator()

# calc.double_bad(5)      # Translates to Calculator.double_bad(calc, 5) -> 2 args given!
# TypeError: Calculator.double_bad() takes 1 positional argument but 2 were given

print(calc.double_ok(5))      # Output: 10
print(calc.double_static(5))  # Output: 10

Key Details & Edge Cases

  • Why Python says “2 were given” when you only wrote 1 inside ():
    • When you invoke a function attribute on an instance (calc.double_bad(5)), Python’s descriptor protocol binds the function into a bound method and automatically prepends calc as argument #1, followed by your explicit argument 5 as argument #2 (Calculator.double_bad(calc, 5)).
    • Similarly, a method defined with zero parameters (def reset():) called as calc.reset() raises TypeError: reset() takes 0 positional arguments but 1 was given.
  • Two other common ways this exact TypeError happens:
    1. Assigning a standalone helper function directly onto an instance (obj.fn = helper):
      • Assigning a regular function to an instance (calc.fn = my_func) does not bind self, whereas assigning it to the class (Calculator.fn = my_func) does bind self as the first argument on every instance call!
    2. Overriding a method in a subclass with fewer parameters (or missing *args, **kwargs):
      • Frameworks (like Django, Flask, unittest, or http.server) call overridden hook methods with specific arguments (e.g., self.handle(request)). Defining def handle(self): without request raises takes 1 positional argument but 2 were given.
  • Choosing between self, @classmethod, and @staticmethod:
    • Use def method(self, x): when the method reads or modifies instance attributes (self.value).
    • Use @classmethod (def method(cls, x):) when the method needs access to the class itself (e.g., alternative constructors) rather than a specific instance.
    • Use @staticmethod (def method(x):) when the function belongs logically inside the class namespace but needs neither self nor cls—making both calc.double_static(5) and Calculator.double_static(5) pass only 5 (1 argument).

Comparison of Class Method Signatures & Implicit Arguments

Method Type Definition Syntax Called via Instance (obj.m(5)) Called via Class (Cls.m(5)) Best Used When
Instance Method (Default) def m(self, x): Passes (obj, 5) $\rightarrow$ 2 args Expects (obj, 5) explicitly Method reads or mutates per-instance state on self.
Static Method @staticmethod<br>def m(x): Passes (5,) $\rightarrow$ 1 arg Passes (5,) $\rightarrow$ 1 arg Utility logic grouped under the class that needs neither self nor cls.
Class Method @classmethod<br>def m(cls, x): Passes (Cls, 5) $\rightarrow$ 2 args Passes (Cls, 5) $\rightarrow$ 2 args Factory constructors or methods operating on class-level state cls.
Unbound Function on Class (Bug) def m(x): (no decorator) Passes (obj, 5) $\rightarrow$ Raises TypeError Passes (5,) $\rightarrow$ works Never—always add self or @staticmethod inside a class body.