Python TypeError: sort() takes no positional arguments

Exactly what it says: sort doesn't take any positional arguments. It takes a keyword-only argument named key:

birds.sort(key=lambda b: b.weight())

From the documentation:

sort(*, key=None, reverse=False)

This method sorts the list in place, using only < comparisons between items. Exceptions are not suppressed - if any comparison operations fail, the entire sort operation will fail (and the list will likely be left in a partially modified state).

sort() accepts two arguments that can only be passed by keyword (keyword-only arguments):

key specifies a function of one argument that is used to extract a comparison key from each list element (for example, key=str.lower). The key corresponding to each item in the list is calculated once and then used for the entire sorting process. The default value of None means that list items are sorted directly without calculating a separate key value.

[...]

The * in the signature is the separator between positional parameters and keyword-only parameters; its position as the initial "argument" indicates the lack of positional parameters.


Looking at the documentation for list.sort, we can see that key is a keyword-only argument. So change the line

birds.sort(lambda b: b.weight())

to

birds.sort(key=(lambda b: b.weight()))


sort() takes a key argument and nothing else (well, it can take a reverse argument). You supplied sort() with an argument it cannot accept. Just add a key= before your lambda

The error message is because key takes keyword arguments, not positional arguments. A positional argument is a name that is not followed by an equal sign and default value.