1

I am having a scope issue, and I know how I would solve this in Java, but Python is giving me some issues. Say I have a code as follows:

a = 1
b = 2
c = 3

list1 = [a,b,c]
list2 = [b,c]

b += 1

print list1
print list2

This code does not change the value inside the lists, and there is an easy workaround for the simple example I've given, but the code I am trying to write has 10+ lists that all have different meanings and are used in different ways, but need to communicate with each other and have all the same values for a,b,c. I need to be able to change the variable a,b,c and have all the lists update as well, is there any other way besides writing out the update for each list?

3
  • A list is probably not the data structure you want. Commented Jun 23, 2017 at 21:13
  • Ok, what's a better structure for this, dictionary? I could use numpy arrays, but I don't think that would solve the 'scope' problem I'm encountering Commented Jun 23, 2017 at 21:16
  • Without more info on your use case, it's hard to say. Commented Jun 23, 2017 at 21:20

1 Answer 1

2

You cannot store the integers by reference, such that incrementing the value in one place causes all other values to be updated. This is because ints are immutable, so changing their value causes a reference change.

>>> a = 5
>>> b = 1
>>> x = [a, b]
>>> id(b)
140508116572264
>>> id(x[1])
140508116572264
>>> b += 1
>>> id(b)
140508116572240

You can see the id of b changes after the increment, so the "b" in the loop and the "b" outside aren't the same.

What you can do, is define a wrapper class for your ints.

class MyInteger:
    def __init__(self, value):
        self.value = value

Now, build your lists with your MyIntegers:

a = MyInteger(1)
b = MyInteger(2)
c = MyInteger(3)

list1 = [a, b, c]
list2 = [b, c]

Now, when you increment the value of b, you can see the changes are reflected in the lists.

print([x.value for x in list1])
b.value += 1
print([x.value for x in list1])

This prints:

[1, 2, 3]
[1, 3, 3]
Sign up to request clarification or add additional context in comments.

5 Comments

Amazing! This should be a relatively simple workaround. So quick question, if I change the value of a list such as list1[1]+=1 would it change it everywhere? If not would a structure like: for i in list1: i += 1 do the trick?
list1[1].value += 1, and yes. It will. i.value += 1 also works, because they reference the same custom object.
Thanks I'm new. Quick Q on that class you've made, I get an error "undefined name 'self'" and "undefined name 'value'", I don't usually craft independently made classes, is this an acceptable error or did I miss something?
No, that is definitely not supposed to happen. You generally shouldn't use self outside the class. value can be called as soon as the object is created. Is the code in your machine as I have pasted it here?
Copied and pasted instead of typing what I saw and it fixed it, maybe the __ was an issue I think I may have typed one _ instead of two. Thanks for the help, error gone, answer accepted, time appreciated. Cheers!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.