草庐IT

Decorator

全部标签

使用包装的 Python Flask

尝试使用Python和Flask设置登录页面并出现错误:(第33行是@login_required)Traceback(mostrecentcalllast):File"routes.py",line33,in@login_requiredFile"/home/pi/FlaskTutorial/local/lib/python2.7/site-packages/flask/app.py",line1013,indecoratorself.add_url_rule(rule,endpoint,f,**options)File"/home/pi/FlaskTutorial/local/li

python - 超出最大递归深度,但仅在使用装饰器时

我正在编写一个程序来计算Python中的Levenshtein距离。我实现了记忆化,因为我递归地运行算法。我的原始函数在函数本身中实现了内存。这是它的样子:#MemoizationtablemappingfromatupleoftwostringstotheirLevenshteindistancedp={}#Levenshteindistancealgorithmdeflev(s,t):#Ifthestringsare0,returnlengthofotherifnots:returnlen(t)ifnott:returnlen(s)#Ifthelasttwocharactersar

python - 这些类型的 python 装饰器是如何编写的?

我想写一个装饰器来限制函数的执行次数,语法如下:@max_execs(5)defmy_method(*a,**k):#dosomethingherepass我认为可以编写这种类型的装饰器,但我不知道如何编写。我认为函数不会是这个装饰器的第一个参数,对吧?我想要一个“普通装饰器”实现,而不是一些带有call方法的类。这样做的原因是要了解它们是如何编写的。请解释语法以及装饰器的工作原理。 最佳答案 这是我想出来的。它不使用类,但使用函数属性:defmax_execs(n=5):defdecorator(fn):fn.max=nfn.ca

python - 默认内存所有方法

我正在编写一个应用程序,用于收集和显示来自科学仪器的数据。其中一条数据是一个频谱:本质上只是一个值列表,加上一个包含一些元数据的字典。一旦应用程序收集了数据,它就不会改变,因此列表和元数据都可以被认为是不可变的。我想通过大量内存对频谱执行计算的函数来利用这一点。这是一个玩具示例:classSpectrum(object):def__init__(self,values,metadata):self.values=valuesself.metadata=metadata#self.valuesandself.metadatashouldnotchangeafterthispoint.@p

python - Python 装饰器做什么,它的代码在哪里?

这个问题在这里已经有了答案:关闭10年前。PossibleDuplicate:UnderstandingPythondecoratorsPython装饰器是做什么的?当我向方法添加装饰器时,在哪里可以看到正在运行的代码?例如,当我添加@login_required在方法的顶部,是否有任何代码替换了该行?此行究竟如何检查用户session?

python - Python 重载装饰器

我知道编写关心参数类型的函数不是Pythonic,但有些情况下根本不可能忽略类型,因为它们的处理方式不同。在你的函数中有一堆isinstance检查是丑陋的;是否有可用的函数装饰器来启用函数重载?像这样:@overload(str)deffunc(val):print('Thisisastring')@overload(int)deffunc(val):print('Thisisanint')更新:这是我在DavidZaslavsky'sanswer上留下的一些评论:Withafewmodification[s],thiswillsuitmypurposesprettywell.One

Python unittest : to mock. patch() 或者只是用 Mock 替换方法?

在Python中编写单元测试时模拟类或方法时,为什么需要使用@patch装潢师?我可以在没有任何补丁注释的情况下用Mock对象替换该方法。例子:classTestFoobar(unittest.TestCase):defsetUp(self):self.foobar=FooBar()#1)Withpatchdecorator:@patch.object(FooBar,"_get_bar")@patch.object(FooBar,"_get_foo")deftest_get_foobar_with_patch(self,mock_get_foo,mock_get_bar):mock_g

python - 如何创建一个能够包装实例、类和静态方法的 Python 类装饰器?

我想创建一个Python类装饰器(*),它能够无缝包装该类可能具有的所有方法类型:实例、类和静态。这是我目前拥有的代码,对破坏代码的部分进行了注释:defwrapItUp(method):defwrapped(*args,**kwargs):print"Thismethodcallwaswrapped!"returnmethod(*args,**kwargs)returnwrappeddundersICareAbout=["__init__","__str__","__repr__"]#,"__new__"]defdoICareAboutThisOne(cls,methodName):

python - 使用装饰器在 __init__ 之后注入(inject)函数调用

我正在尝试找到创建类装饰器的最佳方法,该类装饰器执行以下操作:向装饰类中注入(inject)一些函数在调用装饰类的__init__之后强制调用这些函数之一目前,我只是保存对“原始”__init__方法的引用,并将其替换为调用原始函数和附加函数的__init__。它看起来类似于:orig_init=cls.__init__defnew_init(self,*args,**kwargs):"""'Extend'wrappedclass'__init__sowecanattachtoallsignalsautomatically"""orig_init(self,*args,**kwargs

python - 在 Python 中从子类访问父类中的装饰器

如何从子类的基类访问装饰器?我(错误地)假设ffg.会工作:classbaseclass(object):def__init__(self):print'heythisisthebase'def_deco(func):defwrapper(*arg):res=func(*arg)print'I\'madecorator.Thisisfabulous,butthatcolour,solastseasonsweetiedarling'returnresreturnwrapper@_decodefbasefunc(self):print'I\'mabasefunction'这个类工作正常,但