On Tue, Feb 18, 2020 at 09:04:21PM +0000, Paul Moore wrote:

> This looks to me like Lua's `items()` function.

I can't find that function listed anywhere.

https://www.lua.org/manual/5.3/

Are you maybe thinking of `pairs()`?

https://www.lua.org/manual/5.3/manual.html#pdf-pairs

> It's useful in Lua,
> but I'm not sure there's any obvious reason to assume it'll be a
> natural fit for idiomatic Python code. As people have said, do you
> have a good Python example of real code where this would be useful?
> (And where dct.items() or enumerate(lst) wouldn't be sufficient - code
> that expects to work equally with lists or dictionaries seems more
> Lua-like than Pythonic, IMO).

In Lua, lists (arrays) *are* dictionaries, they keys are merely 
implicitly set to consecutive integers.

My personal experience is that the analogy between (index, item) and 
(key, item) is more useful in theory and practice, but if I ever needed 
to do such a thing, a thin helper function:

    def items(dict_or_seq):
        try:
            return obj.items()
        except AttributeError:
            return enumerate(obj)

would do the job.

I don't think this is useful or important enough to turn it into a 
language-wide protocol. It's not as if the concept has much, if any, use 
outside of mappings and sequences.


-- 
Steven
_______________________________________________
Python-ideas mailing list -- python-ideas@python.org
To unsubscribe send an email to python-ideas-le...@python.org
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at 
https://mail.python.org/archives/list/python-ideas@python.org/message/TO2HDDRJC5VLA6APR4NFVQRL72FYSOBX/
Code of Conduct: http://python.org/psf/codeofconduct/

Reply via email to