hack to auto-define variables?

Alex the_brain at mit.edu
Thu Oct 12 19:17:17 EDT 2000


> If the variable needs to be in a base class, then serious re-editing
> is necessary.
> 
> I would, however, like to add something to the semantics requested:
> I'd like it to generate a log entry everytime it does this.  So, say
> for example:
> 
> >>> x
> *** Variable `x' implicitly declared in __main__, line ??
> None

In the example you use to justify this travesty, you can do what you
describe with __getattr__, if your heart's set on it.

class Base_travesty:

    def __getattr__(self, attr):

        setattr(self, attr, None)
        return None

t = Base_travesty()

assert t.x == None
t.x = 1
assert t.x == 1

You could extend this with code that stored a traceback each time the
__getattr__ method was invoked, too.

Alex.

-- 
Speak softly but carry a big carrot.




More information about the Python-list mailing list