Why are sorted () and reverseed () are built-in functions and not sequence methods?

Now I'm moving into Python 3.3, and I'm wondering why some functions (for example sorted(), reversed()) for managing collections / iterable are built-in, but some of them are implemented as methods of collection objects? I can add an item to the list using the method append(), but in order to get its length, I have to use the built-in function len(). It seems inconsistent to me, but I think I just missed some point in the new language.

+4
source share
1 answer

The short answer is that these decisions were made for human reasons, and not for technical reasons.

Justification len()versus obj.length() explained by Guido van Rossum (Python Benevolent Dictator For Life):

First of all, I chose len (x) for x.len () for HCI reasons (def __len__()came much later). There are actually two related reasons: and HCI:

(a) For some operations, prefix notation only reads better than postfix - prefix (and infix!) operations have a long tradition in mathematics that loves notes where visual effects help the mathematician think about a problem. Compare the ease with which we rewrite a formula such as x * (a + b) to x * a + x * b, to be awkward at doing the same using raw OO notation.

(b) , len (x), , -. : - , - - . , x.len(), , x - - , , len(). , , , , get() keys() -, , write().

, sorted() reversed() :

, ; , , . , max, sum, map, any, in others, , , .

+5

Source: https://habr.com/ru/post/1523968/


All Articles