TypeError: missing 1 required positional argument
1 · The lesson
readWhat this error means
You called a function with fewer arguments than its def line asks for. Python matched the values you passed to the parameters from left to right. It ran out of values, and one parameter with no default was left without a value. The message names that parameter, and this is the most useful part of the message: read the name in quotes first.
When you see it
Traceback (most recent call last): File "main.py", line 6, in <module> print(order.total()) ~~~~~~~~~~~^^ TypeError: Order.total() missing 1 required positional argument: 'tax'
Traceback (most recent call last): File "main.py", line 6, in <module> print(User.describe()) ~~~~~~~~~~~~~^^ TypeError: User.describe() missing 1 required positional argument: 'self'
The second one is a clear sign: when the missing argument is 'self', you called the method on the class, not on an object.
Why it happens
There are four common causes, and only the first is a simple typo.
1. You left an argument out. Someone added a parameter to the function last week, and one call site was never updated.
2. You called a method on the class. User.describe() has no object to pass as self, so self is the missing argument.
3. You passed an argument that silently became self. Order.total(0.2) binds 0.2 to self, so Python reports that tax is missing, although you thought you had passed it.
4. Something else calls your function for you. map(), sorted(key=...), a button callback or a thread passes the number of arguments that it decides, not the number your function needs.
class Order: def __init__(self, subtotal): self.subtotal = subtotal def total(self, tax): return round(self.subtotal * (1 + tax), 2) order = Order(120) print(order.total()) # TypeError: Order.total() missing 1 required positional argument: 'tax'
How to fix it
Pass the argument, or give the parameter a default if it is normal to leave it out:
class Order: def __init__(self, subtotal): self.subtotal = subtotal def total(self, tax=0.2): # default VAT rate when the caller doesn't say return round(self.subtotal * (1 + tax), 2) order = Order(120) print(order.total()) # 144.0 print(order.total(0.05)) # 126.0
Only add a default when there really is a sensible one. If a default hides a forgotten argument, you get a wrong invoice with no error, instead of a clear error.
If the missing one is self, create an instance first:
class User: def __init__(self, name): self.name = name def describe(self): return f"{self.name} (active)" ada = User("Ada") print(ada.describe()) # Ada (active) (call it on the object, not as User.describe())
If the method never uses self, it shouldn't be an instance method. Make it a @staticmethod, and then you can call it on the class.
When a library calls your function, fill in the extra arguments yourself with functools.partial or a lambda:
from functools import partial def apply_discount(price, rate): return round(price * (1 - rate), 2) prices = [19.99, 45.00, 8.50] # map() passes ONE argument per item, so `rate` would be missing. ten_off = partial(apply_discount, rate=0.10) print(list(map(ten_off, prices))) # [17.99, 40.5, 7.65] print([apply_discount(p, 0.10) for p in prices]) # same thing, and arguably clearer
When you'd see this in real code
- Adding a parameter to a widely used function and updating only some of the call sites. The traceback points at the caller you missed.
- Writing
sorted(orders, key=compare)wherecompare(a, b)is an old-style two-argument comparison.key=passes one item, so wrap it withfunctools.cmp_to_key(compare). - A Tkinter or GUI callback defined as
def on_save(self, event)but registered somewhere that passes nothing. - A pandas
df.apply(fee, axis=1)wherefee(row, rate)needs a rate: pass it withdf.apply(fee, axis=1, args=(0.03,)). - Calling
Invoice.from_row(row)whenfrom_rowwas written as an instance method instead of a@classmethod.
Related errors
- TypeError: takes 1 positional argument but 2 were given: the opposite problem, too many arguments.
- TypeError: got an unexpected keyword argument: the right number of arguments, but a wrong name.
- TypeError: 'X' object is not callable
- TypeError: X() takes no arguments: the class has no working __init__ at all, usually a misspelling like _init_ or __int__.
See Also
- All Python errors: the full index, by type and by when it happens.
- Functions (tutorial)
- Classes and objects (tutorial)
- Lambda functions
Found this useful? Share it with a friend who’s learning Python.