On 5/3/2023 3:46 PM, Oscar Benjamin wrote:
On Wed, 3 May 2023 at 18:52, Thomas Passin <li...@tompassin.net> wrote:

On 5/3/2023 5:45 AM, fedor tryfanau wrote:
I've been using python as a tool to solve competitive programming problems
for a while now and I've noticed a feature, python would benefit from
having.
Consider "reversed(enumerate(a))". This is a perfectly readable code,
except it's wrong in the current version of python. That's because
enumerate returns an iterator, but reversed can take only a sequence type.

Depending on what you want to give and receive, enumerate(reversed(a))
will do the job here.  Otherwise list() or tuple() can achieve some of
the same things.

I don't think that is equivalent to the intended behaviour:

reversed(enumerate(a)) # zip(reversed(range(len(a))), reversed(a))
enumerate(reversed(a)) # zip(range(len(a)), reversed(a))

I don't think we know the intended behavior here. The OP did not say what type of object should be returned. He only wanted an expression that would run. Apparently the result should be an enumeration of variable "a" but with "a" reversed. Is "a" supposed to be a sequence? An iterator? Presumably the result was expected to be an enumeration, which is to say an iterator, and enumerate(reversed(a)) would return an iterator.

Perhaps the OP wants all methods/functions that operate on either sequences or iterators should be generalized to be able to operate on both. That doesn't sound unreasonable on the face of it, but I think that deeper study would uncover a lot of hard questions. There will also be a lot of edge cases to handle and get right.

In principle for a sequence input enumerate(a) could be something that
behaves like a sequence and therefore could be reiterated or reversed
etc. The enumerate(a).__reversed__ method could then delegate to
a.__reversed__ and a.__len__ if they exist. This could be confusing
though because the possible behaviour of enumerate(a) would be
different depending on the type of a.

--
Oscar

--
https://mail.python.org/mailman/listinfo/python-list

Reply via email to