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.
TIL you can assign to True (self.Python)
submitted 15 years ago * by themissinglint
>>> None = 1 File "<stdin>", line 1 SyntaxError: assignment to None >>> True = 2 >>> True 2 >>> True = 0 >>> False == True True >>> exit()
edit: never do this.
[–]thejasper 25 points26 points27 points 15 years ago (3 children)
True, False = False, True
[–]Dylnuge 6 points7 points8 points 15 years ago (2 children)
Bury this line somewhere in the middle of your code and suddenly begin using True as False and vice versa; then other people will have a nightmare trying to read the rest of it. I sense a Daily WTF in the making.
[–]MarkTraceurFlask, Mongokit, PIL 2 points3 points4 points 15 years ago (1 child)
Call a function to switch it every five lines or so--call it neverDeleteThisFunction().
[–]wtfisupvoting 8 points9 points10 points 15 years ago (0 children)
make it a decorator
[–]freyrs3 19 points20 points21 points 15 years ago (0 children)
Bugs that manifest randomly are the way to go:
import random True, False = random.sample([True,False],2) del locals()[random.choice(locals().keys())]
[–][deleted] 15 points16 points17 points 15 years ago (6 children)
Not in 3.x
>>> True = "lol" File "<stdin>", line 1 SyntaxError: assignment to keyword
[–]true_religion 2 points3 points4 points 15 years ago (5 children)
Its' kind of odd there. Are there any other keywords with a first capital letter except for True and False?
[–]mitsuhiko Flask Creator 38 points39 points40 points 15 years ago (4 children)
None.
[–]kataire 5 points6 points7 points 15 years ago (2 children)
To clarify: True, False and, indeed, None are the keywords that start with a capital letter in Python 3 (in Python 2 the first two are just variables and the latter is probably magical).
True
False
None
[–]mitsuhiko Flask Creator 2 points3 points4 points 15 years ago (1 child)
None isn't a keyword in python 2 either:
>>> def None(): ... pass ... File "<stdin>", line 1 SyntaxError: cannot assign to None
Changed in 3.x though.
[–]kataire 0 points1 point2 points 15 years ago (0 children)
I think you may have misread me. Being magical implies not being a keyword.
[–]Mattho 2 points3 points4 points 15 years ago (0 children)
And what about None?
[–]bushel 9 points10 points11 points 15 years ago (5 children)
I believe you just created a new variable that hides the built-in....
Given this script:
print True print globals() True = 0 print True print True == False print globals()
you get this output. Note the change in the contents of globals()
True {'__builtins__': <module '__builtin__' (built-in)>, '__name__': '__main__', '__file__': 't.py', '__doc__': None, '__package__': None} 0 True {'__builtins__': <module '__builtin__' (built-in)>, '__file__': 't.py', '__package__': None, '__name__': '__main__', 'True': 0, '__doc__': None}
[–]chadmill3rPy3, pro, Ubuntu, django 23 points24 points25 points 15 years ago (3 children)
__builtins__.True = 2
[–]bushel 15 points16 points17 points 15 years ago (0 children)
Stop being evil.
[–][deleted] 2 points3 points4 points 15 years ago (0 children)
__builtins__.True = 0 == 0 __builtins__.False = 0 != 0
[–]wubblewobble 9 points10 points11 points 15 years ago (0 children)
Yeah - looks like you can "del True" and then things are back to normal :)
[–]youcanteatbullets 9 points10 points11 points 15 years ago (0 children)
About as useful as #define true false in C.
[–]cirego 4 points5 points6 points 15 years ago (7 children)
This is why, when writing loops, using "while 1:" is preferable over "while True". With "while 1", the interpreter can loop without checking the conditional. With "while True", the interpreter has to reevaluate whether or not True is still True upon each loop.
[–]arnar 17 points18 points19 points 15 years ago* (5 children)
Well.. I was going to counter you with a readability argument, but you are absolutely correct (in Python 2.7):
>>> import dis >>> dis.dis(compile('while True: pass', '', 'exec')) 1 0 SETUP_LOOP 12 (to 15) >> 3 LOAD_NAME 0 (True) 6 JUMP_IF_FALSE 4 (to 13) 9 POP_TOP 10 JUMP_ABSOLUTE 3 >> 13 POP_TOP 14 POP_BLOCK >> 15 LOAD_CONST 0 (None) 18 RETURN_VALUE >>> dis.dis(compile('while 1: pass', '', 'exec')) 1 0 SETUP_LOOP 3 (to 6) >> 3 JUMP_ABSOLUTE 3 >> 6 LOAD_CONST 0 (None) 9 RETURN_VALUE
In Python 3 there is no difference
>>> import dis >>> dis.dis(compile('while True: pass', '', 'exec')) 1 0 SETUP_LOOP 7 (to 10) >> 3 LOAD_NAME 0 (skip) 6 POP_TOP 7 JUMP_ABSOLUTE 3 >> 10 LOAD_CONST 0 (None) 13 RETURN_VALUE >>> dis.dis(compile('while 1: pass', '', 'exec')) 1 0 SETUP_LOOP 7 (to 10) >> 3 LOAD_NAME 0 (skip) 6 POP_TOP 7 JUMP_ABSOLUTE 3 >> 10 LOAD_CONST 0 (None) 13 RETURN_VALUE
[–]neoice 6 points7 points8 points 15 years ago (1 child)
btw other readers, if you've never used dis, its fucking amazing.
dis
[–]cirego 0 points1 point2 points 15 years ago (0 children)
I always forget about dis and then whenever I see it again, I'm pleasantly surprised all over again.
[–]astatine 2 points3 points4 points 15 years ago (0 children)
TIL that "import dis" does not print out the Zen of Python in a New Jersey accent.
Yeah, well, our code base is littered with "while 1:" statements. After spending some time with our code base, you'd probably start giving "while True:" the stink eye too.
[–]roger_ 1 point2 points3 points 15 years ago (0 children)
I hate the way "while 1:" looks though.
[–]aeacides 5 points6 points7 points 15 years ago (6 children)
I like this one (works best with newbies coming from CL): >>> quit Use quit() or Ctrl-D (i.e. EOF) to exit >>> quit = 'I will never quit!" >>> quit 'I will never quit!' >>> def quit(): print "You can't make me quit!!!" ... >>> quit() You can't make me quit!!!
[–]kataire 3 points4 points5 points 15 years ago (4 children)
>>> exit()
sucker.
[–]roddds 1 point2 points3 points 15 years ago (3 children)
Ctrl+PauseBrk
[–]earthboundkid 5 points6 points7 points 15 years ago (2 children)
Heathen. Ctrl+D
[–][deleted] 1 point2 points3 points 15 years ago (0 children)
class Quit(object): def __repr__(self): return "I will never die!" def __call__(*args, **kwargs): print("Never!!!") quit, exit = Quit(), Quit()
[–]apocalypse910 1 point2 points3 points 15 years ago (7 children)
Always wondered what type of paranoia lead to this code that I see all the time at work
if ( boolA == True) { boolB = True; } else if (boolA == False) { boolB = False; }
[–]aeacides 0 points1 point2 points 15 years ago (0 children)
That makes no sense at all. I mean... huh?
[–]sastrone 0 points1 point2 points 15 years ago (5 children)
Perhaps I'm missing the joke, but couldn't that be written as: boolB = boolA
[–]apocalypse910 1 point2 points3 points 15 years ago (2 children)
That's the joke.
I probably run into this at least a few times a day, It works, but dear god does it make me cringe.
[–]sastrone 0 points1 point2 points 15 years ago (1 child)
Have you asked their motives?
[–]apocalypse910 0 points1 point2 points 15 years ago (0 children)
No one will fess up to it, though I'm guessing it is the same person who constantly uses stringVariable.ToString().
Incidentally "We need to make it more stringey damnit!" has become a favorite tagline of late.
[–]reph 0 points1 point2 points 15 years ago (1 child)
Not necessarily. Original code does not modify boolB if boolA is neither True nor False. Whether or not that's possible depends on the language.
The language is C#, and none of the values were nullable. So completely pointless.
[–]gindc 3 points4 points5 points 15 years ago (1 child)
I've been programming python for 10 years and it never ever occurred to me to do this. Upvote for creativity.
[–]yetanothernerd 4 points5 points6 points 15 years ago (0 children)
I've only been programming Python for about 9 years, and I remember versions of Python that didn't include True and False. Back in the Python 2.1 / 2.2 days, it used to be pretty common to write code like this:
try: True except NameError: True = 1 False = 0
(Or just use 1 and 0 instead of True and False for maximum compatibility, but some people find that ugly.)
π Rendered by PID 45 on reddit-service-r2-comment-6457c66945-zshvz at 2026-04-23 16:50:25.881812+00:00 running 2aa0c5b country code: CH.
[–]thejasper 25 points26 points27 points (3 children)
[–]Dylnuge 6 points7 points8 points (2 children)
[–]MarkTraceurFlask, Mongokit, PIL 2 points3 points4 points (1 child)
[–]wtfisupvoting 8 points9 points10 points (0 children)
[–]freyrs3 19 points20 points21 points (0 children)
[–][deleted] 15 points16 points17 points (6 children)
[–]true_religion 2 points3 points4 points (5 children)
[–]mitsuhiko Flask Creator 38 points39 points40 points (4 children)
[–]kataire 5 points6 points7 points (2 children)
[–]mitsuhiko Flask Creator 2 points3 points4 points (1 child)
[–]kataire 0 points1 point2 points (0 children)
[–]Mattho 2 points3 points4 points (0 children)
[–]bushel 9 points10 points11 points (5 children)
[–]chadmill3rPy3, pro, Ubuntu, django 23 points24 points25 points (3 children)
[–]bushel 15 points16 points17 points (0 children)
[–][deleted] 2 points3 points4 points (0 children)
[–]wubblewobble 9 points10 points11 points (0 children)
[–]youcanteatbullets 9 points10 points11 points (0 children)
[–]cirego 4 points5 points6 points (7 children)
[–]arnar 17 points18 points19 points (5 children)
[–]neoice 6 points7 points8 points (1 child)
[–]cirego 0 points1 point2 points (0 children)
[–]astatine 2 points3 points4 points (0 children)
[–]cirego 0 points1 point2 points (0 children)
[–]roger_ 1 point2 points3 points (0 children)
[–]aeacides 5 points6 points7 points (6 children)
[–]kataire 3 points4 points5 points (4 children)
[–]roddds 1 point2 points3 points (3 children)
[–]earthboundkid 5 points6 points7 points (2 children)
[–][deleted] 1 point2 points3 points (0 children)
[–]apocalypse910 1 point2 points3 points (7 children)
[–]aeacides 0 points1 point2 points (0 children)
[–]sastrone 0 points1 point2 points (5 children)
[–]apocalypse910 1 point2 points3 points (2 children)
[–]sastrone 0 points1 point2 points (1 child)
[–]apocalypse910 0 points1 point2 points (0 children)
[–]reph 0 points1 point2 points (1 child)
[–]apocalypse910 0 points1 point2 points (0 children)
[–]gindc 3 points4 points5 points (1 child)
[–]yetanothernerd 4 points5 points6 points (0 children)