Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why don't list operations return the resulting list?

Tags:

python

I'm interested in the thought process that led to this. To me, a relative newbie, it seems hampering, since it prevents "chaining" of list processing (e.g. mylist.reverse().append('a string')[:someLimit]). I imagine it might be that "The Powers That Be" decided that list comprehension is a better paradigm (a valid opinion), and so didn't want to encourage other methods - but it seems perverse to prevent an intuitive method, even if better alternatives exist.

Note that I'm not complaining (I'm sure there is a sensible reason, I'm just interested in what it is!), nor looking for a solution (the comments here were very instructive) - just looking for some context, and a deeper understanding of the language's design process.

like image 701
scubbo Avatar asked Jun 26 '12 10:06

scubbo


People also ask

Why does sorting a list return None?

The sort method operates on the contents of the object named — think of it as an action that the object is taking to re-order itself. The sort function is an operation over the data represented by an object and returns a new object with the same contents in a sorted order. This method has no return value.

Does sorted return a new list?

The original numbers variable is unchanged because sorted() provides sorted output and does not change the original value in place. When sorted() is called, it provides an ordered list as a return value.

Which function returns the number of items in a list?

In Python, the built-in function len() is used to get the length (the number of items) of a list.

What does .append return in Python?

The method a. append() modifies a itself, which means that there's nothing to return.


1 Answers

The general design principle in Python is for functions that mutate an object in-place to return None. I'm not sure it would have been the design choice I'd have chosen, but it's basically to emphasise that a new object is not returned.

Guido van Rossum (our Python BDFL) states the design choice here: http://mail.python.org/pipermail/python-dev/2003-October/038855.html)

I'd like to explain once more why I'm so adamant that sort() shouldn't return 'self'.

This comes from a coding style (popular in various other languages, I believe especially Lisp revels in it) where a series of side effects on a single object can be chained like this:

x.compress().chop(y).sort(z)

which would be the same as

x.compress()
x.chop(y)
x.sort(z)

I find the chaining form a threat to readability; it requires that the reader must be intimately familiar with each of the methods. The second form makes it clear that each of these calls acts on the same object, and so even if you don't know the class and its methods very well, you can understand that the second and third call are applied to x (and that all calls are made for their side-effects), and not to something else.

I'd like to reserve chaining for operations that return new values, like string processing operations:

y = x.rstrip("\n").split(":").lower()

There are a few standard library modules that encourage chaining of side-effect calls (pstat comes to mind). There shouldn't be any new ones; pstat slipped through my filter when it was weak.

like image 56
Jon Clements Avatar answered Sep 20 '22 15:09

Jon Clements