我有以下假设的Python程序,它使用了一些垃圾收集器功能(文档):
import gc
# Should turn automatic garbage collection off
gc.disable()
# Create the string
a = "Awesome string number 1"
# Make the previously assigned string unreachable
# (as an immutable object, will be replaced, not edited)
a = "Let's replace it by another string"
# According to the docs, collect the garbage and returns the
# number of objects collected
print gc.collect()
Run Code Online (Sandbox Code Playgroud)
程序打印0,这对我来说很奇怪,因为:
str创建对象并引用该对象a.str创建第二个对象a,现在由其引用a …我正在运行以下程序.重要的是,假设mymodule.py目录中存在这两个程序所在的文件.
第一个:
exec('''import sys
import os
os.chdir('/')
sys.path = []
import mymodule''', {})
Run Code Online (Sandbox Code Playgroud)
第二个:
import mymodule
exec('''import sys
import os
os.chdir('/')
sys.path = []
import mymodule''', {})
Run Code Online (Sandbox Code Playgroud)
第一个片段ImportError按预期引发(毕竟,mymodule所在的目录不在路径中).然而,第二个片段没有,即使mymodule也不在它的路径中,我给它的环境是空的.
我的问题是为什么