0

If I have x=['f', 1], then module.x = x, would module.x be referencing x, or does it create a new list that references the objects inside x's list? If you get my question. This is in terms of like drawing a picture of how Python executes.

1 Answers1

0

When we do:

x = ['f', 1]
  1. It creates a list.
  2. x gets assigned it's reference.

When we do:

module.x = x

module.x gets assigned the same reference.

So, a new list is not created.


Run this code to clearly understand it:

class Temp:
    def __init__(self):
        self.x = None

x = ['f', 1]
print(id(x))    

temp = Temp()
temp.x = x

print(id(temp.x))

assert id(x) == id(temp.x)
Dipen Dadhaniya
  • 4,550
  • 2
  • 16
  • 24