source: http://stackoverflow.com/questions/308999/what-does-functools-wraps-do
In [74]: f(3)
f was called
Out[74]: 12
------------------------------------------------------------------------------------------------------------------
how to use @decorator as @overrider from java.
source: http://stackoverflow.com/questions/1167617/in-python-how-do-i-indicate-im-overriding-a-method
--------------------------------------------------------------------------------------------------------------------
source: http://stackoverflow.com/questions/739654/how-can-i-make-a-chain-of-function-decorators-in-python?rq=1
-----------------------------------------------------------------------------------------------------------------
source: http://stackoverflow.com/questions/739654/how-can-i-make-a-chain-of-function-decorators-in-python?rq=1
When you use a decorator, you're replacing one function with another. In other words, if you have a decorator
The left colume below is the exactly same as the right one.
The left colume below is the exactly same as the right one.
|
|
In [73]: f??
Type: function
String Form:<function with_logging at 0x7fa6d574faa0>
File: /home/jinuine/<ipython-input-65-cf67ff536232>
Definition: f(*args, **kwargs)
Source:
def with_logging(*args, **kwargs):
print func.__name__ + " was called"
return func(*args, **kwargs)
In [74]: f(3)
f was called
Out[74]: 12
------------------------------------------------------------------------------------------------------------------
how to use @decorator as @overrider from java.
source: http://stackoverflow.com/questions/1167617/in-python-how-do-i-indicate-im-overriding-a-method
def overrides(interface_class): def overrider(*args, **kwargs): print args, kwargs method = args[0] assert(method.__name__ in dir(interface_class)) return method return overrider class MySuperInterface(object): def my_method(self, name): print 'hello world! %s' % (name) class ConcreteImplementer(MySuperInterface): @overrides(MySuperInterface) def my_method(self): print 'hello kitty!, %s' % (name)
위의 클래스 선언시 아래가 찍힘.
(<function my_method at 0x251c1b8>,) {}
|
def overrides(interface_class): def overrider(method): assert(method.__name__ in dir(interface_class)) return method return overrider class MySuperInterface(object): def my_method(self, name): print 'hello world! %s' % (name) class ConcreteImplementer(MySuperInterface): @overrides(MySuperInterface) def my_method(self): print 'hello kitty!, %s' % (name) |
--------------------------------------------------------------------------------------------------------------------
source: http://stackoverflow.com/questions/739654/how-can-i-make-a-chain-of-function-decorators-in-python?rq=1
How can I make a chain of function decorators in Python?
def makebold(fn):
def wrapped():
return "<b>" + fn() + "</b>"
return wrapped
def makeitalic(fn):
def wrapped():
return "<i>" + fn() + "</i>"
return wrapped
@makebold
@makeitalic
def hello():
return "hello world"
print hello() ## returns <b><i>hello world</i></b>
-----------------------------------------------------------------------------------------------------------------
source: http://stackoverflow.com/questions/739654/how-can-i-make-a-chain-of-function-decorators-in-python?rq=1
Alternatively, you could write a factory function which return a decorator which wraps the return value of the decorated function in a tag passed to the factory function. For example:
from functools import wraps
def wrap_in_tag(tag):
def factory(func):
@wraps(func)
def decorator():
return '<%(tag)s>%(rv)s</%(tag)s>' % (
{'tag': tag, 'rv': func()})
return decorator
return factory
This enables you to write:
@wrap_in_tag('b')
@wrap_in_tag('i')
def say():
return 'hello'
or
makebold = wrap_in_tag('b')
makeitalic = wrap_in_tag('i')
@makebold
@makeitalic
def say():
return 'hello'
Personally I would have written the decorator somewhat differently:
from functools import wraps
def wrap_in_tag(tag):
def factory(func):
@wraps(func)
def decorator(val):
return func('<%(tag)s>%(val)s</%(tag)s>' %
{'tag': tag, 'val': val})
return decorator
return factory
which would yield:
@wrap_in_tag('b')
@wrap_in_tag('i')
def say(val):
return val
say('hello')
Don't forget the construction for which decorator syntax is a shorthand:
say = wrap_in_tag('b')(wrap_in_tag('i')(say)))
from functools import wraps
def wrap_in_tag(tag):
def factory(func):
@wraps(func)
def decorator():
return '<%(tag)s>%(rv)s</%(tag)s>' % (
{'tag': tag, 'rv': func()})
return decorator
return factory
@wrap_in_tag('b')
@wrap_in_tag('i')
def say():
return 'hello'
makebold = wrap_in_tag('b')
makeitalic = wrap_in_tag('i')
@makebold
@makeitalic
def say():
return 'hello'
from functools import wraps
def wrap_in_tag(tag):
def factory(func):
@wraps(func)
def decorator(val):
return func('<%(tag)s>%(val)s</%(tag)s>' %
{'tag': tag, 'val': val})
return decorator
return factory
@wrap_in_tag('b')
@wrap_in_tag('i')
def say(val):
return val
say('hello')
say = wrap_in_tag('b')(wrap_in_tag('i')(say)))
댓글
댓글 쓰기