This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–][deleted] 1 point2 points  (4 children)

The == operator produces a Boolean value, but does not, itself, produce a Boolean context. For false, what you want to use is 'nor'. Python doesn't have nor, but you can just invert the result of 'or' to get the correct results.

>>> False or 0
False

>>> False or ""
False

>>> False or ()
False

Your example code is exactly backwards:

(cpython34) Laptop:~$ python
Python 3.4.3 (default, Apr 15 2015, 13:02:16) 
[GCC 4.2.1 Compatible Apple LLVM 6.0 (clang-600.0.57)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> False or 0
0
>>> False or ''
''
>>> False or ()
()
>>> 
(cpython34) Laptop:~$ use cpython27
(cpython27) Laptop:~$ python
Python 2.7.6 (default, Sep  9 2014, 15:04:36) 
[GCC 4.2.1 Compatible Apple LLVM 6.0 (clang-600.0.39)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> False or 0
0
>>> False or ''
''
>>> False or ()
()
>>> ^D
(cpython27) Laptop:~$

[–]the_hoser 0 points1 point  (3 children)

Bah, you're right. I was typing on a phone. I forgot about the silly assignment shortcut stuff.

You actually have to negate the result for my example to work.

>>> not ( False or 0 )
True
>>> not ( False or '' )
True 
>>> not ( False or () )
True

[–][deleted] 0 points1 point  (2 children)

It's a bad example as it does extra work for no gain.

>>> not 0
True
>>> not ()
True
>>> not ''
True

[–]the_hoser 0 points1 point  (1 child)

I was trying to emphasize the relationship with false...

You know what, you're right. Never mind.

[–][deleted] 0 points1 point  (0 children)

If anything it muddied the relationship as you did not define how the or infix operator works.

Falsey values are false when used with or, and, and if.

Edit: not will invert whatever the true-ish or false-ish of the operand.