Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #78600 > unrolled thread
| Started by | Didymus <lynto28@gmail.com> |
|---|---|
| First post | 2014-10-02 06:24 -0700 |
| Last post | 2014-10-06 14:37 -0700 |
| Articles | 5 — 5 participants |
Back to article view | Back to comp.lang.python
Assignment Operators? Didymus <lynto28@gmail.com> - 2014-10-02 06:24 -0700
Re: Assignment Operators? Chris Angelico <rosuav@gmail.com> - 2014-10-02 23:27 +1000
Re: Assignment Operators? Grant Edwards <invalid@invalid.invalid> - 2014-10-02 14:23 +0000
Re: Assignment Operators? Ian Kelly <ian.g.kelly@gmail.com> - 2014-10-02 10:00 -0600
Re: Assignment Operators? sohcahtoa82@gmail.com - 2014-10-06 14:37 -0700
| From | Didymus <lynto28@gmail.com> |
|---|---|
| Date | 2014-10-02 06:24 -0700 |
| Subject | Assignment Operators? |
| Message-ID | <9669f493-cfa4-4a84-9f4a-ce3009926ca3@googlegroups.com> |
Hi All,
I was wondering if someone could explain an assignment operator that I'm seeing in some code. As an example:
>>> errors = False
>>> errors |= 3
>>> errors
3
>>> errors |= 4
>>> errors
7
The '|=' operator, I read should be like a = a | b, but this appears to add the two numbers as long as it's more than the previous:
>>> errors |= 5
>>> errors
7
Is there anywhere I can read up more on this and the other assignment operators and what/how they work. I got this off
http://rgruet.free.fr/PQR27/PQR2.7.html
I'm using:
% python
Python 2.7.5 (default, Sep 25 2014, 13:57:38)
[GCC 4.8.3 20140911 (Red Hat 4.8.3-7)] on linux2
Thanks for any help in advance.
Tom
[toc] | [next] | [standalone]
| From | Chris Angelico <rosuav@gmail.com> |
|---|---|
| Date | 2014-10-02 23:27 +1000 |
| Message-ID | <mailman.14550.1412256465.18130.python-list@python.org> |
| In reply to | #78600 |
On Thu, Oct 2, 2014 at 11:24 PM, Didymus <lynto28@gmail.com> wrote: >>>> errors = False >>>> errors |= 3 >>>> errors > 3 >>>> errors |= 4 >>>> errors > 7 > > The '|=' operator, I read should be like a = a | b, but this appears to add the two numbers as long as it's more than the previous: > >>>> errors |= 5 >>>> errors > 7 It is indeed (mostly) equivalent to "a = a | b". Do you understand what bitwise 'or' does? When you use False there, it's equivalent to zero. Everything after that is straight-forward bitwise or. http://en.wikipedia.org/wiki/Bitwise_operation#OR ChrisA
[toc] | [prev] | [next] | [standalone]
| From | Grant Edwards <invalid@invalid.invalid> |
|---|---|
| Date | 2014-10-02 14:23 +0000 |
| Message-ID | <m0jn4b$b9s$1@reader1.panix.com> |
| In reply to | #78601 |
On 2014-10-02, Chris Angelico <rosuav@gmail.com> wrote:
> On Thu, Oct 2, 2014 at 11:24 PM, Didymus <lynto28@gmail.com> wrote:
>>>>> errors = False
>>>>> errors |= 3
>>>>> errors
>> 3
>>>>> errors |= 4
>>>>> errors
>> 7
[...]
> When you use False there, it's equivalent to zero.
Why is that, you ask? [Or should, anyway]
The fact that booleans when found in an arithmetic context are
auto-magically coerced into integers with values 0,1 is a rather
unpythonic wart which has it's historical roots in the time when
Python didn't have a boolean type. People used integers instead and a
lot of code bound the names True and False to the integers 1 and 0.
When the boolean type was introduced it was decided that backwards
compatibility with that practice was important. This resulted in two
pragmatic but somewhat "impure" decisions:
1) In Python 2, True and False are not keywords, they're just global
keywords that come pre-bound to the boolean singleton values of
'true' and 'false'. You can re-bind them to other objects:
Python 2.7.7 (default, Aug 20 2014, 11:41:28)
>>> False = 3.14159
>>> if False: print "False"
...
False
>>>
>>> import math
>>> math.cos(False)
-0.9999999999964793
2) When used in an arithmetic context, boolean values would be
converted into integer values 0,1.
When Python 3 came out, 1) was dropped and True/False were "promoted"
to keywords. But, 2) is still the case.
--
Grant Edwards grant.b.edwards Yow! I guess you guys got
at BIG MUSCLES from doing too
gmail.com much STUDYING!
[toc] | [prev] | [next] | [standalone]
| From | Ian Kelly <ian.g.kelly@gmail.com> |
|---|---|
| Date | 2014-10-02 10:00 -0600 |
| Message-ID | <mailman.14556.1412265676.18130.python-list@python.org> |
| In reply to | #78600 |
On Thu, Oct 2, 2014 at 7:24 AM, Didymus <lynto28@gmail.com> wrote:
> The '|=' operator, I read should be like a = a | b, but this appears to add the two numbers as long as it's more than the previous:
Note that:
a = a or b
and:
a = a | b
are different operations. It sounds like you're probably looking for
the former, not the latter.
[toc] | [prev] | [next] | [standalone]
| From | sohcahtoa82@gmail.com |
|---|---|
| Date | 2014-10-06 14:37 -0700 |
| Message-ID | <f1280a31-7b52-4e65-a5d4-2b9862c7bca0@googlegroups.com> |
| In reply to | #78600 |
On Thursday, October 2, 2014 6:24:37 AM UTC-7, Didymus wrote: > Hi All, > > > > I was wondering if someone could explain an assignment operator that I'm seeing in some code. As an example: > > > > >>> errors = False > > >>> errors |= 3 > > >>> errors > > 3 > > >>> errors |= 4 > > >>> errors > > 7 > > > > The '|=' operator, I read should be like a = a | b, but this appears to add the two numbers as long as it's more than the previous: > > > > >>> errors |= 5 > > >>> errors > > 7 > > > > Is there anywhere I can read up more on this and the other assignment operators and what/how they work. I got this off > > > > http://rgruet.free.fr/PQR27/PQR2.7.html > > > > > > I'm using: > > % python > > Python 2.7.5 (default, Sep 25 2014, 13:57:38) > > [GCC 4.8.3 20140911 (Red Hat 4.8.3-7)] on linux2 > > > > > > Thanks for any help in advance. > > > > Tom As others have said, the | operator is a bit-wise 'or', as opposed to a boolean 'or'. False, when evaluated as an integer, converts to 0. So False | 3 is equal to 3, and so your errors variable is now 3. On the next step, you did | again with 4. Now, | didn't ADD 3 and 4 like you think it did. It did a bit-wise or. 3 in binary is 11. 4 in binary is 100. OR them together and you get 111, which is 7. The fact that the two numbers you chose have the same result whether you add them or 'or' them is a coincidence. 5 in binary is 101. When you OR that with 111, you get 111, which is still 7. If you're confused by this, read up on the binary number system and AND/OR/XOR/NAND logic. -DJ
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.python
csiph-web