[Tutor] Need Explanation...

James Reynolds eire1130 at gmail.com
Mon Dec 12 13:40:26 CET 2011



Sent from my iPad

On Dec 12, 2011, at 7:08 AM, "Homme, James" <james.homme at highmark.com> wrote:

> 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


But a python list is mutable. I'm hardly an expert, but the idea is you are modifying the list. Why would you create another copy of the same list via a return statement?

At it's fundamentals python is really an object oriented language. When you use append, extend, pop, sort, etc on the list you retain the object but change some of the attributes stored in the object. Your string example isn't analogous because they are, as you noted, immutable.

I don't see this as a weakness but a strength. It cuts back on confusion and extraneous variables. 

Your example of Len also doesn't apply. It isn't modifying anything. It's returning the length of the list, in this case. You change nothing.

Open, if I recall, creates a file object in the same way class A:  a = A() creates an instance of A.

But like I said I'm not an expert.







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


More information about the Tutor mailing list