4

I don't understand why the variable 'y' doesn't update when I change the x? (The 'y' variable is dependent on 'x' right?)

x = 5
y = x*2

print(x)
print(y)

x = 3

# Expect it to print '3' and '6' instead it print '3' and '10'
print(x)
print(y)
  • 1
    _The 'y' variable is dependent on 'x' right?_ No, `y` is created based on the `x` value, but after that they're only friends. – bereal May 27 '20 at 12:19
  • immutable objects like `int`, `float`, `string` are saved by value not by reference. So, when you change `x`, the value of unlike mutable objects – Anwarvic May 27 '20 at 12:19
  • in your first declaration you can think of it as simple `y = 5*2` instead of `y=x*2`. After the initial declaration they have nothing to do with each other. – Ahmet May 27 '20 at 12:20
  • See also: [Variable not changing after assigning another value to its dependent variable](https://stackoverflow.com/q/46374966/7851470) – Georgy May 27 '20 at 13:07

4 Answers4

8

(The 'y' variable is dependent on 'x' right?

No.

Few programming languages have dependent / computed variables[0] and Python is not one of them[1]. When y = x*2 is executed, the expression on the right-side of the = is fully evaluated and the result set as the value of y. y is thereafter independent from x[2].

Generally speaking, if you want y to be a function of x... you define it as a function of x:

x = 5
def y(): return x*2

print(x)
print(y())

x = 3

# Expect it to print '3' and '6' instead it print '3' and '10'
print(x)
print(y())

  1. I know of make's lazy variables and Perl's tied scalars
  2. it does have computed attributes (aka properties) but that's a very different thing
  3. There are situations which kind-of look like dependent variables e.g. if you set y to a mutable sub-structure of x changes to this sub-part of x will be visible through y. That's not actually a dependency though, it's just that the two variables point to the same (mutable) structure, so both "see" mutations applied to that shared structure.
Masklinn
  • 34,759
  • 3
  • 38
  • 57
  • Just to elaborate on footnote [2]. An example of this is when you have a list such as ```x = [1, 2, 3]```. Setting ```y = x``` causes both variables to point to the same mutable structure. So if you do something like ```x[0] = 10``` followed by ```print(y[0])```, you will get an output of 10, not 1. – Desmond Cheong May 27 '20 at 13:35
1

The y variable is dependent on x right?

Well, first things first, if you set:

a = 7
b = a
a = 4

Then,

print(id(a)) and print(id(b)), you will get two different ids, hence b will not change when you overwrite a.

Red
  • 26,798
  • 7
  • 36
  • 58
0

Your y points to an older version of the x-variable. To update y, you could do:

x = 5
y = x * 2

print(x)
print(y)

x = 3
y = x * 2

print(x)
print(y)

Which will output:

5
10
3
6
Gustav Rasmussen
  • 3,720
  • 4
  • 23
  • 53
0

Because after you assign a value to variable y, in order to change it you will need to access directly to y. y= x*2 is assigning value to y according to what the value of x in this line of code. this how python works, there are other code languages who can preform like u expected. after assigning the new value to x , you will need to write again y=x*2.

marksman123
  • 389
  • 2
  • 11