为什么我在Python控制台中获得了Nones元组的印刷,而不是单个元组?
当使用Python(通过CMD)并在内部写下此内容时:
>>> import random
>>> print("hello"),print("world"),print(random.randint(5,10))
我得到的输出是:
hello
world
8
(None, None, None)
现在我不确定解释器为什么返回none
的元组,而不是单个无
。
While using Python (via cmd) and writing this inside:
>>> import random
>>> print("hello"),print("world"),print(random.randint(5,10))
the output I'm getting is:
hello
world
8
(None, None, None)
Now I'm not sure why the interpreter returns the tuple of None
's, but not a single None
.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
python在第二个输入行中解释
,
是从print
的返回值中创建元组,这些全部none
。只需对每个打印语句都有一行。这是此行为的另一个示例:
>>> 5,4,3
返回
(5,4,3)
[更新以解决您的评论] ,
因为Python的解释
a,b, c
是元组(a,b,c)
,soprint(),print(),print()
等同于tuple(print(),print(),print())
。如果您在行行时进行IT IT,则不会创建
Tuple
,而Python只是执行print()
语句。现在是实际上使您感到困惑的重点。调用
print()
也将返回无
,但这会自动隐藏,因为这是正常行为。的元组无
s,但是不是自动隐藏的,因为它不是none
。Python is interpreting the
,
in the second input line as creation of a tuple out of the return values ofprint
which are allNone
. Just have a single line for every print statement.Here is another example of this behavior:
>>> 5,4,3
returns
(5, 4, 3)
[Update to address your comment]
Because the python interpretation of
a,b,c
istuple(a,b,c)
, soprint(),print(),print()
is equivalent totuple(print(),print(),print())
.If you do it line after line, the
tuple
is not created and python just executes theprint()
statement.Now comes the point which actually confuses you. Calling
print()
will also returnNone
, but this automatically hidden, as this is the normal behavior. the tuple ofNone
s however is not automatically hidden as it is notNone
.