Re: Why this apparent assymetry in set operations?

2008-01-16 Thread Colin J. Williams
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.

-- 
http://mail.python.org/mailman/listinfo/python-list


Why this apparent assymetry in set operations?

2008-01-15 Thread skip

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?

Skip

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Why this apparent assymetry in set operations?

2008-01-15 Thread Colin J. Williams
Neil Cerutti wrote:
 On Jan 15, 2008 10:10 AM,  [EMAIL PROTECTED] 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 modifies to handle any 
iterable on the RHS.

Colin W.

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Why this apparent assymetry in set operations?

2008-01-15 Thread Neil Cerutti
On Jan 15, 2008 10:10 AM,  [EMAIL PROTECTED] 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 [EMAIL PROTECTED]
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Why this apparent assymetry in set operations?

2008-01-15 Thread Colin J. Williams
Colin J. Williams wrote:
 Neil Cerutti wrote:
 On Jan 15, 2008 10:10 AM,  [EMAIL PROTECTED] 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.

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Why this apparent assymetry in set operations?

2008-01-15 Thread Skip Montanaro
 If the RHS is a set then it works OK:

Yup, I understand that.  Would be kind of bad if it didn't. ;-)

 It could be modifies to handle any 
 iterable on the RHS.

That's my question.  Why does it work in one place but not everywhere?

Skip



-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Why this apparent assymetry in set operations?

2008-01-15 Thread Skip Montanaro
  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).

I was asking about the |= assignment operator which according to the
docs *does* map to the update method.

Skip




-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Why this apparent assymetry in set operations?

2008-01-15 Thread Sergio Correia
Both of you are correct.

 x = set([1,2,3])
 y = set([4,5])
 x |= y
 x
set([1, 2, 3, 4, 5])


On Jan 15, 2008 11:07 AM, Skip Montanaro [EMAIL PROTECTED] wrote:
   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).

 I was asking about the |= assignment operator which according to the
 docs *does* map to the update method.

 Skip





 --
 http://mail.python.org/mailman/listinfo/python-list

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Why this apparent assymetry in set operations?

2008-01-15 Thread Neil Cerutti
On Jan 15, 2008 11:07 AM, Skip Montanaro [EMAIL PROTECTED] wrote:
   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).

 I was asking about the |= assignment operator which according to the
 docs *does* map to the update method.

Oops!  You're right. That's surprising.

This inconsistency is due to the c implementation.

Internally, |= maps to the c function set_ior, while .update(...) maps
to set_update.

Both eventually call set_update_internal, which works fine when the
operand is not a set.

But set_ior specifically punts non-sets before calling set_update_internal.

So this is a bug in set_update or in set_ior. They can't both be
right.

-- 
Neil Cerutti [EMAIL PROTECTED]
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Why this apparent assymetry in set operations?

2008-01-15 Thread Chris M
On Jan 15, 11:51 am, Neil Cerutti [EMAIL PROTECTED] wrote:

 So this is a bug in set_update or in set_ior. They can't both be
 right.


It's not a bug.

Note, the non-operator versions of union(), intersection(),
difference(), and symmetric_difference(), issubset(), and issuperset()
methods will accept any iterable as an argument. In contrast, their
operator based counterparts require their arguments to be sets. This
precludes error-prone constructions like set('abc')  'cbs' in favor
of the more readable set('abc').intersection('cbs').
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Why this apparent assymetry in set operations?

2008-01-15 Thread Paul Rubin
Chris M [EMAIL PROTECTED] writes:
 precludes error-prone constructions like set('abc')  'cbs' in favor
 of the more readable set('abc').intersection('cbs').

set('abc')  set('cbs')
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Why this apparent assymetry in set operations?

2008-01-15 Thread Neil Cerutti
On Jan 15, 2008 12:06 PM, Chris M [EMAIL PROTECTED] wrote:
 On Jan 15, 11:51 am, Neil Cerutti [EMAIL PROTECTED] wrote:
 
  So this is a bug in set_update or in set_ior. They can't both be
  right.
 

 It's not a bug.

 Note, the non-operator versions of union(), intersection(),
 difference(), and symmetric_difference(), issubset(), and issuperset()
 methods will accept any iterable as an argument. In contrast, their
 operator based counterparts require their arguments to be sets. This
 precludes error-prone constructions like set('abc')  'cbs' in favor
 of the more readable set('abc').intersection('cbs').

Thanks. That neatly answers Skip's question, assuming he buys the
putative error pronicity. The docs say the design is based on lessons
learned from the sets module, so that also explains why it's different
from the module version, as well.

-- 
Neil Cerutti [EMAIL PROTECTED]
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Why this apparent assymetry in set operations?

2008-01-15 Thread Steven D'Aprano
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
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Why this apparent assymetry in set operations?

2008-01-15 Thread John Machin
On Jan 16, 3:25 am, Colin J. Williams [EMAIL PROTECTED] 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
-- 
http://mail.python.org/mailman/listinfo/python-list