decorators - would be nice if...

Diez B. Roggisch deets at nospam.web.de
Tue Jul 14 18:03:03 EDT 2009


Ken Seehart schrieb:
> Almost every time I use decorators, I find myself wishing I had access
> to the local namespace of the context from which the decorator is
> executed.  In practice, decorator is being applied to a method, so the
> namespace in question would be the dictionary of the class being created.

You can access the instance.

def decorator(method):
     def _d(self, *args, **kwargs):
         print self.__dict__
         return method(self, *args, **kwargs)

     return _d

class Foo(object):

     @decorator
     def bar(self, a, b):
         print "bar"


f = Foo()

f.bar(1, 2)

So what exactly it is you are missing? The method's locals()?

And could you explain *why* you are missing this?


Diez



More information about the Python-list mailing list