相关疑难解决方法(0)

如何制作一系列功能装饰器?

如何在Python中创建两个装饰器来执行以下操作?

@makebold
@makeitalic
def say():
   return "Hello"
Run Code Online (Sandbox Code Playgroud)

...应该返回:

"<b><i>Hello</i></b>"
Run Code Online (Sandbox Code Playgroud)

我不是试图HTML在一个真实的应用程序中这样做 - 只是试图了解装饰器和装饰器链是如何工作的.

python decorator python-decorators

2647
推荐指数
17
解决办法
49万
查看次数

带参数的装饰器?

我有装饰器传递变量'insurance_mode'的问题.我会通过以下装饰器声明来做到这一点:

 @execute_complete_reservation(True)
 def test_booking_gta_object(self):
     self.test_select_gta_object()
Run Code Online (Sandbox Code Playgroud)

但不幸的是,这种说法不起作用.也许有更好的方法可以解决这个问题.

def execute_complete_reservation(test_case,insurance_mode):
    def inner_function(self,*args,**kwargs):
        self.test_create_qsf_query()
        test_case(self,*args,**kwargs)
        self.test_select_room_option()
        if insurance_mode:
            self.test_accept_insurance_crosseling()
        else:
            self.test_decline_insurance_crosseling()
        self.test_configure_pax_details()
        self.test_configure_payer_details

    return inner_function
Run Code Online (Sandbox Code Playgroud)

python decorator

361
推荐指数
11
解决办法
21万
查看次数

如何创建一个可以使用或不使用参数的Python装饰器?

我想创建一个可以与参数一起使用的Python装饰器:

@redirect_output("somewhere.log")
def foo():
    ....
Run Code Online (Sandbox Code Playgroud)

或没有它们(例如,默认情况下将输出重定向到stderr):

@redirect_output
def foo():
    ....
Run Code Online (Sandbox Code Playgroud)

那可能吗?

请注意,我不是在寻找重定向输出问题的不同解决方案,它只是我想要实现的语法的一个示例.

python decorator

71
推荐指数
6
解决办法
1万
查看次数

标签 统计

decorator ×3

python ×3

python-decorators ×1