Clearing globals in CPython

Mark Lawrence breamoreboy at yahoo.co.uk
Thu Oct 2 10:30:20 CEST 2014


On 02/10/2014 09:11, Steven D'Aprano wrote:
> Peter Otten wrote:
>
>> Steven D'Aprano wrote:
>
>>> 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?
>>
>> $ python3
>> Python 3.4.0 (default, Apr 11 2014, 13:05:11)
>> [GCC 4.8.2] on linux
>> Type "help", "copyright", "credits" or "license" for more information.
>>>>> globals().clear()
>>>>> import that
>> Traceback (most recent call last):
>>    File "<stdin>", line 1, in <module>
>> ImportError: __import__ not found
>>>>> object = 1 .__class__.__bases__[0]
>>>>> Quitter = [c for c in object.__subclasses__() if c.__name__ ==
>> "Quitter"][0]
>>>>> __builtins__ = Quitter.__call__.__globals__["__builtins__"]
>>>>> import this
>> The Zen of Python, by Tim Peters
>
> Nicely done!
>

Other than the Zen of Python what has Tim Peters ever done for us? 
Apart from...

-- 
My fellow Pythonistas, ask not what our language can do for you, ask
what you can do for our language.

Mark Lawrence




More information about the Python-list mailing list