我该如何在Python中创建两个装饰器以实现以下功能?
@make_bold
@make_italic
def say():
return "Hello"
调用 say()
应该返回:
"<b><i>Hello</i></b>"
我该如何在Python中创建两个装饰器以实现以下功能?
@make_bold
@make_italic
def say():
return "Hello"
调用 say()
应该返回:
"<b><i>Hello</i></b>"
@lambda func: (lambda *variable: '<b>' + func(*variable) + '</b>')
@lambda func: (lambda *variable: '<i>' + func(*variable) + '</i>')
def say():
return "Hello"
print(say())
@make_bold := lambda func: (lambda *variable: '<b>' + func(*variable) + '</b>')
@make_italic := lambda func: (lambda *variable: '<i>' + func(*variable) + '</i>')
def say():
return "Hello"
@make_bold
@make_italic
def say2():
return "World"
print(say())
print(say2())
How do we make two decorators in Python that would do the following?
@make_bold @make_italic def say(): return "Hello"
Calling
say()
should return"<b><i>Hello</i></b>"
make_bold
from functools import update_wrapper
class make_bold:
def __new__(cls, kallable):
instance = super().__new__(cls)
instance = update_wrapper(instance, kallable)
return instance
def __init__(self, kallable):
self._kallable = kallable
self._file = sys.stdout
def __call__(self, *args, **kwargs):
# `iret` ...... initial return value
# `oret` ...... output return value
iret = self._kallable(*args, **kwargs)
oret = "<b>" + r + "</b>"
def __getattr__(self, attrname:str):
return getattr(self._kallable, attrname)
make_italic
from functools import update_wrapper
class make_italic:
def __new__(cls, kallable):
instance = super().__new__(cls)
instance = update_wrapper(instance, kallable)
return instance
def __init__(self, kallable):
self._kallable = kallable
self._file = sys.stdout
def __call__(self, *args, **kwargs):
# `iret` ...... initial return value
# `oret` ...... output return value
iret = self._kallable(*args, **kwargs)
ret = "".join(str(x) for x in iret)
oret = "<i>" + ret + "</i>"
def __getattr__(self, attrname:str):
return getattr(self._kallable, attrname)
make_italic
函数中添加了一行额外的代码,用于修改被包装函数返回的值。ret = "".join(str(x) for x in iret)
ABOUT... ret = "".join(str(x) for x in iret)
+--------------+---------------------------+----------+
| non-standard | input | output |
| notation | | |
| for | | |
| input | | |
| type | | |
+--------------+---------------------------+----------+
| string | 'howdy' | 'howdy' |
| tuple<char> | ('h', 'o', 'w', 'd', 'y') | 'howdy' |
| list<char> | ['h', 'o', 'w', 'd', 'y'] | 'howdy' |
| list<string> | ['ho', 'wdy'] | 'howdy' |
| list<int> | [1, 2, 3, 456] | '123456' |
+--------------+---------------------------+----------+