装饰的功能@staticmethod
和装饰的功能有什么区别@classmethod
?
如何在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
在一个真实的应用程序中这样做 - 只是试图了解装饰器和装饰器链是如何工作的.
我想了解内置函数的property
工作原理.令我困惑的是,property
它也可以用作装饰器,但它只在用作内置函数时才需要参数,而不是用作装饰器时.
这个例子来自文档:
class C(object):
def __init__(self):
self._x = None
def getx(self):
return self._x
def setx(self, value):
self._x = value
def delx(self):
del self._x
x = property(getx, setx, delx, "I'm the 'x' property.")
Run Code Online (Sandbox Code Playgroud)
property
的论点是getx
,setx
,delx
和文档字符串.
在下面的代码中property
用作装饰器.它的对象是x
函数,但在上面的代码中,参数中没有对象函数的位置.
class C(object):
def __init__(self):
self._x = None
@property
def x(self):
"""I'm the 'x' property."""
return self._x
@x.setter
def x(self, value):
self._x = value
@x.deleter
def x(self):
del self._x
Run Code Online (Sandbox Code Playgroud)
而且,如何在 …
python properties decorator python-internals python-decorators
我正在看一些使用该@
符号的Python代码,但我不知道它的作用.我也不知道搜索Python文档会搜索什么,或者当@
包含符号时Google不会返回相关结果.
我看到像这样的模式
def __init__(self, x, y, z):
...
self.x = x
self.y = y
self.z = z
...
Run Code Online (Sandbox Code Playgroud)
经常使用更多参数.有没有一种好方法可以避免这种乏味的重复性?该类应该继承namedtuple
吗?
我对如何@property
在Python中使用感兴趣.我已经阅读了python文档和那里的例子,在我看来,它只是一个玩具代码:
class C(object):
def __init__(self):
self._x = None
@property
def x(self):
"""I'm the 'x' property."""
return self._x
@x.setter
def x(self, value):
self._x = value
@x.deleter
def x(self):
del self._x
Run Code Online (Sandbox Code Playgroud)
我不知道从包装_x
填充属性装饰器可以获得什么好处.为什么不实施为:
class C(object):
def __init__(self):
self.x = None
Run Code Online (Sandbox Code Playgroud)
我认为,属性功能在某些情况下可能会有用.但当?有人可以给我一些现实世界的例子吗?
谢谢.
我有一个像下面的装饰.
def myDecorator(test_func):
return callSomeWrapper(test_func)
def callSomeWrapper(test_func):
return test_func
@myDecorator
def someFunc():
print 'hello'
Run Code Online (Sandbox Code Playgroud)
我想增强这个装饰器来接受另一个如下所示的参数
def myDecorator(test_func,logIt):
if logIt:
print "Calling Function: " + test_func.__name__
return callSomeWrapper(test_func)
@myDecorator(False)
def someFunc():
print 'Hello'
Run Code Online (Sandbox Code Playgroud)
但是这段代码给出了错误,
TypeError:myDecorator()只需要2个参数(给定1个)
为什么函数不会自动通过?如何将函数显式传递给装饰器函数?
def make_bold(fn):
return lambda : "<b>" + fn() + "</b>"
def make_italic(fn):
return lambda : "<i>" + fn() + "</i>"
@make_bold
@make_italic
def hello():
return "hello world"
helloHTML = hello()
Run Code Online (Sandbox Code Playgroud)
输出: "<b><i>hello world</i></b>"
我大致了解装饰器以及它在大多数示例中如何与其中一个一起工作.
在这个例子中,有2个.从输出,它似乎@make_italic
首先执行,然后@make_bold
.
这是否意味着对于装饰函数,它将首先运行函数然后移动到其他装饰器的顶部?就像@make_italic
第一个@make_bold
,而不是相反.
那么这意味着它与大多数编程中的自上而下方法的规范不同?只为这个装饰器的情况?还是我错了?
我想知道是否可以根据全局设置(例如操作系统)控制 Python 函数定义。例子:
@linux
def my_callback(*args, **kwargs):
print("Doing something @ Linux")
return
@windows
def my_callback(*args, **kwargs):
print("Doing something @ Windows")
return
Run Code Online (Sandbox Code Playgroud)
然后,如果有人使用 Linux,my_callback
将使用第一个定义,第二个将被默默忽略。
它不是关于确定操作系统,而是关于函数定义/装饰器。
这是Python 2.5,它也是GAE,并不重要.
我有以下代码.我正在使用dec_check类作为装饰器在bar中装饰foo()方法.
class dec_check(object):
def __init__(self, f):
self.func = f
def __call__(self):
print 'In dec_check.__init__()'
self.func()
class bar(object):
@dec_check
def foo(self):
print 'In bar.foo()'
b = bar()
b.foo()
Run Code Online (Sandbox Code Playgroud)
执行此操作时,我希望看到:
In dec_check.__init__()
In bar.foo()
Run Code Online (Sandbox Code Playgroud)
但我得到" TypeError: foo() takes exactly 1 argument (0 given)
"作为.foo()
一种对象方法,以自我为参数.我猜测问题是bar
当我执行装饰器代码时,实例并不存在.
那么如何将一个实例传递bar
给装饰器类呢?
python ×10
decorator ×3
oop ×2
properties ×2
python-2.7 ×2
function ×1
methods ×1
namedtuple ×1
python-3.x ×1
python-attrs ×1
syntax ×1