Why this apparent assymetry in set operations?

Neil Cerutti mr.cerutti at gmail.com
Tue Jan 15 16:26:58 CET 2008


On Jan 15, 2008 10:10 AM,  <skip at pobox.com> wrote:
>
> I've noticed that I can update() a set with a list but I can't extend a set
> with a list using the |= assignment operator.
>
>     >>> s = set()
>     >>> s.update([1,2,3])
>     >>> s
>     set([1, 2, 3])
>     >>> s |= [4,5,6]
>     Traceback (most recent call last):
>       File "<stdin>", line 1, in <module>
>     TypeError: unsupported operand type(s) for |=: 'set' and 'list'
>     >>> s |= set([4,5,6])
>     >>> s
>     set([1, 2, 3, 4, 5, 6])
>
> Why is that?  Doesn't the |= operator essentially map to an update() call?

No, according to 3.7 Set Types, s | t maps to s.union(t).

-- 
Neil Cerutti <mr.cerutti+python at gmail.com>



More information about the Python-list mailing list