[Tutor] Need Explanation...

Homme, James james.homme at highmark.com
Mon Dec 12 13:08:52 CET 2011


Hi,
Alan said:
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...


Is that the same problem with using the len function on sequences and open on files, or is it different?

Thanks.

Jim

-----Original Message-----
From: tutor-bounces+james.homme=highmark.com at python.org [mailto:tutor-bounces+james.homme=highmark.com at python.org] On Behalf Of Alan Gauld
Sent: Saturday, December 10, 2011 4:16 AM
To: tutor at python.org
Subject: Re: [Tutor] Need Explanation...

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 at python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor

________________________________

This e-mail and any attachments to it are confidential and are intended solely for use of the individual or entity to whom they are addressed. If you have received this e-mail in error, please notify the sender immediately and then delete it. If you are not the intended recipient, you must not keep, use, disclose, copy or distribute this e-mail without the author's prior permission. The views expressed in this e-mail message do not necessarily represent the views of Highmark Inc., its subsidiaries, or affiliates.


More information about the Tutor mailing list