473,395 Members | 2,151 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

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
Aug 21 '08 #1
6 988
akva wrote:
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

Aug 21 '08 #2


akva wrote:
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.
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.
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

Aug 21 '08 #3
thanks all,
>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.
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
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
Aug 22 '08 #4
akva wrote:
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>

Aug 22 '08 #5
On Fri, 22 Aug 2008 00:35:31 -0700 (PDT), akva <ki****@gmail.comwrote:
>
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.
Aug 22 '08 #6
thanks everybody.
Aug 24 '08 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: harold fellermann | last post by:
Hi all, I want to implement rich comparision in an extension class. Problem is I cannot find good documentation of the richcmpfunc semantics. Given the signature richcmpfunc compare(PyObject...
3
by: Grant Austin | last post by:
Hi all, I'm trying to overload the '==' operator for a class, Page, that I wrote so that I can use the STL List 'find' algorithm. I'm having difficulties getting a definition that g++ likes and...
4
by: David Rasmussen | last post by:
The problem comes up in all sorts of contexts. But here is an example: We want to model the basics of chess, maybe for making a chess program, or maybe just to make an interactive board or ... ...
1
by: bluekite2000 | last post by:
In other words Array<int,1> A(5), B(10); A = B(Range(0,4)); // Statement 1 Array<int,1> C = B(Range(0,4)); // Statement 2 I d like Statement 1 results in a portion of B's data...
21
by: Paul Steckler | last post by:
Here's some code that's giving me differing results, depending on the compiler. typedef foo { int A,B; } FOO; int main() {
9
by: Alvin Bruney | last post by:
The more knowledgable I get about this .net world, the more questions I have. ..NET uses pass by reference for all objects....uhhh I mean pass by value. (Couldn't resist this jab) Consider a...
13
by: Marc | last post by:
Hi, I've been lurking on clc for a few months now, and want to start by thanking the regulars here for opening my eyes to a whole new dimension of "knowing c". Considering I had never even...
12
by: terminator | last post by:
the following compiles unless the first line is uncommented . when I try to uncomment the first line I get: error : 'B &FM::operator ,(FM::mystruct<A>,B &)' : could not deduce template argument...
2
by: =?Utf-8?B?QU1lcmNlcg==?= | last post by:
I would like to define a structure or a class with an array field that behaves like a simple value-semantics variable. For example, I want something like public structure polynomial public a()...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.