- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
所以我明白了:
The end of a logical line is represented by the token NEWLINE
这意味着 Python 语法的定义方式是结束逻辑行的唯一方式是使用 \n
标记。
物理行也是如此(而不是 EOL,它是您在编写文件时使用的平台的 EOL,但仍然被 Python 转换为通用 \n
。
逻辑行可以等同于也可以不等同于一条或多条物理行,但通常是一条,如果您编写干净的代码,大多数情况下它就是一条。
在某种意义上:
foo = 'some_value' # 1 logical line = 1 physical
foo, bar, baz = 'their', 'corresponding', 'values' # 1 logical line = 1 physical
some_var, another_var = 10, 10; print(some_var, another_var); some_fn_call()
# the above is still still 1 logical line = 1 physical line
# because ; is not a terminator per se but a delimiter
# since Python doesn't use EBNF exactly but rather a modified form of BNF
# p.s one should never write code as the last line, it's just for educational purposes
没有展示 1 个逻辑如何等同于 > 1 个物理的示例,我的问题是来自文档的以下部分:
Statements cannot cross logical line boundaries except where NEWLINE is allowed by the syntax (e.g., between statements in compound statements)
但这究竟意味着什么?我理解复合语句的列表,它们是:if、while、for、 等。它们都是由一个或多个子句和每个子句组成的,又由一个标题和一个套件组成。suite由一个或多个语句组成,我们举个例子更具体一点:
所以 if 语句根据语法是这样的(不包括 elifs 和 else 从句):
if_stmt ::= "if" expression ":" suite
其中套件及其后续语句:
suite ::= stmt_list NEWLINE | NEWLINE INDENT statement+ DEDENT
statement ::= stmt_list NEWLINE | compound_stmt
stmt_list ::= simple_stmt (";" simple_stmt)* [";"]
所以这意味着如果您愿意,您可以选择(由“|”给出)您的套件是以下两种方式之一:
在同一行:
缺点:不是pythonic,你不能有另一个引入新 block 的复合语句(比如func def、另一个if等)
优点:我猜是一条线
例子:
if 'truthy_string': foo, bar, baz = 1, 2, 3; print('whatever'); call_some_fn();
引入一个新 block :
优点:所有,以及正确的方法
例子:
if 'truthy_value':
first_stmt = 5
second_stmt = 10
a, b, c = 1, 2, 3
func_call()
result = inception(nested(calls(one_param), another_param), yet_another))
但是我不知道怎么办
Statements cannot cross logical line boundaries except where NEWLINE is allowed by the syntax
我在上面看到的是一个套件,它是由if 子句 控制的代码块,反过来,< strong>suite,由逻辑上独立的行(语句)组成,其中每条逻辑行都是一条物理行(巧合)。我看不出一条逻辑线如何跨越边界(这基本上只是结束、极限、换行符的花哨词),我看不出一个语句如何跨越这些边界并跨越到下一个声明,或者也许我真的很困惑,把一切都搞混了,但如果有人能解释一下。
提前感谢您抽出时间。
最佳答案
幸运的是有一个Full Grammar specification在 Python 文档中。
该规范中的声明定义为:
stmt: simple_stmt | compound_stmt
逻辑行由 NEWLINE
分隔(这不在规范中,但基于您的问题)。
好吧,让我们来看看这个,a 的规范是什么
simple_stmt
:simple_stmt: small_stmt (';' small_stmt)* [';'] NEWLINE
small_stmt: (expr_stmt | del_stmt | pass_stmt | flow_stmt |
import_stmt | global_stmt | nonlocal_stmt | assert_stmt)
好吧,现在它进入了几个不同的路径,单独遍历所有路径可能没有意义,但根据规范 simple_stmt
可以 交叉逻辑行边界如果任何small_stmt
包含NEWLINE
(目前它们不但可以)。
除了理论上的可能性之外,实际上还有
compound_stmt
:compound_stmt: if_stmt | while_stmt | for_stmt | try_stmt | with_stmt | funcdef | classdef | decorated | async_stmt
[...]
if_stmt: 'if' test ':' suite ('elif' test ':' suite)* ['else' ':' suite]
[...]
suite: simple_stmt | NEWLINE INDENT stmt+ DEDENT
我只选择了 if
语句和 suite
因为它们已经足够了。 if
语句包括 elif
和else
这里面的所有内容都是一个语句(复合语句)。并且因为它可能包含 NEWLINE
(如果 suite
不仅仅是一个 simple_stmt
),它已经满足了“一个跨越的语句”的要求逻辑线边界”。
if
示例(示意图):if 1:
100
200
会是:
if_stmt
|---> test --> 1
|---> NEWLINE
|---> INDENT
|---> expr_stmt --> 100
|---> NEWLINE
|---> expr_stmt --> 200
|---> NEWLINE
|---> DEDENT
所有这些都属于 if 语句(并且它不仅仅是一个由 if
或 while
来“控制”的 block ,...)。
parser
相同的if
, symbol
和 token
一种可视化方法是使用内置的 parser
、token
和 symbol
模块(真的,我不知道在我写答案之前关于这个模块):
import symbol
import parser
import token
s = """
if 1:
100
200
"""
st = parser.suite(s)
def recursive_print(inp, level=0):
for idx, item in enumerate(inp):
if isinstance(item, int):
print('.'*level, symbol.sym_name.get(item, token.tok_name.get(item, item)), sep="")
elif isinstance(item, list):
recursive_print(item, level+1)
else:
print('.'*level, repr(item), sep="")
recursive_print(st.tolist())
实际上我无法解释大部分 parser
结果,但它表明(如果你删除了很多不必要的行)suite
包括它的换行符确实属于 if_stmt
。缩进表示解析器在特定点的“深度”。
file_input
.stmt
..compound_stmt
...if_stmt
....NAME
....'if'
....test
.........expr
...................NUMBER
...................'1'
....COLON
....suite
.....NEWLINE
.....INDENT
.....stmt
...............expr
.........................NUMBER
.........................'100'
.......NEWLINE
.....stmt
...............expr
.........................NUMBER
.........................'200'
.......NEWLINE
.....DEDENT
.NEWLINE
.ENDMARKER
这可能会变得更漂亮,但我希望即使是目前的形式也能起到说明作用。
关于Python词法分析——逻辑行&复合语句,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49529410/
当我尝试加载库 Raster 时,我收到如下错误: 错误:inDL(x, as.logic(local), as.logic(now), ...) 中的“raster”的包或命名空间加载失败:无法加载
当我尝试加载库 Raster 时,我收到如下错误: 错误:inDL(x, as.logic(local), as.logic(now), ...) 中的“raster”的包或命名空间加载失败:无法加载
望着help section about_Comparison_Operators of PowerShell我是这样理解的: PS C:\> $false,$false -eq $true PS C
我刚刚修改了旧代码,现在似乎没有任何效果。请您指导我哪里出错了。 一些不起作用的事情是: 以前,焦点始终停留在屏幕上唯一的输入字段上。 (现在不行了),代码中的 if else 条件也不起作用。 On
请帮我找到一个使用普通 'ol javascript 的解决方案(我无法使用外部框架)。此外,CSS :hover 选择器不适用于现实世界的实现。 注册事件发生的事情设置所有调用最后注册事件数组项。
我想创建一个软件来为残障 child 交通规划公交路线(及其最佳载客量)。 这些总线具有以下规范: m 个座位(最多 7 个 - 因为有司机和助理) o 轮椅“座位”(最多 4 个) 固定的最大负载量
有人能帮我吗?似乎我的 for 逻辑根本不起作用,因为它一直在上午 12:00 返回我的开始时间 这是我的代码 Sub forlogic() Dim i As Single Dim t
我正在尝试设置 OR两个切片器过滤器之间的逻辑。两个切片器来自相同的数据集。以下是更多详细信息: 我的源表: 带切片器的视觉效果: 我的目标是,如果我从切片器 1 和切片器 2 中选择任何值,我的视觉
我有以下 C 语句: int res = x & (x ^ y); 有没有办法做同样的事情,但每次只使用一次x和y? 例如: x | (~x & y) == x | y 最佳答案 是的,通过扩展 xo
我正在创建 Azure 逻辑应用程序以将新的 Sharepoint 文件添加到 Azure Blob。 Sharepoint 由我的公司运行,我使用我的凭据登录来为逻辑应用程序创建 Sharepoin
我有一个问题要求为给定函数合成最简单的乘积表达式总和。基本上,如果 AB == CD,则函数为 1,否则为 0,结果如下: (!A && !B && !C && !D) || (!A && B &&
我正在尝试确定是否可以在不溢出的情况下计算两个 32 位整数的总和,同时仅使用某些按位运算符和其他运算符。因此,如果整数 x 和 y 可以相加而不会溢出,则以下代码应返回 1,否则返回 0。 ((((
处理乍一看需要许多嵌套 if 语句的复杂业务逻辑的好方法是什么? 例子: 折扣券。可能: 1a) 超值折扣 1b) 百分比折扣 2a) 正常折扣 2b) 累进折扣 3a) 需要访问优惠券 3b) 不需
假设我有一个“numbers”对象数组,其中包含“startNo”整数和“endNo”整数。 数组中可以有多个“数字”,我想获取一个包含修改对象的新数组,该数组仅具有不重叠的范围。 例如:如果数组有:
我在这个问题上遇到了困难。我正在使用 JavaScript。 我有一个文本区域,用于检测 @ 输入并将其位置存储在数组中。 var input = "@a @b @c" //textarea var
默认 IN 使用 OR 基本逻辑。有没有办法在范围内使用 AND 基本逻辑。 例如下面的查询 SELECT ItemId,CategoryID FROM ItemCategories WHERE Ca
我想在您将鼠标悬停在网站图像上时添加叠加层。我在这里实现了这个,它工作正常http://jsfiddle.net/stujLbjh/ 这是js代码: var divs = document.query
这个问题在这里已经有了答案: Which is faster: x>2 是否比 x>>31 快?换句话说,sar x, 2 是否比 sar x, 31 快?我做了一些简单的测试,他们似乎有相同的速度
我有grails criteriaQuery,我在这里再次检查OR逻辑,就像这样一个状态变量: or { eq("status", Status.ONE) eq("status",
我有grails criteriaQuery,我在这里再次检查OR逻辑,就像这样一个状态变量: or { eq("status", Status.ONE) eq("status",
我是一名优秀的程序员,十分优秀!