Python: object lifetime new-init-call-del

Disclaimer: This article is a blogger original article, welcome to reprint, please indicate the source https://blog.csdn.net/mouday/article/details/91491211

Lifetime of an object:
create, initialize, use, garbage collection

The sample code

# -*- 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__
"""

Referring
briefly the init , new new , Call method

Guess you like

Origin blog.csdn.net/mouday/article/details/91491211