python-decorators

    0热度

    1回答

    以下是我的python 2.7控制台的输出。我一直在用python 3编写类似的东西,它按预期工作。那么,为什么我允许做以下的重新分配(在Python 2.7): >>> class Fola: ... def __init__(self,a,b): ... self._a = a ... self._b = b ... @property ... def a(self):

    1热度

    1回答

    我有一个记忆化装饰,看起来像这样: def memoize(obj): from functools import wraps cache = {} @wraps(obj) def memoizer(*args, **kwargs): if args not in cache: cache[args] = obj(*args, *

    1热度

    1回答

    在注释:Is there a decorator to simply cache function return values? @gerrit指出,使用可变问题,但哈希的,反对的功能与functools.lru_cache装饰: 如果我传递一个哈希的,可变的参数,第一次调用该函数后,更改 对象的值,第二次调用将返回更改后的对象,而不是原始对象。这几乎肯定是 不是用户想要的。 从我的理解,假设的可

    1热度

    1回答

    我试图添加一个access_rights修饰器到我的Bottle应用程序来检查访问路线时的权限。但是,它没有获取装饰函数的参数,这会在尝试再次调用装饰函数时导致错误。 下面是一个使用装饰的代码示例: @route('/users') @access_rights({'POST': ['admin']}) def users(user): pass 的user参数来源于瓶插件,我写

    -1热度

    1回答

    在我的课堂我写了这个方法: def ts_prep(self, test_id): ltouple =() tab_lib = temp_library.objects.filter(main_id=test_id) l1 = ["Settings", ""] ltouple += (l1,) if tab_lib.count() == 0: lt

    1热度

    1回答

    我有点困惑如何获得和设置温度在这个属性装饰的代码。这是使用属性装饰器的正确方法吗?但是我不确定如何使用它。我可以调用c.temperature并返回初始值(0或任何我用来实例化类),但之后我无法使用set_temperature()设置温度。我看了几个关于这个话题的答案,但可能没有提到这一点。 class Celsius: def __init__(self, temperature=0

    0热度

    1回答

    我正在查看一些关于Python的链接。 https://medium.com/the-renaissance-developer/python-101-object-oriented-programming-part-1-7d5d06833f26 还有在那里用于创建(?)属性和setter方法的装饰器。以下是代码: class Vehicle: def __init__(self, nu

    1热度

    1回答

    以下是我处理的简化版本: def mydecorator(prop): def wrapper(self_arg): return prop(self_arg) + 'bar' return wrapper class Foo(object): def __init__(self): self.value = 'foo' @p

    1热度

    4回答

    我正在刷新我关于一些python特性的记忆,我没有得到,我从this python tutorial得知,并且有一个我不完全理解的例子。这是关于一个装饰计数调用一个函数,下面的代码: def call_counter(func): def helper(x): helper.calls += 1 return func(x) helper.calls =

    0热度

    1回答

    我问另一个问题python flask how to pass a dynamic parameter to a decorator,我得到了答案。 我现在正在使用烧瓶,并使用flask-restful api,这将很容易建立宁静的API 和我的代码是这样的。 class ShopCategoryListAPI(Resource): #@redis_hash_shop(shop_id=L