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.

4
  • Possible duplicate of How to trigger function on value change? Commented Feb 28, 2016 at 5: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. Commented Feb 28, 2016 at 5:42
  • Updated the question Commented Feb 28, 2016 at 5:49
  • 2
    I think this is, ironically, an 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. Commented Feb 28, 2016 at 6:42

5 Answers 5

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]
Sign up to request clarification or add additional context in comments.

Comments

0

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

Comments

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)
    

Comments

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

Comments

-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)

1 Comment

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.

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.