basic language question

Terry Reedy tjreedy at udel.edu
Thu Sep 4 12:55:27 EDT 2003


"Stephan Diehl" <stephan.diehl at gmx.net> wrote in message
news:bj7ots$48j$06$1 at news.t-online.com...
> Once in a while, I get bitten by the fact, that mutating list
methods such
> as 'append' or 'extend' return None instead of the (mutated) list
itself.
> Is there a compelling reason for that? I googled around, but didn't
find
> anything about this.

Some people, including GvR, think in-place mutators should not return
anything to distinguish them from methods which return a new object
and leave original untouched.  One argument, I presume based on some
experience, is that if both types of method look the same, people will
forget difference, leading to obscure bugs.  Others, confident in
their ability to remember and not make mistakes, want mutated object
returned so they chain methods togethers.  While Python is generally a
consenting-adults language, this is one place where Guido opted for
the supposedly 'safer' choice.

'Returners' could wrap no-return mutators with functions or
derived-class methods that do return the object, but I have never seen
anyone post a complete module that does so for, say, all list
mutators.

Terry J. Reedy






More information about the Python-list mailing list