In my project I have to stick to Python 2.5 (Google App Engine). Somewhere in the application (actually a framework), I have to keep track which variables are defined and in which order they are defined, in other words I would like to intercept whenever an assignment operator is processed.
Using Python 3, I would define a metaclass M with a __prepare__
method that returns an intelligent dictionary that keeps track of when it is accessed. Then I just have to execute everything inside a class statement with metaclass M.
Is there any way to emulate this in Python 2.5?
EXAMPLE of what I would like to achieve
With the metaclass approach of Python 3, I could implement variables that work like references, for example M could be so that
# y is a callable
class C(metaclass=M):
x = ref(y)
x = 1
would be equivalent (up to the creation of C) with y(1)
, i.e. the first assignment to a variable in C's dictionary by a black-box ref
function creates this variable. Further assignments simply call the parameter of the ref function.