Hacker News new | past | comments | ask | show | jobs | submit login

I think I'm combining issues in a problematic way here and I don't even know what was going on when I typed __in__ (twice! :/) instead of __contains__ - I'll just blame the lack of coffee. I realise why the membership 'in' should operate on the hash table in memory for O(1) performance and it makes perfect sense. I've just never been fully comfortable with the idea that keys are considered the 'members' of a 'mapping' in and of themselves. Maybe because I usually consider one-way mapping as a special case.

The more reasonable (I think) problem I have is one of language semantics which is introduced by in being applicable to a mapping in the first place - why should a mapping be iterable at all if it is ambiguous (as I believe) as to what it should return? Members should be key:val pairs but membership checks refer only to keys and it's probably not unreasonable for a user to want any of .keys(), .values() or .items() when iterating. That's obviously why they're made available so why should __iter__ special case one of them?

I assume this is to match semantics introduced by the membership check and probably historical because without a .values() method, key iteration and lookups would be the usual way to get all values out of a mapping. This just seems like the kind of thing that could have been changed in Python3 (although 2to3 would probably not have been able to handle the syntax changes automatically).




The code to test for membership of a key value pair is already straightforward for any mapping:

    map[key]==value
Which is a weak reason in support of the use of containment testing for something else.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: