相关疑难解决方法(0)

带参数的装饰器?

我有装饰器传递变量'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中的Decorator类

我想构造用作装饰器的类,原则如下:

  1. 应该可以在top off 1函数上堆叠多个这样的类装饰器.
  2. 生成的函数名称指针应该与没有装饰器的同一函数无法区分,可以保存它的类型/类.
  3. 除非装饰者实际要求,否则对装饰器进行排序不应该是相关的.IE浏览器.独立装饰者可以按任何顺序应用.

这是一个Django项目,我正在处理的具体情况现在该方法需要2个装饰器,并显示为普通的python函数:

@AccessCheck
@AutoTemplate
def view(request, item_id) {}
Run Code Online (Sandbox Code Playgroud)

@AutoTemplate更改了函数,以便它不返回HttpResponse,而只返回一个字典以供在上下文中使用.使用RequestContext,并从方法名称和模块推断模板名称.

@AccessCheck根据item_id添加对用户的附加检查.

我猜这只是为了让构造函数正确并复制适当的属性,但这些属性是什么?

以下装饰器不会像我描述的那样工作:

class NullDecl (object):
    def __init__ (self, func):
        self.func = func
    def __call__ (self, * args):
        return self.func (*args)
Run Code Online (Sandbox Code Playgroud)

如以下代码所示:

@NullDecl
@NullDecl
def decorated():
    pass

def pure():
    pass

# results in set(['func_closure', 'func_dict', '__get__', 'func_name',
# 'func_defaults', '__name__', 'func_code', 'func_doc', 'func_globals'])
print set(dir(pure)) - set(dir(decorated));
Run Code Online (Sandbox Code Playgroud)

此外,尝试添加"打印FUNC.名称在NullDecl构造函数,它会为第一个装饰工作,而不是第二个" -正如名字将会丢失.

精致的eduffy的回答有点,而且看起来效果很好:

class NullDecl (object):
    def __init__ (self, func):
        self.func = func
        for n …
Run Code Online (Sandbox Code Playgroud)

python decorator

38
推荐指数
3
解决办法
2万
查看次数

带参数的 Python3“重复”装饰器:@repeat(n)

我看过(很多)带有和不带参数的装饰器的教程和片段,包括我认为是规范答案的那两个:带有参数的装饰器带有 @ 语法的 python 装饰器参数,但我不明白为什么我的代码出现错误。

以下代码位于文件中decorators.py

#!/usr/bin/env python3
# -*- coding: utf-8 -*-

"""
Description: decorators
"""
import functools

def repeat(nbrTimes=2):
    '''
    Define parametrized decorator with arguments
    Default nbr of repeats is 2
    '''
    def real_repeat(func):
        """
        Repeats execution 'nbrTimes' times
        """
        @functools.wraps(func)
        def wrapper_repeat(*args, **kwargs):
            while nbrTimes != 0:
                nbrTimes -= 1
                return func(*args, **kwargs)
        return wrapper_repeat
    return real_repeat
Run Code Online (Sandbox Code Playgroud)

我从语法检查器得到的第一个警告是nbrTimes“未使用的参数”。

我在 python3 交互式控制台中测试了上述内容:

>>> from decorators import repeat

>>> @repeat(nbrTimes=3)
>>> …
Run Code Online (Sandbox Code Playgroud)

python default-parameters python-3.x python-decorators

3
推荐指数
1
解决办法
3534
查看次数