Connecting Tech Pros Worldwide Help | Site Map

semantics of the |= operator

 
LinkBack Thread Tools Search this Thread
  #1  
Old August 21st, 2008, 03:45 PM
akva
Guest
 
Posts: n/a
Default semantics of the |= operator

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

  #2  
Old August 21st, 2008, 03:45 PM
Diez B. Roggisch
Guest
 
Posts: n/a
Default Re: semantics of the |= operator

akva wrote:
Quote:
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.
Yes. That's the exact purpose of the in-place operators when they deal with
mutable objects. What else did you expect?

Now of course this behaves different:

def foo(x):
x += 1

y = 100
foo(y)
print y

will result in y still being 100, as the value 101 that is bound to x inside
foo is *not* re-bound to the name y in the outer scope. This is because
numbers (and strings and tuples) are immutables, and thus the operation
won't modify the 100 in place to become 101, instead return a new object.

Diez

  #3  
Old August 21st, 2008, 09:25 PM
Terry Reedy
Guest
 
Posts: n/a
Default Re: semantics of the |= operator



akva wrote:
Quote:
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
The manual explicitly specifies that mutable objects may implement the
operation part of operation-assignments by updating in place -- so that
the object assigned is a mutated version of the original rather than a
new object.

The value equivalency only applies in the namespace in which the
statement appears.
Quote:
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])
Put the print inside foo and you will get set([1,2,10]), as with bar.
Quote:
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.
This has nothing to do with being inside a function.

tjr

  #4  
Old August 22nd, 2008, 07:45 AM
akva
Guest
 
Posts: n/a
Default Re: semantics of the |= operator

thanks all,
Quote:
>Yes. That's the exact purpose of the in-place operators when they deal with
>mutable objects. What else did you expect?
well, frankly I expected a |= b to mean exactly the same as a = a | b
regardless of the object type.
Quote:
The manual explicitly specifies that mutable objects may implement the
operation part of operation-assignments by updating in place *-- so that
the object assigned is a mutated version of the original rather than a
new object.
could you please refer me a link where this is specified? I couldn't
find it
in python documentation
Quote:
This has nothing to do with being inside a function.
yes, I know. maybe my example is a bit too verbose. could avoid using
functions.
But you got my main point. I found it somewhat counter-intuitive that
operation-assignments
can modify value in place.

Regards,
Vitali
  #5  
Old August 22nd, 2008, 08:05 AM
Fredrik Lundh
Guest
 
Posts: n/a
Default Re: semantics of the |= operator

akva wrote:
Quote:
could you please refer me a link where this is specified? I couldn't
find it in python documentation
http://docs.python.org/ref/augassign.html

"An augmented assignment expression like x += 1 can be rewritten as x =
x + 1 to achieve a similar, but not exactly equal effect. In the
augmented version, x is only evaluated once. Also, when possible, the
actual operation is performed in-place, meaning that rather than
creating a new object and assigning that to the target, the old object
is modified instead."

</F>

  #6  
Old August 22nd, 2008, 05:45 PM
Peter Pearson
Guest
 
Posts: n/a
Default Re: semantics of the |= operator

On Fri, 22 Aug 2008 00:35:31 -0700 (PDT), akva <kiruta@gmail.comwrote:
Quote:
>
well, frankly I expected a |= b to mean exactly the same as a = a | b
regardless of the object type.
So did I. I'm glad your post called this to my attention; I
recently told my kid exactly that wrong thing.


--
To email me, substitute nowhere->spamcop, invalid->net.
  #7  
Old August 24th, 2008, 08:25 PM
akva
Guest
 
Posts: n/a
Default Re: semantics of the |= operator

thanks everybody.
 

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Popular Articles

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over 220,662 network members.