我正在学习 Python,并且正在尝试该with **** as ****:
语句。我认为它的工作原理很像 C# using(****) {
,但恐怕我在关注过时的例子。
这是我的代码:
# -*- coding: iso-8859-1 -*-
import pprint
pow = 1, 2, 3
with pprint.pprint as pprint:
pprint(pow)
我假设这里发生的事情是在我的小闭包中是该函数pprint
的别名。pprint.pprint
我收到了一个奇怪的错误:
Traceback (most recent call last):
File "test.py", line 7, in <module>
with pprint.pprint as pprint:
AttributeError: __exit__
所以现在我想我正在使用旧版本 Python 的语法,就像我之前所做的一样(print "Hello"
)
为什么我的代码没有按预期工作?