您当前的位置: 首页 >  彭世瑜 Python

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

彭世瑜 发布时间:2019-06-12 10:38:57 ,浏览量:2

对象的生命周期: 创建、初始化、使用、垃圾回收

代码示例

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

参考 简述 init、new、call 方法

关注
打赏
1688896170
查看更多评论

彭世瑜

暂无认证

  • 2浏览

    0关注

    2727博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文
立即登录/注册

微信扫码登录

0.0801s