[Tutor] Need Explanation...

Alan Gauld alan.gauld at btinternet.com
Sat Dec 10 10:15:42 CET 2011


On 10/12/11 07:41, sunil tech wrote:

> /def app(x):/
> /     return x.append(100)/
> /
> /p = app(a)/
> /
> /now list holds appended value [1,2,3,100]/
> /but p is empty... why it is?/

Because app() returns the result of append().
But append() returns None, since it modifies the list in place.

This is one of the few features of Python I dislike. It would not have 
been difficult to make these modifier methods return the thing modified. 
This style would then allow chained methods.

We do it with strings:

"foobar is a string".rstrip('ing').upper()

because strings are immutable. But we could have done it with other 
sequence types too. Sadly we didn't and history/tradition leaves us with 
these counterintuitive modifiers that return None. It catches everybody 
out at some point...


-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/



More information about the Tutor mailing list