On Fri, Dec 27, 2019 at 03:16:50PM -0000, Marco Sulla via Python-ideas wrote:
> Steven D'Aprano wrote:
> > On Fri, Dec 27, 2019 at 02:22:48AM -0000, Marco Sulla via Python-ideas
> > wrote:
> > > It's very common to see:
> > > for i, x in enumerate(sequence):
> > > [...]
> > >
> > > Yes, that is very common, except that "sequence" can be any iterable
> > with an unpredictable length, or even an infinite length, such as a
> > generator
>
> There's no problem.
Of course it's a problem. Who is going to go out and force developers of
a thousand different libraries and classes to add your two methods to
their classes? It's no problem for *you* because you aren't doing the
work. You are asking them to do more work, for no benefit. Your two
methods adds no new functionality beyond what is already supplied by
enumerate.
> Also iterators and generators can have a entries() method
The is no such type as "iterator" and no central place to add the
method. Iterator is defined by a protocol: any object with a `__next__`
method and an `__iter__` method that returns self is an iterator. It is
an intentionally simple protocol.
Forcing iterators to also include an extra two methods just adds
unnecessary complexity to the protocol. It means that everyone who
writes an iterator class will be forced to support your two methods,
even if they have no need for them.
And nobody has need for them, because they do nothing that enumerate
doesn't already do. Your entries method is just an alternative spelling
for enumerate().
> Yes and no: you added too much! dict views, set and frozenset are not
> sequences
Doesn't matter. We can iterate over dict views and sets, and we can
enumerate *any* iterable.
for i, x in enumerate(obj)
works perfectly well when obj is a set, a dict view, an iterator, a
sequence, a dict, an open file, or any other iterable object.
That's the point: enumerate works with all iterables, without exception.
Your extra methods do not. Why would anybody use your methods instead of
the idiom that is backwards and forwards compatible, and that works with
all iterables, not just a few of them?
Yes, *all* iterables, including mappings like dicts:
py> for i, x in enumerate({'a': 10, 'b': 20, 'c': 30}):
... print(i, x)
...
0 a
1 b
2 c
> Well, enumerate does work with **almost** every iterable. It does not
> work with maps, you have to use keys().
You don't have to use keys().
[...]
> > Even if the methods can share an implementation
> > (probably calling enumerate and range behind the scenes)
>
> This can be a fast and furious implementation for a first draft, but
> IMHO they should return a view, like for maps.
What benefit do you get from having the indexes be in a view?
> Currently, if you want to iterate over a map items or over a sequence,
> iterable, generator and file entries, you already have to use two
> different APIs.
That's incorrect. The for-loop API
for target in iterable
works perfectly well for all iterables, including mappings, dict
views, sequences, containers, iterators, generators, files,
strings and arrays.
--
Steven
_______________________________________________
Python-ideas mailing list -- [email protected]
To unsubscribe send an email to [email protected]
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at
https://mail.python.org/archives/list/[email protected]/message/BRL53OPS4PNXPR5Y2VP2STQFSFGP6HZN/
Code of Conduct: http://python.org/psf/codeofconduct/