Wol*_*olf 9 python function syntax-error
在问题中"yield"关键字有什么作用?,我发现使用了一种我不希望有效的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
Run Code Online (Sandbox Code Playgroud)
我试图评估这种语法:
到目前为止失败了
SyntaxError:语法无效
我查询了问题中给出的链接(可能已过时),并在网上搜索了该用途def,但我没有发现任何解释这个"点名"模式的内容.我正在使用Python 3,也许这是Python 2的一个功能?
是(或是)这种语法有效,如果是的话是什么意思?
不,语法无效。通过查看文档很容易证明。在Python 2中,标识符是通过以下规则构造的:
identifier ::= (letter|"_") (letter | digit | "_")*
letter ::= lowercase | uppercase
lowercase ::= "a"..."z"
uppercase ::= "A"..."Z"
digit ::= "0"..."9"
Run Code Online (Sandbox Code Playgroud)
在 Py3 中,除了扩展到 Unicode 字符范围之外,规则或多或少是相同的。
看来作者的意思大概是这样的
class Node:
...
def _get_child_candidates(self, ...):
...
Run Code Online (Sandbox Code Playgroud)