Skip to content
Advertisement

why sorted() in python didn’t accept positional arguments?

JavaScript

this line always gives a error->

TypeError: sorted expected 1 argument, got 2

in fact the syntax of sorted is sorted(iterable,key,reverse), in which key and reverse are optional, so according to this, second parameter i pass must go with key.

and when i def my own func

JavaScript

here 200 automatically passed as y argument for func2. How does this work?

Advertisement

Answer

In addition to @user4815162342’s answer,
From the documentation,

JavaScript

Notice the * between iterable and key parameter. That is the python syntax for specifying that
every parameter after * must be specified as keyword arguments.

So your custom function should be defined as the following to apply the similar implementation:

JavaScript

TypeError: func2() takes 1 positional argument but 2 were given

User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement