实现使用引用计数来确定何时应删除变量。
在变量超出范围后(如您的示例),如果没有剩余的引用,则将释放内存。
def a():
x = 5 # x is within scope while the function is being executed
print x
a()
# x is Now out of scope, has no references and can Now be deleted
除了列表中的字典键和元素外,通常没有什么理由在Python中手动删除变量。