I was working on my local python 2.7 interpreter to test some code logic. As, the command to close the interpreter is "exit()", I assigned a variable like,
exit = False
After trying out some calculations, when I wanted to quit out of my interpreter and gave
exit()
it threw following error,
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: 'bool' object is not callable
I know have overridden a system variable maybe but is it so easy to do that? And even If I did, when I tried to assign it to
exit = True
it keeps throwing me error like "bool object is not callable" So, what went wrong here?
I know have overridden a system variable maybe but is it so easy to do that?
What you’ve done is shadowed a builtin name with a global name.1 That’s perfectly legal to do, and in fact sometimes it’s useful. For example:
exit
in your module, sys
wouldn’t be allowed to define sys.exit
. And the same for io.open
, codecs.open
, and gzip.open
. And numpy.min
and pandas.apply
. And so on.future
and putting 3.x-style zip
, etc. into your globals.More generally, shadowing builtins with globals is the same thing as shadowing builtins, globals, or nonlocals with locals, so it would be pretty strange to ban one without banning the other.
More generally still, Python is designed around the “consenting adults” idea. It doesn’t go out of its way to protect you from everything that might possibly be used to shoot yourself in the foot. There are a handful of things that are so rarely useful and so commonly cause trouble (like reassigning None
) that it was worth adding a bit of code to prevent them, but for the most part, you’re free to do things even if it’s not always a good idea to do them.
And even If I did, when I tried to assign it to
exit = True
it keeps throwing me error like "bool object is not callable" So, what went wrong here?
That doesn’t undo what you did. It just means the builtin is now shadowed by a global with the value True
, instead of one with the value False
. And True
is no more callable than False
.
If you want to undo the shadowing, just delete the shadowing global:
del exit
1. Python has a few different meanings of “builtin” that don’t quite perfectly overlap. The exit
function is not compiled as a builtin, and isn’t even part of the builtins
module until it gets monkeypatched in by site
, but it is accessed by builtin-namespace lookup, which is the meaning I’m talking about here.