LangWart: Method congestion from mutate multiplicty
Terry Reedy
tjreedy at udel.edu
Sun Feb 10 16:44:25 EST 2013
On 2/10/2013 1:45 PM, Rick Johnson wrote:
> On Sunday, February 10, 2013 2:39:21 AM UTC-6, Terry Reedy wrote:
>> While it is true that sorted(iterable) is essentially
>>
>> def sorted(iterable):
>> tem = list(iterable)
>> tem.sort
>> return tem
>>
>> the body is not an expression and cannot be substituted in an
>> expression.
>
> Yes but the body can be compressed to this single line: "list(iterable).sort()"
That single line now evaluates to None, so that does not work.
>> Even if list mutation methods returned the list, which they do not and
>> for good reason,
>
> I am not proposing that in-place modification return the object.
It seems to me that you are, as that is the only way for
'list(iterable).sort()' to replace 'sorted(iterable)', as you proposed
both originally and above.
The reason sorted(iterable) was added is 'list(iterable).sort()', which
newbies would try, *does not work*. Sorted was added so people would not
have to write
tem = list(iterable)
tem.sort()
<statement using tem>
del tem
as they did previously, and instead could write
<statement using sorted(iterable)>
Reversed was added not only for the same reason, but also to avoid the
temporary list altogether when not actually needed, which it often or
usually is not.
--
Terry Jan Reedy
More information about the Python-list
mailing list