python - Decorator args and kwargs returned within tuple -
i have following decorator , class.
def auth(fn): def check_headers(self): print 'checking headers...' #self.headers work done here def inner(self, *args, **kwargs): check_headers(self) fn(self, args, kwargs) return inner class worker(object): @auth def work(self, *args, **kwargs): print 'auth passed' print args print kwargs worker_obj = worker() worker_obj.work('arg', kw='kwarg')
which outputs :
> checking headers... > auth passed > (('arg',), {'kw': 'kwarg'}) > {}
but expecting :
> checking headers... > auth passed > ('arg',) > {'kw': 'kwarg'}
how come args/kwargs getting put in tuple when original method (work()) being run, post-decoration?
i know stripping down
def auth(fn): return fn
returns parameters correctly, need work on worker instance (self) before returning. surely missed decorators.
thanks!
when call fn(self, args, kwargs)
, passing 2 positional arguments: tuple of args
, , dict of kwargs
. if call work(1, x=2)
, call func(self, (1,), {'x': 2})
. expand origianl args , kwargs separate arguments, need do
fn(self, *args, **kwargs)
this mean when call work(1, x=2)
, call fn(self, 1, x=2)
.
you can see documentation on here.
Comments
Post a Comment