"Stupid Python Tricks" explores ways to abuse Python features for fun and profit. Stupid Python Tricks may well be diametrically opposed to best practices. Don your peril-sensitive sunglasses before proceeding.
You know how when you have a lot of globals and it's a pain to modify them, because you have to declare the globals you want to modify in each function using the global statement? Yeah, me neither. After all, globals are a poor programming practice! And I never engage in those...
However, for educational porpoises, or at least didactic dolphins, here's a short hack that takes advantage of the fact that Python globals can be accessed, but not modified, without declaring them global. Basically, what we do is replace the built-in globals() function with an object that lets you read and set globals via attribute access. Instead of doing this:
def my_func(a, b, c):
ans = a + b + c
You can instead do:
def my_func(a, b, c):
globals.ans = a + b + c
This works fine because you aren't re-binding any global names; instead, you are merely mutating an existing object. Or so Python believes... bwahahaha! While we didn't save any lines of code in this short example, imagine if we had dozens of functions that used globals. We'd save literally dozens of global statements! And we could dine on spaghetti code for weeks.
Calling this object, i.e. globals(), continues to return the modules's globals dictionary, just like before, thanks to a __call__() method on the class.
Without further ado, then, here's globals.py:
from inspect import currentframe
from sys import modules
def __getattribute__(self, key, currentframe=currentframe):
pass # if we raise NameError here we get 2 err msgs
raise NameError("global name '%s' is not defined" % key)
def __setattr__(self, key, value, currentframe=currentframe):
currentframe().f_back.f_globals[key] = value
def __call__(self, currentframe=currentframe):
globals = Globals()
modules[__name__] = globals # so other modules will use it
import globals # make sure that worked
# simple test
globals.answer = 42
assert globals.answer is answer
This short bit of code demonstrates more than one dirty, dirty hack. inspect.currentframe()
is used to allow us to manipulate the globals of the module
containing the function it's called from, rather than the globals of its
own module. We assign into sys.modules to replace the globals module object with our Globals class instance so that you only need import globals, not from globals import globals. Since it's a functional replacement for the built-in globals() function, we could stick it into the __builtins__ namespace so other modules would get it even without importing it, but even I have my limits!