The thing I've noticed is that print()
is sometimes outputs final calculated value when multiple statements are printed. To illustrate:
a = [1, 2, 3]
print(a, a.reverse(), a)
Output is [3, 2, 1] None [3, 2, 1]
, but shouldn't the first output of a
be [1, 2, 3]
?
If prints are separated, say:
a = [1, 2, 3]
print(a, end=' ')
print(a.reverse(), a)
The output gives correct [1, 2, 3] None [3, 2, 1]
.
This behavior caused me a couple of hard minutes trying to figure out the bug. I've searched all around for the answer, but supposedly I'm looking in the wrong direction.
Is this because values are calculated before print()
? In if so, why?
UPDATE:
I've asked the question because from other languages experience the reason of this isn't so obvious for me. For example, PHP:
$a = [1, 2, 3];
var_dump(
$a, array_pop($a), $a
);
Produces output without pre-executing expressions:
array(3) {[0]=>int(1) [1]=>int(2) [2]=>int(3)}
int(3)
array(2) {[0]=>int(1) [1]=>int(2)}
While question might be stupid, I don't see any more answers to it around except for this one on SO.