简体   繁体   English

当我不使用打印时,为什么 Python 会在循环体中打印一个值?

[英]Why does Python print a value in the body of a loop when I don't use print?

I understand that the Python interpreter is a REPL ... but in a loop, the "value" of the loop must be single-valued... correct?我知道Python 解释器是一个 REPL ……但是在循环中,循环的“值”必须是单值的……对吗? And yet I see this (tested in both Python 2.7.18 and 3.8.5 with range instead of xrange ):然而我看到了这一点(在 Python 2.7.18 和 3.8.5 中使用range而不是xrange ):

>>> for _ in xrange(5):
...   3
...
3
3
3
3
3

Where is this behavior documented in Python?这种行为在 Python 中记录在哪里?

It seems to get suppressed when in a called function:在被调用的函数中似乎被抑制了:

>>> def foo(x):
...   1
...   x
...   return x+1
...
>>> foo(5)
6
>>> def bar(n):
...   for x in xrange(5):
...     1
...     x
...   return n+1
...
>>> bar(5)
6
>>> for x in xrange(5):
...   1
...   x
...
1
0
1
1
1
2
1
3
1
4

Note to downvoters: This is not an obvious behavior!给downvoters 的注意:这不是一个明显的行为! Contrast with Javascript:与 Javascript 对比:

> for (var i = 0; i < 5; ++i) {
    i
}
4

It's not the "value" of the loop that's resulting in 3 being displayed.导致显示 3 的不是循环的“值”。 It's the result of the interpreter evaluating 3.__repr__() on each iteration of the loop.这是解释器在循环的每次迭代中评估3.__repr__()的结果。

For instance if you instead evaluated i you would get the following.例如,如果您改为评估i您将得到以下结果。

for i in range(5):
   i
# 0
# 1
# 2
# 3
# 4

If you want a for loop that doesn't display anything you'll want to make sure that all statements in the loop don't print anything when evaluated in the interpreter.如果您想要一个不显示任何内容的 for 循环,您需要确保循环中的所有语句在解释器中计算时不打印任何内容。 An example of such a statement would be.这种声明的一个例子是。

for i in range(5):
    _ = None

The reason that for loops don't have a "final value" (unlike your javascript example) is because in Python for loops are statements, and statements in Python don't have a value. for 循环没有“最终值”的原因(与您的 javascript 示例不同)是因为在 Python 中 for 循环是语句,而 Python 中的语句没有值。

The interpreter will print out any variables you just type out ..解释器将打印出您刚刚输入的任何变量..

It's just a convenience function.这只是一个方便的功能。

If you type in "3" in the interpreter, it will also print out "3".如果你在解释器中输入“3”,它也会打印出“3”。

Its because you are using Python interactive interpreter (REPL).这是因为您使用的是 Python 交互式解释器 (REPL)。 REPL always calls the repr method of the object. REPL 总是调用对象的repr方法。 In your case it is 3 .在您的情况下,它是3

If you try creating a .py file of this code and run it.如果您尝试创建此代码的 .py 文件并运行它。 Nothing will be printed and the program will just run and close.不会打印任何内容,程序将运行并关闭。

You're confusing levels of abstraction.你混淆了抽象层次。 Every statement has its value.每一个陈述都有它的价值。 In the case of your loop, you interpret the line 3 five times, and thus have five non-empty results returned.在您的循环中,您将第3行解释了五次,因此返回了五个非空结果。 The interactive interpreter prints that value each time.交互式解释器每次都会打印该值。

To see it a little more clearly, change the expression:为了更清楚地看到它,请更改表达式:

for i in range(5):
    i

Evidently the REPL shows the result of every expression executed as a statement , not just the outer statement (the for-loop).显然,REPL 将每个表达式的执行结果显示为语句,而不仅仅是外部语句(for 循环)。 The for-loop itself does not have any value at all: it is just a statement, not an expression. for 循环本身根本没有任何值:它只是一个语句,而不是一个表达式。

Incidentally, you can see the same thing in other constructions, such as an if-statement:顺便说一句,您可以在其他结构中看到相同的内容,例如 if 语句:

if True:
    1 + 2
    "Hello"

shows显示

3
'Hello' 

in the REPL.在 REPL 中。

Presumably the reason it does not do the same thing for expressions inside functions is that it would produce so much extraneous information that it would only cause confusion.大概它对函数内部的表达式不做同样的事情的原因是它会产生太多无关的信息,只会引起混淆。

OK I think I found something definitive in the Python docs under expression statements :好的,我想我在表达式语句下的 Python 文档中找到了一些明确的内容:

6.1. 6.1. Expression statements表达式语句

Expression statements are used (mostly interactively) to compute and write a value, or (usually) to call a procedure (a function that returns no meaningful result; in Python, procedures return the value None).表达式语句用于(主要以交互方式)计算和写入值,或(通常)调用过程(返回无意义结果的函数;在 Python 中,过程返回值 None)。 Other uses of expression statements are allowed and occasionally useful.表达式语句的其他用途是允许的,有时也很有用。 The syntax for an expression statement is:表达式语句的语法是:

 expression_stmt ::= expression_list

An expression statement evaluates the expression list (which may be a single expression).表达式语句计算表达式列表(可能是单个表达式)。

In interactive mode, if the value is not None , it is converted to a string using the built-in repr() function and the resulting string is written to standard output (see section The print statement) on a line by itself.在交互模式下,如果值不是None ,则使用内置的repr()函数将其转换为字符串,并将结果字符串repr()写入标准输出(参见打印语句部分)。 (Expression statements yielding None are not written, so that procedure calls do not cause any output.) (产生None表达式语句None被写入,因此过程调用不会导致任何输出。)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么即使我不输入print,python也会打印? - Why does python print even if I don't type print? 在python shell中键入“她说,&#39;不要&#39;”时,为什么会打印“她说,&#39;不要&#39;” - When typing “she said, \”don't\“” in the python shell why does it print 'she said, “don\'t”' python - 当某些函数在python中使用“打印”时,屏幕上是否有可能隐藏打印(或不打印)? - Possability hidden print( or don't print) on screen when some functions use "print" in python? 为什么当我使用 end 参数时,python print() 函数会在行尾打印一个 %? - Why does python print() function print a % at the end of the line when I use the end parameter? 当我从 Python 中的 utf-8 文件打印文本时,为什么看不到希伯来语字符? - Why don't I see the hebrew characters, when I print text from an utf-8 file in Python? 为什么在循环使用睡眠时Python中的打印不会暂停? - Why print in Python doesn't pause when using sleep in a loop? Python-为什么此for循环仅打印1个字母? - Python - why does this for loop only print 1 letter? 为什么我的打印在第二个循环之后执行,即使我先使用打印? - Why does my print execute after the second loop even if I use print first? 刚开始学习 python; 当我使用列表推导时,为什么这个 for 循环的打印不同? 我如何使循环相同? - New to learning python; why is the print for this for-loop different when I use a list comprehension? how do i make the loop be the same? 为什么我不能在 1 行中使用 python 打印句子和变量值? - why i cant print sentence and variable value in 1 line use python?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM