0
var1 = 1
var2 = var1 # why doesn't this bind the two variables?
var2 = var2 + 3 
print(var1)
print(var2)

The output is

1

4

What is the Python 3.x (added after StackrunnethOver's original answer - apologies for not specifying) syntax to bind var2 to the same memory as var1? Not that I want to, but rather that I want to make sure that my assignment statements aren't accidentally binding two variables to one another.

Community
  • 1
  • 1

1 Answers1

0

This line of questioning (how do I know what happens to variables and when) is usually very language-dependent, so it’s often a lot more relevant to talk about it in the context of a specific language. However, in the example you present, I think there is a general answer:

For the code:

var1 = 1
var2 = var1
var2 = var2 + 3

On line 2, there are two things that could happen:

  1. var2 gets assigned the value 1
  2. var2 now points to the same location in memory as var1, and var1’s location in memory holds the value 1

If #1 happens, we get the behavior you see above. This should not be surprising.

If #2 happens, now var1 and var2 point to the same location in memory. Your question is why isn’t this the case? Consider, if line 2 does make var2 point to the same thing as var1, what does line 3 do?

...

If x = y makes x point to the thing y points to, what does x = y + 3 do? Make x point to the thing y + 3 points to? This doesn’t really make sense.

What this gets at, underneath, is the difference between a value, a variable, and a pointer. In the example you give, var1 and var2 are variables, which on the left hand side of an equals sign are treated as such (you can assign values to your variables e.g. x = 3). But on the right-hand side of an equals sign, variables become their values before other operations take place. In var2 = var1 + 3, the expression becomes var2 = 1 + 3 and then var2 = 4.

In some languages, this behavior is not always automatic. Instead, you have to create it for yourself, by using pointers. This effectively means you have one way of referencing a variable x which explicitly says you want the location in memory x points to, which is the true “value of x”, and another way of referencing it x’ which explicitly says you want the value at that location in memory.

In such a world, your code could be written as

var1’ = 1
var2’ = var1’
var2’ = var2’ +  3

To get behavior #1 above, or

var1’ = 1
var2 = var1
var2’ = var2’ + 3

To get output

4

4

All this assuming that var1 and var2 have been initialized beforehand.

In such languages (like C) you can actually do things like `x = y + 3’, where x and y are pointers. It literally makes x equal to the position in memory three spots further along than y. This is called pointer arithmetic.

Community
  • 1
  • 1
MyStackRunnethOver
  • 4,872
  • 2
  • 28
  • 42
  • First time seeing backtick used this way in Python (I'm using Pythong 3.x - forgive me for not mentioning that in my original question). Trying to wrap my head around what you're telling me. var1` = var2' binds var1 to the memory location of var2 - such that what happens to either is happening to both? If 'yes', then why do I need it here: var2' = var2' + 3 when I'm simply looking for default Python behavior? – James Bell Dec 30 '19 at 15:55
  • So explicitly: the above is not Python. It’s just pseudo code. Check out [Are there pointers in Python?](https://stackoverflow.com/questions/3106689/pointers-in-python/3107534) - at some point someone’s gonna tell you there are pointers in C and Python is built on C so they must be *somewhere* - you heard it here first. BUT keep in mind one of the major decisions of the Python language is (similar to Java) to spare you from worrying about questions like this – MyStackRunnethOver Dec 31 '19 at 17:28