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 prependscalcas argument #1, followed by your explicit argument5as argument #2 (Calculator.double_bad(calc, 5)). - Similarly, a method defined with zero parameters (
def reset():) called ascalc.reset()raisesTypeError: reset() takes 0 positional arguments but 1 was given.
- When you invoke a function attribute on an instance (
- Two other common ways this exact
TypeErrorhappens:- 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 bindself, whereas assigning it to the class (Calculator.fn = my_func) does bindselfas the first argument on every instance call!
- Assigning a regular function to an instance (
- Overriding a method in a subclass with fewer parameters (or missing
*args, **kwargs):- Frameworks (like Django, Flask,
unittest, orhttp.server) call overridden hook methods with specific arguments (e.g.,self.handle(request)). Definingdef handle(self):withoutrequestraisestakes 1 positional argument but 2 were given.
- Frameworks (like Django, Flask,
- Assigning a standalone helper function directly onto an instance (
- Choosing between
self,, and:- Use
def method(self, x):when the method reads or modifies instance attributes (self.value). - Use
(def method(cls, x):) when the method needs access to the class itself (e.g., alternative constructors) rather than a specific instance. - Use
(def method(x):) when the function belongs logically inside the class namespace but needs neitherselfnorcls—making bothcalc.double_static(5)andCalculator.double_static(5)pass only5(1 argument).
- Use
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 | <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 | <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 inside a class body. |
You must log in or register to comment.
