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.
Asked
Active
Viewed 28 times
0
-
What is `module`? Please give a complete minimal example. – DYZ Oct 09 '19 at 05:38
-
Short answer is `module.x` is referencing to `x`. In other words, if you change `x` it will change `module.x` as well – Anwarvic Oct 09 '19 at 05:42
-
@DYZ, I think he/she means a member variable of an object – Anwarvic Oct 09 '19 at 05:43
1 Answers
0
When we do:
x = ['f', 1]
- It creates a list.
x
gets assigned it'sreference
.
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