The Python Oracle

How do I undo True = False in python interactive mode?

--------------------------------------------------
Rise to the top 3% as a developer or hire one of them at Toptal: https://topt.al/25cXVn
--------------------------------------------------

Music by Eric Matyas
https://www.soundimage.org
Track title: Puzzling Curiosities

--

Chapters
00:00 How Do I Undo True = False In Python Interactive Mode?
00:34 Accepted Answer Score 137
00:46 Answer 2 Score 47
01:17 Answer 3 Score 11
01:30 Answer 4 Score 17
01:51 Thank you

--

Full question
https://stackoverflow.com/questions/3056...

--

Content licensed under CC BY-SA
https://meta.stackexchange.com/help/lice...

--

Tags
#python #boolean #python2x

#avk47



ACCEPTED ANSWER

Score 137


You can simply del your custom name to set it back to the default:

>>> True = False
>>> True
False
>>> del True
>>> True
True
>>>



ANSWER 2

Score 47


This works:

>>> True = False
>>> True
False
>>> True = not False
>>> True
True

but fails if False has been fiddled with as well. Therefore this is better:

>>> True = not None

as None cannot be reassigned.

These also evaluate to True regardless of whether True has been reassigned to False, 5, 'foo', None, etc:

>>> True = True == True   # fails if True = float('nan')
>>> True = True is True
>>> True = not True or not not True
>>> True = not not True if True else not True
>>> True = not 0



ANSWER 3

Score 17


For completeness: Kevin mentions that you could also fetch the real True from __builtins__:

>>> True = False
>>> True
False
>>> True = __builtins__.True
>>> True
True

But that True can also be overriden:

>>> __builtins__.True = False
>>> __builtins__.True
False

So better to go with one of the other options.




ANSWER 4

Score 11


Just do this:

True = bool(1)

Or, because booleans are essentially integers:

True = 1