Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

python

sorting

a=[1,2,3,4]
def func(x):
   return x**x

b=sorted(a,func)

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

def func2(x,y=4,z=10):
    print(x,y,z)
func2(100,200)--->output-->>100 200 10

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

like image 308
Nirbhay Garg Avatar asked Jun 24 '20 21:06

Nirbhay Garg


People also ask

What type can the argument to sorted () be?

Arguments of sort and sorted Both sort and sorted have three keyword arguments: cmp , key and reverse .

How do you pass a positional argument in Python?

Functions can accept a variable number of positional arguments by using *args in the def statement. You can use the items from a sequence as the positional arguments for a function with the * operator. Using the * operator with a generator may cause your program to run out of memory and crash.

Can we pass positional arguments in any order in Python?

Positional arguments must be passed in order as declared in the function. So if you pass three positional arguments, they must go to the first three arguments of the function, and those three arguments can't be passed by keyword.

Is Python sorted () stable?

sort() and sorted() in python are “stable sorts”, meaning that it'll preserve the existing order when faced with a tie.


2 Answers

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

sorted(iterable, *, key=None, reverse=False)

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:

def func2(x, *, y=4, z=10):
    print(x, y, z)

func2(100, 200)

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

like image 142
ywbaek Avatar answered Oct 06 '22 01:10

ywbaek


why sorted() in python doesn't accept positional arguments?

Because sorted used to accept positional arguments in Python 2, but the second argument was the comparison function, not the key function as it is now. Python 3 dropped support for comparison function in list.sort and sorted was modified accordingly. It was deemed too confusing to silently change the meaning of a poositional argument, so the devs decided to completely forbid positional arguments (except for the very first argument, the iterable to sort), and require keyword arguments for everything else.

The intention is for unmodified Python 2 code that calls sorted(sequence, function) to fail fast in Python 3, instead of attempting to call the comparison function as a key function, calling it with an incorrect number of arguments. This prevents confusion that might ensue if the comparison function happened to accept a variable number of arguments, or if the sequence were empty so the sort happened to "work" for some inputs.

Likewise, sorted(sequence, cmp=function) fails early due to cmp having been removed. sorted(sequence, key=function) works as intended in both Python 2 and 3.

like image 44
user4815162342 Avatar answered Oct 06 '22 01:10

user4815162342