从学习Python开始
>>> x = 42
>>> x = 'shrubbery' # Reclaim 42 now?
Because Python caches and reuses small integers and small strings, as mentioned earlier, the object 42 here is probably not literally reclaimed; instead, it will likely remain in a system table to be reused the next time you generate a 42 in your code. Most kinds of objects, though, are reclaimed immediately when they are no longer referenced; for those that are not, the caching mechanism is irrelevant to your code.
当不再被引用时,CPython会缓存哪些类型的对象
什么样的对象不再被引用时CPython不会缓存
CPython是否仅在对象不可变时才缓存不再引用的对象? 或者它与对象的易变性无关
谢谢
不变性是其中的一个重要部分:您不希望新的空集是对现有空集的引用,只希望看到它被更新,而当“原始”被更新时,您却不知道它被更新了
但是,CPython缓存的对象通常不是在程序生命周期中创建的任意对象。小整数是在启动时创建的,不管它们最终是否被使用(我不知道小弦的事;它们可能只在必要时创建,但从那时起会被缓存。)
相关问题 更多 >
编程相关推荐