Python decorator pattern: reducing code duplication involving inner functions and functools.wraps -


i'm seeing lot of documentation on stackoverflow , elsewhere how write python decorators. typically recommend using functools.wraps , (potentially multiple) inner functions. complex if want decorator can either called or without brackets, i.e. @foo or @foo(bar).

for example, this stackoverflow question , various answers give lot of insight how this. however, appear rather surprisingly complicated (either conditional logic or deeper nesting of functions) seems conceptually simple. biggest concern that, in examples given, >50% of code unrelated particular decorator's behavior , shared boilerplate among decorators written using pattern.

the real-world examples looking @ the fabric project's decorators.py , some of django project's various decorator.py instances. seems bit strange me there lot of boilerplate code unrelated actual intent.

i understand why want use functools.wraps code maintainability reasons, seems overly complex. there way dry and/or encapsulate code? seems part care perspective of user code actual body of generated inner function. how go writing remaining boilerplate once , reusing forever?

thanks in advance!

the decorator, wrapt, , decorators packages on pypi abstract out of boilerplate code beyond functools.wraps provides.

if want use both @foo , @foo(args), incur complexity in code, since foo(func) , foo(args)(func) different ways wrap function. agree pete @foo() clearer in case.


Comments

Popular posts from this blog

facebook - android ACTION_SEND to share with specific application only -

python - Creating a new virtualenv gives a permissions error -

javascript - cocos2d-js draw circle not instantly -