是否可以使用as
in if
类似的声明with
我们使用的,例如:
with open("/tmp/foo", "r") as ofile:
# do_something_with_ofile
这是我的代码:
def my_list(rtrn_lst=True):
if rtrn_lst:
return [12, 14, 15]
return []
if my_list():
print(my_list()[2] * mylist()[0] / mylist()[1])
我可以用吗if
在这种类型中:
if my_list() as lst:
print(lst[2] * lst[0] / lst[1])
在第一if
我打了电话my_list
四次。我可以使用变量,但我想知道有什么方法可以使用as
?
No. The if
语句定义为:
if_stmt: 'if' test ':' suite ('elif' test ':' suite)* ['else' ':' suite]
... 在哪里test
是一组与 or/and 组合的测试表达式。
有关更多信息,请参阅:http://docs.python.org/3.3/reference/grammar.html http://docs.python.org/3.3/reference/grammar.html
您可以之前定义变量,然后测试其值:
lst = my_list()
if lst:
print(lst[2] * lst[0] / lst[1])
如果你想要一些挑战(并且仅限于实验性的东西),你可以改变 Python 语法,玩得开心:http://www.python.org/dev/peps/pep-0306/ http://www.python.org/dev/peps/pep-0306/
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)