Home > Mobile >  How `print()` works?
How `print()` works?

Time:02-24

A python3.x code demo:

def fun():
    global x
    y.append(6)
    x = 6

x = 3
y = [3]
print(x, y, fun(), x, y)

The segment outputs:

3 [3, 6] None 6 [3, 6]

If the process of print() calculates all of the items and then outputs the items, then that explains why the result of y before fun() and after fun() are both [3, 6], as they are here. But why then are the values of x before and after the function fun() different?

CodePudding user response:

When you call a function, like print in your example, the arguments are evaluated from left to right.

Your call has x as an argument twice. The first one gets evaluated with the original value, 3. Then fun() gets called, which has the side effect of changing the global variable x. When you evaluate x again afterwards, you get the updated value, 6.

It might be a bit clearer if you used functions calls for all the arguments, as that would let you print some output too:

def get_x():
    print("x is", x)
    return x

def change_x():
    global x
    x  = 1
    print("x changed")

x = 0

print("args evaluated left to right:", get_x(), change_x(), get_x())

The output from that is:

x is 0
x changed
x is 1
args evaluated left to right: 0 None 1

CodePudding user response:

This link explains the variable assignment very well. https://nedbatchelder.com/text/names.html:

Fact: Python passes function arguments by assigning to them. For example :

def fn(a):
    pass
    
x = 10

When we say fn(x), it's going to be a = x. Another rule is the arguments are evaluated from left to right.

After saying those two rules, let me re-write the print function in a specific way that matches to our example:

def print_(var1, var2, var3, var4, var5, sep=' ', end='\n'):
    sys.stdout.write(str(var1)   sep)
    sys.stdout.write(str(var2)   sep)
    sys.stdout.write(str(var3)   sep)
    sys.stdout.write(str(var4)   sep)
    sys.stdout.write(str(var5)   sep)
    sys.stdout.write(end)

print_(x, y, fun(), x, y)

Python holds the value of the global x inside the local variable var1.

var1 = x    # `x` is 3 now

After the fun() function executes, the x in global changes to 6, but the point is var1 is still 3 not 6:

var1 = x = 3
x = 6
print(var1)  # 3

...and print_ is going to use it's local variables (var1 not x).

But from now on whenever Python sees the x, it stores 6 instead. so var4 is 6.

var4 = x    # `x` is 6 now

But since the y is a list and you mutated the list, both var2 and y are seeing the change ! Because they both point to the same object:

var2 = y = [3]
y.append(6)
print(var2)   # [3, 6]

CodePudding user response:

The function print copies arguments in local variables, but each arguments are different local variables and are evaluated left to right. What happens giving your line print( x, y, func(), x, y) is this :

1. x is evaluated and copied in local variable. xLocal1 = x = 3

2. y is evaluated and copied in local variable. yLocal1 = [3]

3. func() is evaluated, it changes x AND BOTH yLocal1 and y as there iss a global x that specifies it applies to only global variable and not local. x = 6, yLocal1 = [3,6], y = [3,6]. It changes the variable from last step

4. x is evaluated and copied in xLocal2. xLocal2 = x = 6

5. y is evaluated and copied in yLocal2. yLocal2 = y = [3,6]

6. And it finally prints all the local variables xLocal1, yLocal1, xLocal2 and yLocal2 that equals to 3, [3,6], 6, [3,6]

  • Related