semantics of the |= operator

akva kiruta at gmail.com
Thu Aug 21 17:37:14 CEST 2008


Hi All,

what's the exact semantics of the |= operator in python?
It seems that a |= d is not always equivalent to a = a | d

For example let's consider the following code:

def foo(s):
   s = s | set([10])

def bar(s):
   s |= set([10])

s = set([1,2])

foo(s)
print s # prints set([1, 2])

bar(s)
print s # prints set([1, 2, 10])

So it appears that inside bar function the |= operator modifies the
value of s in place rather than creates a new value.
I'm using Python 2.5

Thanks everybody,
Vitali



More information about the Python-list mailing list