[].keys() and [].items() (Was: Why I think range is a wart)
Clark C . Evans
cce at clarkevans.com
Fri Mar 15 23:58:28 EST 2002
On Thu, Mar 14, 2002 at 12:18:13AM +0000, Huaiyu Zhu wrote:
| On Wed, 13 Mar 2002 22:39:02 GMT, Terry Reedy <tejarex at yahoo.com> wrote:
| >> I would like to see:
| >> .keys()
| >> .items()
|
| Question: Which is better: items(x) or x.items()?
Does it have to be mutually exclusive? Python has
a tradition of enabling both mechanisms.
| I think items(x) can just piggy-back on the current iterator protocol, and
| can be made to work on any iteratable objects.
Would "keys()/items()" be compatible with the iterator proposal?
It would be extending the iterator to return pairs... this might
be a bit divergent...
| On the other hand, x.items() has to be implemented by each and every
| list-like classes. Their subclasses need to override them whenever
| necessary.
I like x.keys() and x.items() since it is _currently_ how
maps work already... extending the behavior to lists makes
sense to me and I don't have to "think about it".
As for the implementation difficulty, a generic "mix-in"
implementation can be provided; or even more simply one
could just assign self.items = items; self.keys = keys.
| So else being equal, I'd prefer items(x) to x.items().
I don't see why we can't have our cake and eat it too. ;)
| Question: Which is better name: keys or indices? items or indexed?
I have a strong opinion here... use the same function
names as used by a map! A list is really just a map
such that the domain is limited to positive integers.
Having a different set of names isn't a good idea.
| I think the latter two (indices, indexed) are more descriptive of sequence
| types. This is especially true if one ever want to use a seqdict type.
Ick.
| Question: Which name is better: iterkeys or xkeys?
Which ever works for maps; the decision should be shared
as a list is a special map with a limited domain.
Best,
Clark
More information about the Python-list
mailing list