《Learning Python 5th》第608页有示例代码:
>>> list(print(x.upper(), end=' ') for x in 'spam')
S P A M [None, None, None, None]
那么,为什么最后会弹出[无,无,无,无]?
最佳答案
print
函数返回 None
...因此,您的表达式构造了一个列表 [None, None, None, None]
。由于您处于 python 读取求值打印循环 (REPL) 中,因此表达式的结果在求值后将被打印...
因此,表达式的求值具有打印 SPA M
的副作用,然后在对表达式求值后,打印它的值 ([None, None, None, None ]
)。
关于python - 为什么 list(print(x.upper(), end =' ' ) for x in 'spam' ) 得到一个 [None, None, None, None] 列表?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38626055/