Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #78563
| From | Mark Lawrence <breamoreboy@yahoo.co.uk> |
|---|---|
| Subject | Re: Clearing globals in CPython |
| Date | 2014-10-01 19:17 +0100 |
| References | <542c2527$0$13013$c3e8da3$5496439d@news.astraweb.com> |
| Newsgroups | comp.lang.python |
| Message-ID | <mailman.14529.1412187452.18130.python-list@python.org> (permalink) |
On 01/10/2014 17:00, Steven D'Aprano wrote: > Out of curiosity, I ran: > > globals().clear() > > in the interactive interpreter. It broke much more than I expected! > Built-ins were no longer available, and import stopped working. > > I expected that global variables would be all lost, but built-ins would > remain, since they don't live in the global namespace. I was wrong: > >>>> globals().clear() >>>> x = len([]) > Traceback (most recent call last): > File "<stdin>", line 1, in <module> > NameError: name 'len' is not defined > > The reason, I think, is that CPython has a special __builtins__ global > variable that the interpreter uses to access built-in functions. What > *appears* to be happening is that if that __builtins__ global is missing, > CPython can not access the built-ins. > > (Supporting this interpretation: IronPython, like CPython, has a > __builtins__ global, and behaves the same when the globals() are cleared. > Jython, which does not have a __builtins__ global, does not.) > > Well that's okay, I thought to myself, I'll just import the built-in > functions: > >>>> from builtins import len # use '__builtin__' in Python 2 > Traceback (most recent call last): > File "<stdin>", line 1, in <module> > ImportError: __import__ not found > > Oops. > > So, with no built-ins available, import no longer works. That makes things > rather tricky. > > Obviously the easiest way to recover is to exit the current session and > restart it, but as a challenge, can we recover from this state? > > We obviously need a Restart Command. -- My fellow Pythonistas, ask not what our language can do for you, ask what you can do for our language. Mark Lawrence
Back to comp.lang.python | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
Clearing globals in CPython Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2014-10-02 02:00 +1000
Re: Clearing globals in CPython Chris Angelico <rosuav@gmail.com> - 2014-10-02 02:14 +1000
Re: Clearing globals in CPython Chris Kaynor <ckaynor@zindagigames.com> - 2014-10-01 10:08 -0700
Re: Clearing globals in CPython Peter Otten <__peter__@web.de> - 2014-10-01 19:24 +0200
Re: Clearing globals in CPython Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2014-10-02 18:11 +1000
Re: Clearing globals in CPython Mark Lawrence <breamoreboy@yahoo.co.uk> - 2014-10-02 09:30 +0100
Re: Clearing globals in CPython jakubo <jakubo@example.org> - 2014-10-06 09:05 +0000
Re: Clearing globals in CPython Steven D'Aprano <steve@pearwood.info> - 2014-10-06 09:08 +0000
Re: Clearing globals in CPython Chris Angelico <rosuav@gmail.com> - 2014-10-06 20:30 +1100
Re: Clearing globals in CPython Mark Lawrence <breamoreboy@yahoo.co.uk> - 2014-10-01 19:17 +0100
Re: Clearing globals in CPython Terry Reedy <tjreedy@udel.edu> - 2014-10-01 14:44 -0400
Re: Clearing globals in CPython Marco Buttu <marco.buttu@gmail.com> - 2014-10-01 18:39 +0200
Re: Clearing globals in CPython Grant Edwards <invalid@invalid.invalid> - 2014-10-02 14:07 +0000
Re: Clearing globals in CPython Chris Angelico <rosuav@gmail.com> - 2014-10-03 00:12 +1000
Re: Clearing globals in CPython Stefan Behnel <stefan_ml@behnel.de> - 2014-10-03 07:26 +0200
csiph-web