[Python-ideas] get method for sets?

Greg Ewing greg.ewing at canterbury.ac.nz
Thu May 17 01:51:49 CEST 2012


Bruce Leban wrote:
> Then maybe you should be using a different data structure than a set. 
> Maybe set_with_same_tag that declares that constraint and can enforce 
> the constraint if you want.

Another class of use cases is where you know that the
set contains only one element, and you want to find out
what that element is.

I encountered one of these in my recent PyWeek game
entry. I have a set of selected units, and commands
that can be applied to them. Some commands can only
be used on a single unit at a time, so there are places
in the code where there can only be one element in the
set.

Using a separate SetWithOnlyOneElement type in that
case would be tedious and unnecessary. I don't need to
enforce the constraint; I know it's satisfied because
I wouldn't have ended up at that point in the code if
it wasn't.

-- 
Greg



More information about the Python-ideas mailing list