[Python-ideas] exclusively1, common, exclusively2 = set1 - set2, set1 & set2, set2 - set1

Joshua Landau joshua.landau.ws at gmail.com
Sun Jul 7 04:47:33 CEST 2013


On 7 July 2013 03:23, Steven D'Aprano <steve at pearwood.info> wrote:
> On 07/07/13 07:40, Joshua Landau wrote:
>
>> (I still don't get what people have against my version though. A 2-way
>> partition makes sense)
>
> For the record, your suggestion was to add a convenience method:
>
> set1.partition(set2) => (set1 - set2, set1 & set2)
>
> and then call it as a two-liner:
>
> only1, both = set1.partition(set2)
> only2 = set2 - set1
>
>
> instead of Paddy's suggestion (with the method name left unknown):
>
> only1, both, only2 = set1.???????(set2)
>
>
> I don't think much of your suggestion. It doesn't solve the stated use-case,
> where you want a three-way partition of two sets.

Only it does, as the problem was that it was "wasteful" to do the
extra passes over the list - which mine solves. If you really need a
convenience function for a three-way partition it makes sense to put
it in a function and call "partition3(set1, set2)".

It looks better as a function, too, as the arguments "act" on
each-other, rather than being unidirectional as the method-based
syntax implies.

> And I'm having difficulty
> in thinking of practical examples where I might only want two out of the
> three "Venn subsets".

I've already given one. When you have a set of <flags> and you only
want to deal with <these ones> and pass <the rest> to the next in the
chain, you'd want to do <flags>.partition(<these ones>). There are a
lot of circumstances of this sort.

Personally, I've no idea why you'd want all three. I'm willing,
though, to accept that you have a good reason.

> Since this is a convenience method, it doesn't add any
> new functionality, it needs to be *more* rather than *less* convenient than
> what it replaces.

I'm quite surprised you think it's less convenient than the default
method. The main reason it was brought up was because of the
"wastefulness". If it's really so bad to be forced to write a
function, given that the original efficiency problems are solved, may
I ask why we still have to write:

d = dict1.copy()
d.update(dict2)
func(d)

to pass a function the combination of two dictionaries (in my
experience a far more needed utility)? I think that ship has sailed
long ago¹.

¹ This may change soon if we get to do "func({**dict1, **dict2})", but
that's not the primary motive for the change so w/e.


More information about the Python-ideas mailing list