[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.python

Re: Why this apparent assymetry in set operations?

Colin J. Williams

1/15/2008 4:25:00 PM

Colin J. Williams wrote:
> Neil Cerutti wrote:
>> On Jan 15, 2008 10:10 AM, <skip@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).
>>
> If the RHS is a set then it works OK:
>
> *** Python 2.5.1 (r251:54863, Apr 18
> 2007, 08:51:08) [MSC v.1310 32 bit
> (Intel)] on win32. ***
>>>> import sets
>>>> s1= sets.Set([2, 4, 5])
> Set([2, 4, 5])
>>>> s1= sets.Set([2, 4, 5])
>>>> s2= sets.Set([4, 5, 6])
>>>> s1|s2
> Set([2, 4, 5, 6])
>>>> s1|=s2
>>>> s1
> Set([2, 4, 5, 6])
>
> It could be modified to handle any
> iterable on the RHS.
>
> Colin W.
>

I'm sorry, there appears to be a bug:
# tSet.py
import sets
s1= sets.Set([1, 2, 3])
s1.union_update([3, 4,5])
print(s1)
s2= sets.Set([6, 7, 8])
s1 |+ s2 # This fails:
exceptions.TypeError: bad operand type
for unary +: 'Set'
print s1

Colin W.

3 Answers

Steven D'Aprano

1/15/2008 9:39:00 PM

0

On Tue, 15 Jan 2008 11:25:25 -0500, Colin J. Williams wrote:

> I'm sorry, there appears to be a bug: # tSet.py
> import sets
> s1= sets.Set([1, 2, 3])
> s1.union_update([3, 4,5])
> print(s1)
> s2= sets.Set([6, 7, 8])
> s1 |+ s2 # This fails:
> exceptions.TypeError: bad operand type for unary +: 'Set'

And so it should fail. Did you mean |= instead of |+ ?


--
Steven

John Machin

1/15/2008 9:59:00 PM

0

On Jan 16, 3:25 am, "Colin J. Williams" <c...@sympatico.ca> wrote:
Colin W.
>
> I'm sorry, there appears to be a bug:

There is, but but not where you think it is :-)

> # tSet.py
> import sets

[not the bug] Earlier evidence is that you are using 2.5.1; why import
sets??

> s1= sets.Set([1, 2, 3])
> s1.union_update([3, 4,5])
> print(s1)
> s2= sets.Set([6, 7, 8])
> s1 |+ s2 # This fails:
> exceptions.TypeError: bad operand type
> for unary +: 'Set'

Try reading and understanding the exception message ... "unary +" as
in the syntactically equivalent expression
s1 | +s2

HTH,
John

Colin J. Williams

1/16/2008 1:14:00 PM

0

Steven D'Aprano wrote:
> On Tue, 15 Jan 2008 11:25:25 -0500, Colin J. Williams wrote:
>
>> I'm sorry, there appears to be a bug: # tSet.py
>> import sets
>> s1= sets.Set([1, 2, 3])
>> s1.union_update([3, 4,5])
>> print(s1)
>> s2= sets.Set([6, 7, 8])
>> s1 |+ s2 # This fails:
>> exceptions.TypeError: bad operand type for unary +: 'Set'
>
> And so it should fail. Did you mean |= instead of |+ ?
>
>
Thanks, keyboard error.

Colin W.