Thoughts on List Methods and Return Values

Tim Peters tim.one at home.com
Sat Feb 17 20:42:49 CET 2001


[Don O'Donnell, about list methods]
> Methods that return None:
>    append, extend, insert, remove, reverse, sort
> ...
> Which leads me to the thought that if *all* the methods which now return
> None were to modify the list in place as well as return the modified
> list, we would soon get used to this behaviour and achieve some coding
> economy by being able to chain operations.
> ...

[Alex Martelli]
> Quite apart from any evaluation of this proposal, I fail to see
> what 'lot of code' could possibly be broken by this change.  Is
> there a 'lot of code' _relying_ on the None return, e.g.:
> ...

In a roundabout way, yes:

>>> x = [random.random() for i in xrange(1000000)]
>>> x.sort()

Oops!  Guess what that does to your interactive session if x.sort() starts
to return x.  People using my doctest.py would find that tons of their
docstring examples "suddenly don't work" because of that too.

BTW, it's not just lists, it's all mutate-in-place methods of all builtin
types that return None.

dict.update()-etc-ly y'rs  - tim





More information about the Python-list mailing list