dinamically altering a function

vegetax vegeta.z at gmail.com
Sat Mar 12 15:58:47 EST 2005


I i need a decorator that adds a local variable in the function it
decorates, probably related with nested scopes, for example:

def dec(func):
    def wrapper(obj = None):
        if not obj : obj = Obj()
  <bind obj to func>
 return func()

    return wrapper()

@dec()
def fun(b):
    obj.desc = 'marked'
    obj.b = b
    return obj

so the call to fun : fun(obj = myobj,'b argument')
or fun('b argument')

So the function "fun" assumes it has an obj instance and other instance
objects computed by the decorator, this decorator will be like a generic
factory for this kind of functions,which depends on the decorator to work.


    




More information about the Python-list mailing list