## 188Re: [python-iter] Digest Number 21

Expand Messages
• Mar 15, 2001
• 0 Attachment
> > May it really be meaningless?
>
> Yes. An easier example: a set. Elements of the set are neither keys
> nor values.
>
> If you claim that they are keys or values, please define keys or
> values of a collection.

Well -- I'm sure there are no keys in a set, but I think it's natural
to see them as values, no? Isn't a set a collection of values?
And, in general, if the collection has some structure, it may also
allow you to access these values by supplying a key/index...

Of course, mathematically, both lists and dictionaries are
mappings, i.e. sets of pairs... I would say that the "values" in
the case of a mapping is the range, while the keys are the
domain. To generalise to non-mappings, i.e. plain sets, I guess
one has to choose whether it is more natural to view the
elements as keys or values. Since it seems plain weird to have
keys without values, but not to have values without keys,
I would think the latter is preferrable.

So, to return to my description in the previous mail; if you iterate
over the keys of x, then x[key] should give you the value. If this
is not possible, you are iterating over the values.

So, "for element in set" would clearly be iterating over the
values, since "set[element]" wouldn't work. Or if it did, then
you were in fact iterating over the keys, but I'm not sure what
set[element] would be...

So, to conclude, since we in fact from a statement

for x in collection:
dosomething(x)

can not conclude whether x is a key of a value, and this is
clearly interesting to know, I don't think it's meaningless
to request that there be some way of finding out.

Of course one might test collection[x] in s try-block, but that
seems a bit awkward.

--

Magnus Lie Hetland http://www.hetland.org

"Reality is what refuses to disappear when you stop
believing in it" -- Philip K. Dick
• Show all 6 messages in this topic