Python:对象的生命周期new-init-call-del

简介: Python:对象的生命周期new-init-call-del

对象的生命周期:

创建、初始化、使用、垃圾回收


代码示例

# -*- coding: utf-8 -*-

class Demo(object):
    # 创建 反回 类的实例对象
    def __new__(cls, *args, **kwargs):
        print("__new__")
        return super(Demo, cls).__new__(cls, *args, **kwargs)

    # 初始化 只能反回 None
    def __init__(self):
        print("__init__")

    # 使用
    def __call__(self, *args, **kwargs):
        print("__call__")

    # 垃圾回收
    def __del__(self):
        print("__del__")


if __name__ == '__main__':
    demo = Demo()
    demo()
"""
__new__
__init__
__call__
__del__
"""

参考

简述 initnewcall 方法

            </div>
目录
相关文章
|
1月前
|
算法 开发者 Python
【Python 基础扫盲 】self参数、__init__方法和.__str__方法的用处和区别?
【Python 基础扫盲 】self参数、__init__方法和.__str__方法的用处和区别?
20 0
|
5月前
|
Python
49 python - __del__()方法
49 python - __del__()方法
22 0
|
8月前
|
Python
Python的self作用,以及__init__,__new__
Python的self作用,以及__init__,__new__
35 0
|
Python
python 中__init__ ,__new__ ,__call__,__del__ 方法
python 中__init__ ,__new__ ,__call__,__del__ 方法
131 0
|
8月前
报错AttributeError: Can‘t pickle local object ‘Worker.__init__.<locals>.<lambda>‘解决办法
报错AttributeError: Can‘t pickle local object ‘Worker.__init__.<locals>.<lambda>‘解决办法
229 0
|
10月前
|
Python
Python 中对象的比较操作 == 与 is
Python 中对象的比较操作 == 与 is
|
10月前
|
C++ Python
python类中初始化形式:def __init__(self)和def __init__(self, 参数1,参数2,,,参数n)区别
python类中初始化形式:def __init__(self)和def __init__(self, 参数1,参数2,,,参数n)区别
116 0
|
设计模式 Java Python
简述Python类中的 __init__、__new__、__call__ 方法
简述Python类中的 __init__、__new__、__call__ 方法
81 0
|
Python
Python描述符(__get__和__set__和__delete__)
Python描述符(__get__和__set__和__delete__)
90 0
|
Java Python
Python:对象的生命周期new-init-call-del
Python:对象的生命周期new-init-call-del
55 0