带括号的上下文管理器适用于 python 3.9 但不适用于 3.8
Parenthesized context managers work in python 3.9 but not 3.8
所以我有一个 with
语句的简单示例。
它适用于 Python 3.8 和 3.9:
class Foo:
def __enter__(self, *args):
print("enter")
def __exit__(self, *args):
print("exit")
with Foo() as f, Foo() as b:
print("Foo")
输出(如预期):
enter
enter
Foo
exit
exit
但是如果我像这样添加括号它只适用于 Python 3.9:
class Foo:
def __enter__(self, *args):
print("enter")
def __exit__(self, *args):
print("exit")
with (Foo() as f, Foo() as b):
print("Foo")
3.8 中的输出:
File "foo.py", line 8
with (Foo() as f, Foo() as b):
^
SyntaxError: invalid syntax
我知道,我可以删除括号,但我不明白为什么它首先在 Python 3.9 中起作用。
我找不到相关的更改日志。
Parenthesized context managers are mentioned as a new feature in What’s New In Python 3.10。变更日志指出:
This new syntax uses the non LL(1) capacities of the new parser. Check
PEP 617 for more details.
但是PEP 617 was already accepted in Python 3.9, as described in its changelog:
Python 3.9 uses a new parser, based on PEG instead of LL(1). The new
parser’s performance is roughly comparable to that of the old parser,
but the PEG formalism is more flexible than LL(1) when it comes to
designing new language features. We’ll start using this flexibility in
Python 3.10 and later.
它甚至已经是 the Python 3.9 grammar 的一部分:
with_stmt:
| 'with' '(' ','.with_item+ ','? ')' ':' block
| 'with' ','.with_item+ ':' [TYPE_COMMENT] block
| ASYNC 'with' '(' ','.with_item+ ','? ')' ':' block
| ASYNC 'with' ','.with_item+ ':' [TYPE_COMMENT] block
with_item:
| expression 'as' star_target &(',' | ')' | ':')
| expression
我的猜测是因为 LL1 解析器在 Python 3.10 中被正式删除,直到那时它才被认为是一个新功能,而在 3.9 中仍然受到支持。
所以我有一个 with
语句的简单示例。
它适用于 Python 3.8 和 3.9:
class Foo:
def __enter__(self, *args):
print("enter")
def __exit__(self, *args):
print("exit")
with Foo() as f, Foo() as b:
print("Foo")
输出(如预期):
enter
enter
Foo
exit
exit
但是如果我像这样添加括号它只适用于 Python 3.9:
class Foo:
def __enter__(self, *args):
print("enter")
def __exit__(self, *args):
print("exit")
with (Foo() as f, Foo() as b):
print("Foo")
3.8 中的输出:
File "foo.py", line 8
with (Foo() as f, Foo() as b):
^
SyntaxError: invalid syntax
我知道,我可以删除括号,但我不明白为什么它首先在 Python 3.9 中起作用。 我找不到相关的更改日志。
Parenthesized context managers are mentioned as a new feature in What’s New In Python 3.10。变更日志指出:
This new syntax uses the non LL(1) capacities of the new parser. Check PEP 617 for more details.
但是PEP 617 was already accepted in Python 3.9, as described in its changelog:
Python 3.9 uses a new parser, based on PEG instead of LL(1). The new parser’s performance is roughly comparable to that of the old parser, but the PEG formalism is more flexible than LL(1) when it comes to designing new language features. We’ll start using this flexibility in Python 3.10 and later.
它甚至已经是 the Python 3.9 grammar 的一部分:
with_stmt: | 'with' '(' ','.with_item+ ','? ')' ':' block | 'with' ','.with_item+ ':' [TYPE_COMMENT] block | ASYNC 'with' '(' ','.with_item+ ','? ')' ':' block | ASYNC 'with' ','.with_item+ ':' [TYPE_COMMENT] block with_item: | expression 'as' star_target &(',' | ')' | ':') | expression
我的猜测是因为 LL1 解析器在 Python 3.10 中被正式删除,直到那时它才被认为是一个新功能,而在 3.9 中仍然受到支持。