basic language question

John Roth newsgroups at jhrothjr.com
Thu Sep 4 15:32:47 EDT 2003


"Michael Peuser" <mpeuser at web.de> wrote in message
news:bj820r$8up$05$1 at news.t-online.com...
>
> "Stephan Diehl" <stephan.diehl at gmx.net> schrieb im Newsbeitrag
> 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.
>
> There is a very old religious law saying: Thou shalt not cause side
effects
> by a function.
> There is some wisdom in it but it is easily forgotten with languages which
> do not differ between functions and routines.

Like most "religious laws," it's a rule that has a multitude of exceptions.
You can't do I/O without having side effects, something that the designers
of functional languages have learned, sometimes the hard way.

As far as I'm concerned, this is one of those things that Ruby did
right. By convention, methods that modify the object as a side effect
have names that end with an "!" (exclamation point.) Ruby usually supplies
two methods in such cases, one that modifies the object, and one that
creates a copy and then modifies the copy. Both methods return the
modified or new object.

It obeys the Principle of Least Surprise because if you forget the
"!", you get the slow version that does a copy and leaves the original
intact.

Nice as the solution is, it's impossible to import it into Python
cleanly without introducing catastrophic backward incomaptibilities.

John Roth
>
> Kindly
> Michael P
>
>






More information about the Python-list mailing list