【发布时间】:2017-02-13 17:41:06
【问题描述】:
在问题What does the "yield" keyword do? 中,我发现正在使用的Python 语法我没想到会有效。这个问题很老并且有大量的选票,所以我很惊讶至少没有人对这个函数定义发表评论:
def node._get_child_candidates(self, distance, min_dist, max_dist):
if self._leftchild and distance - max_dist < self._median:
yield self._leftchild
if self._rightchild and distance + max_dist >= self._median:
yield self._rightchild
我试图评估这种语法:
- 将属性分配给类或对象
- 重新定义导入模块的功能
到目前为止失败了
SyntaxError: 无效语法
我查找了问题中给出的link (maybe outdated),并在网上搜索了def 的用法,但我没有找到任何解释这种“点名”模式的内容。我使用的是 Python 3,也许这是 Python 2 的一个特性?
此语法是否有效,如果有效,是什么意思?
【问题讨论】:
-
如果您查看代码,您也会看到
node = candidates.pop(),然后您会看到node._get_child_candidates,这是函数中的拼写错误。他们正在节点实例上调用方法get_child_candidates。 -
@PadraicCunningham 好吧,在我提到的情况下是一个错字,但在所有情况下语法都是无效的(我试图找出identifier syntax you mentioned,但发现
Pcrule 有点混乱) ? -
python2 和 python3 的唯一区别是 Python 3.0 引入了 ASCII 范围之外的其他字符 即非 ascii 标识符,pep python.org/dev/peps/pep-3131 详细介绍了它
标签: python function syntax-error