python对象容器和回收的详解
python对象容器和回收的详解
1、对象容器
有些对象包含对其他对象的引用,这些对象称为容器对象容器,如元组、列表和字典。很多时候,我们说容器的值,是指被引用对象的值,而不是引用编号。然而,在讨论容器可变性时,它仅指直接包含在容器中的参考号。例如,元组是不可变的。如果它包含对可变对象的引用,当可变对象改变时,容器的值将改变。
2、对象回收
对象不会被显式销毁,但它们将被视为垃圾回收。只要没有对象引用,这就是Python垃圾收集机制!垃圾收集机制比较复杂,比如循环引用,其实并没有用到,但是还是有引用的,涉及算法规则。写完PythonAdvanced,在写Python原理的时候做进一步的研究。
Python垃圾收集机制不是万灵药,不能解决所有问题。所以在引用外部资源后,比如打开文件,需要注意显式关闭,防止资源总是占用内存而无法释放,造成内存泄漏。关闭除了手动调用Close()方法外,还可以使用与来自动关闭。
使用try...除了可以保持物体活动。
以上就是python对象容器和回收的详解,希望能对大家有所帮助,更多Python学习教程请关注IT培训机构:千锋教育。

相关推荐HOT
更多>>
python函数标注是什么
python函数标注是什么在参数的使用方法中,还有一种解包的情况是需要我们掌握的。比如,将列表或者字典的值转换为函数的参数,就需要用到参数解...详情>>
2023-11-14 14:13:35
python正负索引的使用
python正负索引的使用本文教程操作环境:windows7系统、Python3.9.1,DELLG3电脑。1、说明使用正索引(从左至右)、负索引(从右至左)访问元素,时...详情>>
2023-11-14 13:45:04
python垃圾回收的机制过程
python垃圾回收的机制过程本文教程操作环境:windows7系统、Python3.9.1,DELLG3电脑。1、步骤(1)收集所有容器对象(list,dict,tuple,customClas...详情>>
2023-11-14 13:05:15
pythontry语句的执行原理
pythontry语句的执行原理1、原理分析(1)执行try子句(try和except关键字之间的多行语句)。(2)如果没有异常发生,则跳过except子句并完成try语句...详情>>
2023-11-14 09:37:35热门推荐
技术干货






