append method
Chris Kaynor
ckaynor at zindagigames.com
Wed May 23 16:31:32 EDT 2012
On Wed, May 23, 2012 at 12:42 PM, Dave Angel <d at 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.
More information about the Python-list
mailing list