Reputation: 13
In Python, when you write x=10
, it reserves a memory location and essentially stores 10, right? Then, if you write x=20
will 20 replace the value of 10 (like C/C++ does) or will it write 20 to a new memory location and consider the old 10 as garbage?
Thanks in advance ;)
Upvotes: 1
Views: 57
Reputation: 369633
You don't know. The Python Language Specification does not talk about things like "memory location" or "address".
It simply specifies the semantics of the code. Implementors are free to implement those semantics however they may wish.
For GraalPython, for example, I would guess that the compiler would completely optimize away the variable.
Upvotes: 0
Reputation: 1151
You do not have to manually free memory that you use.
Perhaps this is useful also.
The process of freeing memory when it is not used anymore. Python performs garbage collection via reference counting and a cyclic garbage collector that is able to detect and break reference cycles.
Sample on allocation (ints are immutable)
something=10
print(id(something)) # memory address
something=12
print(id(something))
140159603405344
140159603405408
Upvotes: 1