On Tue, 18 Feb 2020 at 20:47, Rhodri James
On 18/02/2020 20:33, Soni L. wrote:
On 2020-02-18 5:08 p.m., Rhodri James wrote:
On 18/02/2020 19:43, Soni L. wrote:
It'd be nice to have a __valid_getitem_requests__ protocol that, if present, yields pairs such that:
for key, value in items(obj): assert obj[key] == value
for any obj.
OK, I'll bite. What is this "items()" function you apply to the arbitrary object?
Similar to len(). Just a shitty wrapper for __valid_getitem_requests__.
Language, sunshine.
Do you have a use case for this, or is it just theoretically nice to have? I have to say it isn't nice enough for me to actually want it, and I say that as someone who regularly forgets that iterating over a dict gets you its keys.
This looks to me like Lua's `items()` function. 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). Paul