0

I started lately to use Python instead of Matlab and I have a question to which the answer might be obvious but I can't figure it out yet.

I have the following module in python called shared_variables.py:

global a 
global b 

a = 2 
b = 3 
c = a
d = b

in my main.py script I do the following things:

import shared_variables

for i in range(1,4):
     shared_variables.a += 1
     shared_variables.b += 1
     print 'a= ',shared_variables.a
     print 'b= ',shared_variables.b
     print 'c= ',shared_variables.c
     print 'd= ',shared_variables.d

and the output is the following:

 a=  3
 b=  4
 c=  2
 d=  3
 a=  4
 b=  5
 c=  2
 d=  3
 a=  5
 b=  6
 c=  2
 d=  3

Basically c and d values are not updated at each iteration. How can I solve this problem? I am asking this question because I have written a longer program in which I need to share common values between different modules that i need to update at each different iteration.

6
  • 3
    global a and global b have no meaning outside of functions. Commented Dec 9, 2013 at 14:42
  • What are you trying to achieve? What's the bigger picture? Do you know about functions and classes in Python? Commented Dec 9, 2013 at 14:44
  • Sorry, in the actual code I removed global a and global b but still I have the same problem :/ Commented Dec 9, 2013 at 14:45
  • 2
    Maybe having these variables in a shared/imported module is not the best way to go. You should read about Python functions and classes. It looks like you will end up with a convoluted solution because you don't yet know how to structure your code with functions/classes. You would typically have an object of some kind to hold these values and pass it into to your functions. Or you would write functions that returned the values to other functions. Commented Dec 9, 2013 at 14:53
  • 1
    I see! I will read about classes and objects then. It seems the correct way! Thanks a lot for all the answers! Commented Dec 9, 2013 at 15:20

3 Answers 3

5

The following lines set the values of the variables once (e.g., assign the current value of a to c):

a = 2 
b = 3 
c = a
d = b

It does not mean that c changes whenever a changes nor that d changes whenever b changes. If you want variables to change value you'll need to assign a new value to them explicitly.

Sign up to request clarification or add additional context in comments.

5 Comments

Note that the OP is using in-place addition; a += 1 and b += 1. This often creates the impression that you are actually altering the value; mutating the integer.
It is incorrect. c = a does mean that c changes whenever a changes. They refer to the same object. See my answer
@J.F.Sebastian my answer applies to integers, not mutable objects. For mutable objects it does indeed mean modifying a is "reflected" in c.
@SimeonVisser: it is also incorrect. After a = c a is c (single, uno object, just different names). Nothing to reflect but itself.
@J.F.Sebastian I understand, maybe my choice of "reflected" wasn't a good choice. In memory they're indeed the same.
3

Integers are immutable in Python. You can't change them.

a += 1 is a syntax sugar for a = a + 1 i.e., after the assignment a is a different int object. c is not a anymore.

If a and c were mutable objects such as lists then changing a would change c. c = a makes c and a both to refer to the same object i.e., c is a.

For example,

a = [0]
c = a
a[0] += 1 
print(a, c) # -> [1] [1]

Here are nice pictures to understand the relation between names and objects in Python

Comments

2

c and d start out as references to the same value as a and b, not to the same memory position. Once you assign new values to a and b, the other two references won't follow.

Python values are like balloons, and variable names are like labels. You can tie a thread between the label (name) and the balloon (value), and you can tie multiple labels to a given balloon. But assignment means you tied a new balloon to a given label. The other labels are not re-tied as well, they still are tied to the original balloon.

Python integers are immutable; they remain the same balloon throughout. Incrementing a by adding 1 with the in-place addition operator (a += 1) still has to find another balloon with the result of a + 1, and tie a to the new integer result. If a was tied to a balloon representing 2 before, it'll be replaced by a new balloon representing the value 3, and a will be retied to that. You cannot take a marker to the integer balloon and erase the 2 to replace it with 3.

3 Comments

c and d refer to the exact same objects as a and b correspondingly (or as you call it "to the same memory position" if it makes sense for a particular Python implementation, try it id(a) == id(c) and a is c). The issue is a += 1 makes a refer to a different object (due to int immutability). a and c are no longer the same object.
@J.F.Sebastian: Exactly, that is what I am saying. The thing is that some people coming from C think that variables are memory positions. Two references to the same memory position will see the same value as one or the other is updated.
I meant your first sentence is wrong e.g., in CPython a and c literally refer to the same "memory position" (id returns that position).

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.