0

I need to equal the memory address of two variables... I mean If the value of the first variable changes, the second variable will also take the same value. For example :

    a = 10

    b = 10

    # now addresses are same

    a = 5
    print(b)  # must print 5

I want to do something like this: id(a) = id(b)

  • 3
    Why do you want to do this? You cannot. – mozway Jan 25 '23 at 15:41
  • 1
    The Python data model doesn't have any notion of memory addresses. Variables in Python simply do not work they way you are expecting. See https://nedbatchelder.com/text/names.html. – chepner Jan 25 '23 at 15:42
  • 4
    The fact that `a` and `b` refer to the same `int` object is an implementation-specific optimization of CPython, not something guaranteed by the Python language itself. – chepner Jan 25 '23 at 15:42
  • I don't believe you can automatically bind two variables together this way. Also only ***small*** integers will be actually share the same memory. – PM 77-1 Jan 25 '23 at 15:45
  • 1
    You could get *close* to this, in very specific use cases, for an instance attribute paired with a `property` that accesses it. But it'll never work for top-level names like this. The fact that you want to do it at all likely indicates [an XY problem](https://meta.stackexchange.com/q/66377/322040). – ShadowRanger Jan 25 '23 at 15:53

0 Answers0