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

Question:

So I tried the “evil” thing Ned Deily mentioned in his answer here. Now I have that the type True is now always False. How would I reverse this within the interactive window?

Thing to not do:

True = False

Since True has now been completely overridden with False, there doesn’t seem to be an obvious way to back-track. Is there a module that True comes from that I can do something like:

True = <'module'>.True
Asked By: horta

||

Answers:

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

>>> True = False
>>> True
False
>>> del True
>>> True
True
>>>
Answered By: user2555451

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
Answered By: 101

Just do this:

True = bool(1)

Or, because booleans are essentially integers:

True = 1
Answered By: Loovjo

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.

Answered By: simonwo

Another way:

>>> True = 1 == 1
>>> False = 1 == 2
Answered By: Roberto Bonvallet

Solutions that use no object literals but are as durable as 1 == 1. Of course, you can define False once True is defined, so I’ll supply solutions as half pairs.

def f(): pass
class A(): pass
True = not f()
False = A != A
False = not (lambda:_).__gt__(_)
True = not (lambda:_).__doc__
Answered By: PythonNut
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.