这是python装饰器还是python属性?
我是一名新的python程序员,在阅读以下代码时,我不理解代码的意思:这是python装饰器还是python属性?,python,Python,我是一名新的python程序员,在阅读以下代码时,我不理解代码的意思:@predict\u all.property('sequences')。我发现python decorator几乎类似于@decortor风格。所以我对这个代码@predict\u all.property('sequences')感到困惑,因为在@predict\u all后面有一个点。这是python装饰器还是python属性 @recurrent(states=['states', 'cells'], outputs=
@predict\u all.property('sequences')
。我发现python decorator几乎类似于@decortor
风格。所以我对这个代码@predict\u all.property('sequences')
感到困惑,因为在@predict\u all
后面有一个点。这是python装饰器还是python属性
@recurrent(states=['states', 'cells'], outputs=['destination', 'states', 'cells'])
def predict_all(self, **kwargs):
pre_emb = tuple(self.pre_context_embedder.apply(**kwargs))
itr_in = tensor.concatenate(pre_emb + self.rec_input(**kwargs), axis=1)
itr = self.input_to_rec.apply(itr_in)
itr = itr.repeat(4, axis=1)
(next_states, next_cells) = self.rec.apply(itr, kwargs['states'], kwargs['cells'], mask=kwargs['latitude_mask'], iterate=False)
post_emb = tuple(self.post_context_embedder.apply(**kwargs))
rto = self.rec_to_output.apply(tensor.concatenate(post_emb + (next_states,), axis=1))
rto = self.process_rto(rto)
return (rto, next_states, next_cells)
@predict_all.property('sequences')
def predict_all_sequences(self):
return self.sequences
补充信息:
问题的根源,我为什么要问这个问题
当我了解decorator时,我发现大多数教程都显示decorator很简单,就像这样:@timer
,即@
加上一个函数名。属性是这样的:@property
,即在方法之前添加@property
。因此,当我阅读上述代码时,我感到困惑,这段代码@predict_all.property('sequences')
意味着什么,我以前从未见过这种形式的代码。因此我感到困惑,这段代码是代表python装饰器还是python属性?我在谷歌上搜索了一下,没有找到关于这种风格的任何东西,即@
加
。所以我把这个问题贴在Stack Overflow上,希望得到有用的答案或评论
我学到的python decorator示例代码如下:
def timer(func):
def deco(*args, **kwargs):
start_time = time.time()
func(*args, **kwargs)
stop_time = time.time()
print("the func run time is %s" %(stop_time-start_time))
return deco
@timer
def test1():
time.sleep(1)
print('in the test1')
正如Charles Duffy所说,
predict_all.property
可以是一个函数,返回一个充当装饰器的函数
为了进一步了解,您可以观看下面的演示
类预测(对象):
def属性(自身、条件):
def包装(fn):
def内部(*参数,**kw):
如果条件=='序列':
打印(“序列”)
其他:
打印(“其他”)
返回fn(*参数,**kw)
返回内部
返回包装器
predict_all=predict()
@预测所有属性(“序列”)
def foo():
通过
foo()
#结果——序列
希望它有帮助
@
-应用decorator(语法糖)
-属性访问
decorator
是可调用的东西,它返回可调用的东西
property
是一种特殊的装饰器,它是一个类(描述符),返回对象(描述符对象),在Python中,属性和描述符用于属性访问控制
现在让我们进入问题是@predict\u all.property('sequences')是python装饰器还是python属性?
它是装饰器。它不是属性。predict\u all
返回应用程序
类()的对象<代码>应用程序类具有名为属性
的方法,该方法是一个常规的装饰器,用于按指定名称获取类属性
您也可以考虑以下使用装饰器的例子(在线尝试)
这是完整的密码吗?我们可以把代码的输出也放进去吗?完整的代码在这个链接[link]()@Reactive_learner我不明白为什么有人只是对这个问题投了反对票,但没有说任何有用的答案或评论。太奇怪了。你为什么认为是一个还是另一个
predict_all.property
可以是一个函数,返回一个充当装饰器的函数。(我们不知道你的predict\u all
是什么,也不知道它从哪里来,所以这当然是推测性的)。除此之外,“请解释一下这个代码”的问题在这里通常是不受欢迎的;它们必须非常狭隘和具体,才能在主题上加以考虑。这可能是可以接受的,但我可以看到不同的合理观点;反对的一个论点是,它不是一个独立的库——我们不知道哪些库提供了您所询问的代码,所以这个问题本质上是一个需要猜测的问题。如果你构建了一些独立的东西,包括它的import
s和链接到所使用的库,那就干净多了。
import time
def simple_decorator(func):
def wrapper(*args, **kwargs):
print('i am simple decorator', func, args, kwargs)
return func(*args, **kwargs)
return wrapper
@simple_decorator
def func_1(*args, **kwargs):
print('i am func_1')
class DecoratorManager:
def __init__(self):
self.log = list()
def not_that_simple_decorator(self, key):
self.log.append((time.time(), key))
def simple_decorator(func):
def wrapper(*args, **kwargs):
print('i am simple decorator', func, args, kwargs)
return func(*args, **kwargs)
return wrapper
return simple_decorator
d_m = DecoratorManager()
@d_m.not_that_simple_decorator(key='pretty_key')
def func_2(*args, **kwargs):
print('i am func_2')
def func_3(*args, **kwargs):
print('i am func_3')
func_3 = d_m.not_that_simple_decorator(key='pretty_key')(func_3)
func_1(1, a=10)
func_2(2, b=20)
func_3(3, c=30)
print('decorator_manager_log:', d_m.log)