4

I have two integers x and y that are equal to each other. Whenever I change x I want the change to also be reflected in y. How can I do that with Python?

I think this can be achieved somehow by pointing both variables to the 'memory location' where the value is stored.

Hassaan
  • 253
  • 4
  • 13
  • Possible duplicate of [How to trigger function on value change?](http://stackoverflow.com/questions/6190468/how-to-trigger-function-on-value-change) – Jossef Harush Kadouri Feb 28 '16 at 05:39
  • 2
    What is the data type? For mutable objects such as lists, you are already there. For immutable types such as integers and strings, you'll need a container. – tdelaney Feb 28 '16 at 05:42
  • Updated the question – Hassaan Feb 28 '16 at 05:49
  • 2
    I think this is, ironically, an [XY problem](http://meta.stackexchange.com/questions/66377/what-is-the-xy-problem). You can't assign a variable to a memory location and you can't retrieve an object at a memory location. You can't use "pointers" as you might in C. I think you're going to have to explain *why* you need these variables to be equal and "where" they exist relative to each other. – Jared Goguen Feb 28 '16 at 06:42

5 Answers5

1

lists and dictionaries act as pointers in python so a hacky version of what you're attempting might look like this:

In [16]: x = y = [0]

In [17]: x
Out[17]: [0]

In [18]: y
Out[18]: [0]

In [19]: x[0] = 10

In [20]: y
Out[20]: [10]

In [21]: x
Out[21]: [10]
fivetentaylor
  • 1,277
  • 7
  • 11
0

Encapsulate both in a function or such ... read/write is controlled through the function thus assuring the behavior you require

Scott Stensland
  • 26,870
  • 12
  • 93
  • 104
0

From Rosetta Code:

Python does not have pointers and all Python names (variables) are implicitly references to objects. Python is a late-binding dynamic language in which "variables" are untyped bindings to objects. (Thus Pythonistas prefer the term name instead of "variable" and the term bind in lieu of "assign").

From what I can infer, when working with integers, you can't point y to x and do something to x and see the changes happen to y as well.

At the very best, you can change x and y simultaneously, in 2 ways:

  1. Multiple assignments (bindings)

    >>> x = y = 9
    >>> print (x, y)
    (9, 9)
    >>> x = 6  # won't change y
    >>> print (x, y)
    (6, 9)
    
  2. Tuple unpacking

    >>> x, y = [9] * 2
    >>> x, y = (9, ) * 2  # alternative
    >>> print (x, y)
    (9, 9)
    >>> x = 6  # y says "nice try, x"
    >>> print (x, y)
    (6, 9)
    
Eddo Hintoso
  • 1,442
  • 14
  • 22
0

You can't.

You're going to have to figure out another way to keep track of things. I think you probably could be structuring your code to use a mutable data type, like a list or dict, or perhaps create a custom one:

class MutableInt:
    def __init__(self, value=0):
        self._value = value

    def __add__(self, other):
        self._value += other

    def __repr__(self):
        return str(self._value)

mutable_int = MutableInt(4)
print(mutable_int) # 4

def addOne(mutable_int):
    mutable_int += 1

addOne(mutable_int)
print(mutable_int) # 5
Community
  • 1
  • 1
Jared Goguen
  • 8,772
  • 2
  • 18
  • 36
-1

You can change the x or y via a function, try the following code:

def change_x(value):
    global y
    y = value
    return value

def change_y(value):
    global x
    x = value
    return value

x = y = 3
print(x,y)
x = change_x(5)
print(x,y)
y = change_y(6)
print(x,y)
Ren
  • 2,852
  • 2
  • 23
  • 45
  • 1
    In general, the use of `global` is less-than-ideal. It would be better to fix a bad structure than to try to work around it with `global`. – Jared Goguen Feb 28 '16 at 06:45