PEP 218 Re: ANN: set-0.1 module available

Denis S. Otkidach ods at fep.ru
Fri May 17 10:17:24 EDT 2002


On Fri, 17 May 2002, Roman Suzi wrote:

RS> > We often need a collection that is extended step-by-step
RS> avoiding
RS> > duplicates.  Certainly we can use immutable sets:
RS> >
RS> > s = {-}
RS> > for item in some_sequence:
RS> >     if is_good(item):
RS> >         s &= {item}  # create new set if it's immutable or
RS> just
RS> >                      # add otherwise
RS>
RS> I think, that is
RS>
RS> s = set(filter(is_good, some_sequence))
RS>
RS> is for.

This is OK but inefficient if intermediate list is very large
with many duplicates.  But there are many situations where no
such functional solution (or it's not obvious):

def func(item):
    ...
    result = {item}
    result &= func(other_item)  # other_item can be already in
                                # result
    return result

and so on.

RS> I hope for mutable sets so. I even think, sets could use
RS> a generalised member function:
RS>
RS> N in Integers
RS>
RS> f in SecureFunctions

Python doen't support interfaces.  Interface of sequences is
defined in documentation only.  Should we define set in
documentation the same way as object with __contains__?

RS> where membership function is substituted. So, set built-in
RS> type is more
RS> about setting a protocol for set type. At first,
RS> implementation doesn't
RS> matter much.






More information about the Python-list mailing list