A
A
Alexander Krasnov2015-10-20 03:39:17
Python
Alexander Krasnov, 2015-10-20 03:39:17

Why does the sorted function's key parameter take key = lambda x: abs(x) but not abs()?

Hello!
Why does the sorted function's key parameter take key = lambda x: abs(x) but not abs()?
And for example, if I create a function like this:

def my_abs(x=0):
                  abs(x)

I still can't do this:
sorted(my_list, key = my_abs())

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Alexander, 2015-10-20
@Pompeius_Magnus

Because you are passing as key the result of calling the abs function, not the function itself. Remove parentheses.

A
abs0lut, 2015-10-20
@abs0lut

Python 3.4 - everything works

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question