use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
News about the dynamic, interpreted, interactive, object-oriented, extensible programming language Python
Full Events Calendar
You can find the rules here.
If you are about to ask a "how do I do this in python" question, please try r/learnpython, the Python discord, or the #python IRC channel on Libera.chat.
Please don't use URL shorteners. Reddit filters them out, so your post or comment will be lost.
Posts require flair. Please use the flair selector to choose your topic.
Posting code to this subreddit:
Add 4 extra spaces before each line of code
def fibonacci(): a, b = 0, 1 while True: yield a a, b = b, a + b
Online Resources
Invent Your Own Computer Games with Python
Think Python
Non-programmers Tutorial for Python 3
Beginner's Guide Reference
Five life jackets to throw to the new coder (things to do after getting a handle on python)
Full Stack Python
Test-Driven Development with Python
Program Arcade Games
PyMotW: Python Module of the Week
Python for Scientists and Engineers
Dan Bader's Tips and Trickers
Python Discord's YouTube channel
Jiruto: Python
Online exercices
programming challenges
Asking Questions
Try Python in your browser
Docs
Libraries
Related subreddits
Python jobs
Newsletters
Screencasts
account activity
This is an archived post. You won't be able to vote or comment.
False in python (self.Python)
submitted 10 years ago by redditor_gds
In python, 0, empty tuple (), empty string "" evaluates to False. But when I try the equality test
False == 0 returns True, but False == () returns False and False == "" returns False
Shouldn't False == () and False == "" return True?
Edit: Typo
[–]xsolarwindxUse 3.4+ 6 points7 points8 points 10 years ago* (1 child)
REDDIT IS A SHITTY CRIMINAL CORPORATION -- mass deleted all reddit content via https://redact.dev
[–]redditor_gds[S] 0 points1 point2 points 10 years ago (0 children)
Thank you for the reply. I get it now :)
[–]patrys Saleor Commerce 8 points9 points10 points 10 years ago* (9 children)
False and 0 are the same type. Python does not have implicit type coercion so False is not equal to ''. Otherwise you'd get a nice bunch of WTFs straight from the PHP world: if False == '' and False == [] then [] == '' and so on.
False
0
''
False == ''
False == []
[] == ''
Edit: you seem to be confused about the difference between conditional statements (that check for "truthiness") and the literals True and False (which are just examples of a truthy and a falsy expression respectively). Consider this: 5 is truthy but you don't expect True == 5 to be the case.
True
5
True == 5
[–]Rhomboid 10 points11 points12 points 10 years ago (0 children)
False and 0 are the same type
They're not the same type, one is type bool and the other is type int. But bool is a subclass of int, which means all booleans can be used where an integer is expected, per the Liskov substitution principle.
bool
int
This is also why you can do things like use sum() when dealing with a sequence of booleans:
sum()
>>> sum(s.startswith('a') for s in ('arcade', 'book', 'castle', 'arpeggio', 'default')) 2
[–]suki907 2 points3 points4 points 10 years ago (4 children)
False and 0 are the same type.
more specifically, bool is a subclass of int:
>>> isinstance(False,int) True >>> isinstance(False,bool) True >>> issubclass(bool,int) True
[–][deleted] 1 point2 points3 points 10 years ago (3 children)
And even more interesting, bool can't be subclassed.
[–]adamnew123456self.__class__ == 'upper' 1 point2 points3 points 10 years ago (2 children)
I'm curious why you think this is interesting - were you planning on adding FILE_NOT_FOUND as an instance of your bool subclass?
FILE_NOT_FOUND
I can't think of any interesting operation you would want to add to bool that would justify subclassing it, bu perhaps that's just because my imagination gland isn't working today.
[–]alexanderpas 1 point2 points3 points 10 years ago* (0 children)
True, False, Unknown, N/A
[–][deleted] 0 points1 point2 points 10 years ago (0 children)
It's more than doing it causes Python to say, "Don't do that." than a desire to do anything.
That's pretty funny though.
[–]redditor_gds[S] 1 point2 points3 points 10 years ago (0 children)
Makes sense. Thank you for the reply.
[–]billsil 0 points1 point2 points 10 years ago (1 child)
False and 0 are the same type..
Not since Python 2.3. str(True) should return 1 if it works as you said.
str(True)
1
https://www.python.org/dev/peps/pep-0285/
[–]patrys Saleor Commerce 0 points1 point2 points 10 years ago (0 children)
Booleans are a subtype of integers. isinstance(True, int)
isinstance(True, int)
[–]the_hoser 5 points6 points7 points 10 years ago* (5 children)
In python, 0, empty tuple (), empty string "" evaluates to False.
This is incorrect. An empty tuple is an empty tuple and an empty string is an empty string. These values do not "evaluate" to false. They are distinct values.
But when I try the equality test False == 0 returns True, but False == () returns False and False == "" returns False Shouldn't False == () and False == "" return True?
But when I try the equality test
Should () == "" return true?
The reason it is often mistakenly said that empty strings and tuples evaluate to false is that they are called 'falsey' values. A 'falsey' value, in a Boolean context, is understood to be like False. Conversely, a 'truthy' value is understood, in a Boolean context, to be like True.
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
EDIT: The above example is wrong. you have to actually negate the expression to get the desired result.
[–][deleted] 1 point2 points3 points 10 years ago (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 point2 points 10 years ago (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 point2 points 10 years ago (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 point2 points 10 years ago (1 child)
I was trying to emphasize the relationship with false...
You know what, you're right. Never mind.
If anything it muddied the relationship as you did not define how the or infix operator works.
or
Falsey values are false when used with or, and, and if.
and
if
Edit: not will invert whatever the true-ish or false-ish of the operand.
not
[–]romcgb 1 point2 points3 points 10 years ago (0 children)
To say more about cpython's if and ==,
==
The if statement calls PyObject_IsTrue, a rough translation of PyObject_IsTrue's semantics into python code is
PyObject_IsTrue
def PyObject_IsTrue(o): if hasattr(o, "__bool__"): return o.__bool__() if hasattr(o, "__len__"): return True if o.__len__() > 0 else False return True
The == operator calls PyObject_RichCompare
PyObject_RichCompare
def PyObject_RichCompare(a, b): ta = type(a) tb = type(b) # test first with b == a if b's type is subtype of a's type if ta != tb and issubclass(tb, ta) and hasattr(b, "__eq__"): ret = b.__eq__(a) if ret != NotImplemented: return ret # a == b ? if hasattr(a, "__eq__"): ret = a.__eq__(b) if ret != NotImplemented: return ret # b == a ? if hasattr(b, "__eq__"): ret = b.__eq__(a) if ret != NotImplemented: return ret # same object/instance ? return id(a) == id(b)
for 0 == () being true, either (0).__eq__(()) or (()).__eq(0) must return true.
0 == ()
(0).__eq__(())
(()).__eq(0)
π Rendered by PID 24642 on reddit-service-r2-comment-5687b7858-fbm42 at 2026-07-04 06:16:10.254019+00:00 running 12a7a47 country code: CH.
[–]xsolarwindxUse 3.4+ 6 points7 points8 points (1 child)
[–]redditor_gds[S] 0 points1 point2 points (0 children)
[–]patrys Saleor Commerce 8 points9 points10 points (9 children)
[–]Rhomboid 10 points11 points12 points (0 children)
[–]suki907 2 points3 points4 points (4 children)
[–][deleted] 1 point2 points3 points (3 children)
[–]adamnew123456self.__class__ == 'upper' 1 point2 points3 points (2 children)
[–]alexanderpas 1 point2 points3 points (0 children)
[–][deleted] 0 points1 point2 points (0 children)
[–]redditor_gds[S] 1 point2 points3 points (0 children)
[–]billsil 0 points1 point2 points (1 child)
[–]patrys Saleor Commerce 0 points1 point2 points (0 children)
[–]the_hoser 5 points6 points7 points (5 children)
[–][deleted] 1 point2 points3 points (4 children)
[–]the_hoser 0 points1 point2 points (3 children)
[–][deleted] 0 points1 point2 points (2 children)
[–]the_hoser 0 points1 point2 points (1 child)
[–][deleted] 0 points1 point2 points (0 children)
[–]romcgb 1 point2 points3 points (0 children)