89

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
Community
  • 1
  • 1
horta
  • 1,110
  • 8
  • 17
  • 21
    All this does is create a local variable named `True`, which hides the builtin constant. The "real" `True` is unaffected, and can still be created e.g. with `(1 == 1)` or the `bool()` function. It also lives in `__builtins__.True`. – Kevin May 31 '15 at 23:27
  • 15
    Note that this is not possible in Python 3, which treats `True` and `False` as keywords. – Lambda Fairy May 31 '15 at 23:53
  • I think you can still use `True = not False` or in this case also `True = not True` to recover. Otherwise, as stated in an answer below, use `del True` to recover the old value. – Simon Hessner Aug 08 '18 at 09:07

6 Answers6

137

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

>>> True = False
>>> True
False
>>> del True
>>> True
True
>>>
  • 4
    So the `True = False` command actually creates a new, local name that shadows the keyword, rather than actually replacing a variable? And this just cleans it up? – jpmc26 Jun 01 '15 at 19:18
  • 10
    @jpmc26 - Yes, that is correct. Note however that `True` and `False` are not keywords in Python 2.x; they are builtins. That is why this is possible. In Python 3.x, where `True` and `False` *are* keywords, `True = False` would raise a syntax error because you cannot assign to a keyword. –  Jun 01 '15 at 19:21
  • 5
    Interesting to note that this fails if `__builtins__.True` has been changed, as hinted at in the answer from @simonwo. – 101 Jun 01 '15 at 21:51
  • 7
    @figs - Yea, `del` will not work if you've modified `__builtins__`. In that case, it would be best to do `True = 1 == 1` like Roberto Bonvallet proposed. Of course, if you are already going so far as to modify `__builtins__`, then you could also do `del __builtins__` and really ruin the interpreter. At some point, it might be best to just restart it instead of trying to repair it piecemeal. :) –  Jun 01 '15 at 22:13
  • @iCodez del assignment – PyRulez Jun 02 '15 at 01:34
  • `del` works to recover other builtins too, `list` being a common one to accidentally shadow when mucking around. – Nick T Jun 02 '15 at 03:46
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
101
  • 8,514
  • 6
  • 43
  • 69
37

Another way:

>>> True = 1 == 1
>>> False = 1 == 2
Roberto Bonvallet
  • 31,943
  • 5
  • 40
  • 57
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.

simonwo
  • 3,171
  • 2
  • 19
  • 28
11

Just do this:

True = bool(1)

Or, because booleans are essentially integers:

True = 1
Loovjo
  • 534
  • 8
  • 23
2

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__
PythonNut
  • 6,182
  • 1
  • 25
  • 41