LangWart: Method congestion from mutate multiplicty
Rick Johnson
rantingrickjohnson at gmail.com
Sun Feb 10 13:45:13 EST 2013
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()"
> Reversed(iterable) is more complicated because it returns an iterator,
> not a list, and looks for a class-specific __reversed__ method.
> [...]
Well if you're taking the position that iterators are difficult to create i say you are exaggerating a bit. Using the for loop:
py> for LOCALVAR in SEQUENCE:
... do_something
we can get the iterator for free. If however you want to control the iteration /without/ being locked into a loop, you can explicitly call:
py> iter(seq)
Or, if you prefer methods over global functions:
py> seq.__iter__()
Or, if python employed /true/ OOP paradigm:
py> Iterator(seq)
> 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.
> reversed(it) is not the same as list(it).reverse(). So
> that part of the premise of this thread is wrong.
Well, it's not the same /now/, because of how Python handles this operation. The status quo is to encourage the implicit idiom over the explicit, however, this behavior could be optimized to cleanly handle /explicit/ syntax only.
More information about the Python-list
mailing list