[Python-Dev] Retrieve an arbitrary element from a set without removing it

Georg Brandl g.brandl at gmx.net
Tue Oct 27 18:40:45 CET 2009


Chris Bergstresser schrieb:
> On Tue, Oct 27, 2009 at 11:06 AM, Georg Brandl <g.brandl at gmx.net> wrote:
>> Sorry to nitpick, but there is no list.get().
> 
>    No?  How ... odd.  I guess it wouldn't have come up, but I was sure
> there was a .get method which took an optional default parameter if
> the index didn't exist, mirroring the dict method.  Still, I think my
> point stands--it's a clear extrapolation from the existing dict.get().

I don't see that.  Both dict.get() and your hypothetical list.get() are
variants of the [] subscription operator, i.e. __getitem__, that have a
default value, defaulting to None.  The [] operator retrieves an element
from the object at the specified "position", be it dict key, list index
or some other abstract position.

Contrary to that, sets don't support subscript access, there is no notion
of a value at a specified position, so I would find the set.get() naming
confusing.

Georg

-- 
Thus spake the Lord: Thou shalt indent with four spaces. No more, no less.
Four shall be the number of spaces thou shalt indent, and the number of thy
indenting shall be four. Eight shalt thou not indent, nor either indent thou
two, excepting that thou then proceed to four. Tabs are right out.



More information about the Python-Dev mailing list