我是一个新的python程序员,在阅读以下代码时,我不明白代码的意思:@predict_all.property('sequences'). 我发现python装饰器几乎就像@decortor风格一样。所以我很困惑这个代码@predict_all.property('sequences')时,有是一个点.以下的@predict_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
补充资料:
问题的根源,我为什么要问这个问题?
当我了解装饰器时,我发现大多数教程都显示装饰器很简单,就像这种风格:@timer,也就是@加上一个功能名称。并且属性是这样的样式:@property,即@property在方法之前添加。所以当我阅读上面的代码时,我很困惑这段代码@predict_all.property('sequences')是什么意思,我以前没有见过这种形式的代码。所以我很困惑,这段代码代表的是python装饰器还是python属性?我用谷歌搜索并没有找到关于这种风格的东西,那就是@plus .。所以我把这个问题贴在 Stack Overflow 上,希望得到有用的答案或评论。
我学到的python装饰器的示例代码如下:
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')
蛊毒传说
杨__羊羊
相关分类