13

Is there a way in to make copy of a variable so that when the value changes of variable 'a' it copies itself to variable 'b'?

Example

a='hello'

b=a     #.copy()  or a function that will make a copy

a='bye'

# Is there a way to make 
# 'b' equal 'a' without 
# doing 'b=a'

print a
print b

I am having a problem using the Tkinter library where I have checkbutton that has been stored in a list and I'm trying to get the variable that it holds.

But it takes around 5 lines of code to reach the variable.

Is there a way of holding a copy of the variable that changes when the checkbutton variable changes?

gsamaras
  • 69,751
  • 39
  • 173
  • 279
Brandon Nadeau
  • 3,328
  • 12
  • 40
  • 60
  • Why would you want that? just use `a`? – John Mee Nov 24 '12 at 04:30
  • possible duplicate of [Simulating Pointers in Python](http://stackoverflow.com/questions/1145722/simulating-pointers-in-python) – James Sumners Nov 24 '12 at 04:32
  • 1
    See [Python: How do I pass a variable by reference?](http://stackoverflow.com/q/986006) – Martijn Pieters Nov 24 '12 at 04:33
  • I cant. Its a problem I am facing using the tkinter library where I have checkbutton that has been stored in a list and I'm trying to get the variable that it holds but it takes around 5 lines of code to reach the variable. I just want a way of holding a copy of the variable that changes when the checkbutton variable chagnes – Brandon Nadeau Nov 24 '12 at 04:33
  • 1
    @Crispy: Then make *that* your question. We can help you much better with practical problems with an actual usecase. This question is too vague, really. – Martijn Pieters Nov 24 '12 at 04:34

5 Answers5

19

You're exploring how Python deals with references. Assignment is simply binding a reference to an object on the right hand side. So, this is somewhat trivial:

a = 'foo'
b = a
print b is a  #True -- They *are the same object*

However, as soon as you do:

b = 'bar'
b is a  #False -- they're not longer the same object because you assigned a new object to b

Now this becomes really interesting with objects which are mutable:

a = [1]
b = a
b[0] = 'foo'
print a  #What?? 'a' changed?

In this case, a changes because b and a are referencing the same object. When we make a change to b (which we can do since it is mutable), that same change is seen at a because they're the same object.

So, to answer your question, you can't do it directly, but you can do it indirectly if you used a mutable type (like a list) to store the actual data that you're carrying around.


This is something that is very important to understand when working with Python code, and it's not the way a lot of languages work, so it pays to really think about/research this until you truly understand it.

Peter Mortensen
  • 30,030
  • 21
  • 100
  • 124
mgilson
  • 283,004
  • 58
  • 591
  • 667
  • 2
    I came here to find a way to prevent the behavior in the third scenario (object to be copied is mutable). In case others wanted the same info, what I eventually found is that other than using copy.copy or copy.deepcopy, you may also use the following syntax to create a new object and negate the mutability of the original -- if the desired object can be instantiated in such a way of course: ```new_list = list(L) # copy new_dict = dict(olddict) # copy new_set = set(L) # convert list to set new_tuple = tuple(L) # convert list to tuple``` – Jacob J Sep 09 '20 at 01:39
2

In short, with simple value assignments, you cannot do this. As you saw:

a=4
b=a
a=5

>>> print b
4

However, with mutable objects like lists, you can do this. As such:

a=[1]
b=a
a.append(2)

>>> print a
[1,2]
>>> print b
[1,2]
jdotjdot
  • 15,178
  • 11
  • 63
  • 112
1

Depending on what you want to do, you might want to check the weakref module.

This allows you to have a primary object and then several copies that will become None as soon as the primary object is gone.

mgilson
  • 283,004
  • 58
  • 591
  • 667
vkontori
  • 1,106
  • 1
  • 10
  • 16
0

You are asking if it is possible to create a reference to a variable and hold in another variable. No, this is not possible. See e.g. Create a reference to a variable (similar to PHP's "=&")?

Community
  • 1
  • 1
djechlin
  • 57,408
  • 33
  • 153
  • 271
0

There is an ugly way to create an independent copy of a simple variable via a temporary list:

a = 6; 
b = [a][0]
Jeru Luke
  • 16,909
  • 11
  • 66
  • 79