Re: append method
wrote: s=[1,2,3] s.append(5) s [1, 2, 3, 5] s=s.append(5) s print s None why can't s=s.append(5) ,what is the reason? Because the append method returns None, not the object. It modifies the object in place, and does not create any copy. You can still write s = s + [5] if you really want to, but what's the point ? JM -- http://mail.python.org/mailman/listinfo/python-list
Re: append method
On Wed, May 23, 2012 at 8:23 AM, 水静流深 1248283...@qq.com wrote: s=[1,2,3] s.append(5) s [1, 2, 3, 5] s=s.append(5) s print s None why can't s=s.append(5) ,what is the reason? For the same reason that you don't see `[1, 2, 3, 5]` immediately after doing `s.append(5)` the first time around, but must instead check `s`: because the value is not returned from the function. `s` is modified in-place, and nothing is returned. This was a deliberate design decision made a long time ago that is very well documented; try Googling for `python why doesn't list.append return the value` for example. -- ~Zahlman {: -- http://mail.python.org/mailman/listinfo/python-list
Re: append method
On 5/23/2012 5:23 AM 水静流深 said... s=[1,2,3] s.append(5) s [1, 2, 3, 5] s=s.append(5) s print s None why can't s=s.append(5) It could, but it doesn't. ,what is the reason? A design decision -- there's currently a mix of methods that return themselves and not. Mostly is appears to me that mutables modify in place without returning self and immutables return the new value. But that's simply my observation. Emile -- http://mail.python.org/mailman/listinfo/python-list
Re: append method
On 05/23/2012 03:13 PM, Emile van Sebille wrote: On 5/23/2012 5:23 AM 水静流深 said... s=[1,2,3] s.append(5) s [1, 2, 3, 5] s=s.append(5) s print s None why can't s=s.append(5) It could, but it doesn't. ,what is the reason? A design decision -- there's currently a mix of methods that return themselves and not. Mostly is appears to me that mutables modify in place without returning self and immutables return the new value. But that's simply my observation. Emile It's simpler than that. Methods/functions either modify the object (or one of their arguments), or return the results, but generally not both. So sorted() returns a sorted list without modifying the input. And the sort() method modifies the list, but does not return it. So you're right that methods on non-mutables must return the new value, since they can't modify the object. -- DaveA -- http://mail.python.org/mailman/listinfo/python-list
Re: append method
On Wed, May 23, 2012 at 12:42 PM, Dave Angel d...@davea.name wrote: On 05/23/2012 03:13 PM, Emile van Sebille wrote: A design decision -- there's currently a mix of methods that return themselves and not. Mostly is appears to me that mutables modify in place without returning self and immutables return the new value. It's simpler than that. Methods/functions either modify the object (or one of their arguments), or return the results, but generally not both. So sorted() returns a sorted list without modifying the input. And the sort() method modifies the list, but does not return it. So you're right that methods on non-mutables must return the new value, since they can't modify the object. While this is true, its also important to keep in mind that, mostly due to magic methods, what appears to be a single function call my be multiple, and as such there are cases which appear to do both. Consider sorted on a generator. It returns the result, but the magic method __iter__ is implicitly called by sorted, and mutates the generator. Aside from those cases, the only cases I can think of in the Python standard library are cases where the code is only intended to be used directly in the interpreter, and not scripted, namely debugging aids such as pstats, where the convenience of having both outweighs the potential of confusion. -- http://mail.python.org/mailman/listinfo/python-list