I'm wondering why this code returns same id
of a
variable.
Doesn't interpreter suppose to clean a
from memory after f()
finished?
Is this behavior result of some CPython optimization? Can we force it to clean a
object?
gc.collect()
doesn't help in this. I thought that waiting for some time may change this behavior, but it doesn't.
import time
def f():
a = "wat wat wat wat wat wat wat wat"
print(id(a))
f()
for i in range(30):
time.sleep(1)
s = "/" if i % 2 == 0 else "\\"
print(s, end="", flush=True)
print()
f()
# Tested with python 3.10.8
# 140443262854256
# /\/\/\/\/\/\/\/\/\/\/\/\/\/\/\
# 140443262854256