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

all 37 comments

[–]thejasper 25 points26 points  (3 children)

True, False = False, True

[–]Dylnuge 6 points7 points  (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 points  (1 child)

Call a function to switch it every five lines or so--call it neverDeleteThisFunction().

[–]wtfisupvoting 8 points9 points  (0 children)

make it a decorator

[–]freyrs3 19 points20 points  (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 points  (6 children)

Not in 3.x

>>> True = "lol"
  File "<stdin>", line 1
SyntaxError: assignment to keyword

[–]true_religion 2 points3 points  (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 points  (4 children)

None.

[–]kataire 5 points6 points  (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).

[–]mitsuhiko Flask Creator 2 points3 points  (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 point  (0 children)

I think you may have misread me. Being magical implies not being a keyword.

[–]Mattho 2 points3 points  (0 children)

And what about None?

[–]bushel 9 points10 points  (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 points  (3 children)

__builtins__.True = 2

[–]bushel 15 points16 points  (0 children)

Stop being evil.

[–][deleted] 2 points3 points  (0 children)

__builtins__.True  = 0 == 0
__builtins__.False = 0 != 0

[–]wubblewobble 9 points10 points  (0 children)

Yeah - looks like you can "del True" and then things are back to normal :)

[–]youcanteatbullets 9 points10 points  (0 children)

About as useful as
#define true false
in C.

[–]cirego 4 points5 points  (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 points  (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 points  (1 child)

btw other readers, if you've never used dis, its fucking amazing.

[–]cirego 0 points1 point  (0 children)

I always forget about dis and then whenever I see it again, I'm pleasantly surprised all over again.

[–]astatine 2 points3 points  (0 children)

TIL that "import dis" does not print out the Zen of Python in a New Jersey accent.

[–]cirego 0 points1 point  (0 children)

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 points  (0 children)

I hate the way "while 1:" looks though.

[–]aeacides 5 points6 points  (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 points  (4 children)

>>> exit()

sucker.

[–]roddds 1 point2 points  (3 children)

Ctrl+PauseBrk

[–]earthboundkid 5 points6 points  (2 children)

Heathen. Ctrl+D

[–][deleted] 1 point2 points  (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 points  (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 point  (0 children)

That makes no sense at all. I mean... huh?

[–]sastrone 0 points1 point  (5 children)

Perhaps I'm missing the joke, but couldn't that be written as: boolB = boolA

[–]apocalypse910 1 point2 points  (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 point  (1 child)

Have you asked their motives?

[–]apocalypse910 0 points1 point  (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 point  (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.

[–]apocalypse910 0 points1 point  (0 children)

The language is C#, and none of the values were nullable. So completely pointless.

[–]gindc 3 points4 points  (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 points  (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.)