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/

_______________________________________________
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor

Reply via email to