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?
Advertisement
Answer
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