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>
目录
相关文章
|
2月前
|
设计模式 Java 数据库连接
|
2月前
|
测试技术 Python
Python 类中__init__方法的作用
【8月更文挑战第24天】
36 0
|
5月前
|
算法 开发者 Python
【Python 基础扫盲 】self参数、__init__方法和.__str__方法的用处和区别?
【Python 基础扫盲 】self参数、__init__方法和.__str__方法的用处和区别?
298 0
|
Python
python 中__init__ ,__new__ ,__call__,__del__ 方法
python 中__init__ ,__new__ ,__call__,__del__ 方法
163 0
|
Python
Python的self作用,以及__init__,__new__
Python的self作用,以及__init__,__new__
46 0
|
Python
Python 中对象的比较操作 == 与 is
Python 中对象的比较操作 == 与 is
|
C++ Python
python类中初始化形式:def __init__(self)和def __init__(self, 参数1,参数2,,,参数n)区别
python类中初始化形式:def __init__(self)和def __init__(self, 参数1,参数2,,,参数n)区别
150 0
|
设计模式 Java Python
简述Python类中的 __init__、__new__、__call__ 方法
简述Python类中的 __init__、__new__、__call__ 方法
99 0
|
Python
Python类的特殊方法 init
Python类的特殊方法 init自制脑图 介绍了类对象的初始化;创建对象的流程;类的基本结构。
56 1
Python类的特殊方法 init
|
Python
Python描述符(__get__和__set__和__delete__)
Python描述符(__get__和__set__和__delete__)
104 0