operators and datatypes for sets

Anton Vredegoor anton at vredegoor.doge.nl
Sat May 5 10:33:24 EDT 2001


On Fri, 27 Apr 2001 10:34:23 +0200, "Alex Martelli"
<aleaxit at yahoo.com> wrote:

>"Anton Vredegoor" <anton at vredegoor.doge.nl> wrote 

>> http://home.hccnet.nl/a.vredegoor/universe/universe.html

>I think it would be handy for a 'set' operand to "coerce"
>the other operand (if the latter is a sequence) to 'set',

<snip>

>You can use the __coerce__ special method, or do it in
>each operator (and its __rXXX__ version for completeness).

Sorry I can't use __coerce__ , the python reference forbids it:

"Finally, sequence types should implement addition (meaning
concatenation) and multiplication (meaning repetition) by defining the
methods __add__(), __radd__(), __iadd__(), __mul__(), __rmul__() and
__imul__() described below; they should not define __coerce__() or
other numerical operators."

But then again I have used some operators already, so I will look into
it someday. More example code would be welcome.

>Also .has_subset.  Implementation is trivial, just a
>small factory function that you might as well expose:
>
>def make_set(set_or_sequence):
>    if isinstance(set_or_sequence, set):
>        return set_or_sequence
>    else:
>        return set(set_or_sequence)
>
>and just call this appropriately on "other operand".

Thanks for this factory function idea, I have used it in an other way
than you suggest here though. When creating a new set I first check
whether a set with the same items already exists in the universe and
if so I just return the existing set instead of creating a new set.
This makes __contains__ a lot easier to implement.

Also the module now supports sets in sets. Strangely enough very
little coding was necessary since it was mostly already in there. It
took me a long detour in acyclic digraphs with sinks having multiple
incoming arcs before some mathematician friend pointed it out to me
that there was nothing wrong with the program, since it could already
do what I wanted because the __repr__  function was recursive. On the
other hand, for what I wanted to do this was unnecessary. It was a
non-problem, so the problem was solved. I just misinterpreted the
output. I guess your earlier remarks about confusing __contains__ with
has_subset were even more important than I thought.

>Sounder implementation migh be to accept classes to
>you not-known that "implement a set interface" or
>"are adaptable to a set protocol" rather than just
>do isinstance(), but unless/until PEPs 245/246 or some
>variants thereof should prevail, it's never very
>sure how best to test... maybe here, since you're
>always using other.vector, you could be satisfied
>with testing for that with hasattr().
>

For the moment set(list) can be used to coerce a list into a set.
Feedback on the module would be very welcome, especially if someone
could provide an example of it behaving in an unexpected way. The url
is the same as before.

Anton.



More information about the Python-list mailing list