博客
关于我
第8.31节 Python中使用__delattr__清除属性数据
阅读量:93 次
发布时间:2019-02-26

本文共 1515 字,大约阅读时间需要 5 分钟。

__delattr__???Python?????????

????????????????getattribute?setattr????????????????????????Python????????????get?set?delete??delete??????????__delattr__???????????????????????????????????????????

__delattr__??????

__delattr__??????????????????????

??.__delattr__(???)

????????????????car.power?????__delattr__??????????????

car.__delattr__('power')

????

  • ????????????????????????__delattr__???????AttributeError?
  • ??????__delattr__???????????
  • ??????????????????????????????????
  • ????

    ?????Car???????__delattr__??????

    • ????

      class Car:    refcount = 0  # ???????__delattr__??    def __init__(self, power):        self.power = power        self.totaldistance = 0    def drive(self, distance):        print(f"In method drive: distance={distance}")        self.totaldistance += distance
    • ?????

      car = Car('?????')
    • ?????

      car.__dict__  # ??????????{'power': '?????', 'totaldistance': 0}
    • ???????

      car.__delattr__('power')  # ???AttributeError: '?????'

      ???????????????__delattr__????

    • ?????

      car.__delattr__('power')  # ??power??

      ???

      car.__dict__  # {'totaldistance': 0}
    • ??????????????????__delattr__???

      car.__delattr__('drive')  # ???AttributeError: 'drive'

    ??????????

  • ????__delattr__?????????????del ??.???????__delattr__??????????????????????

  • ????????Python?????????ARC??????????????__delattr__??????????????????

  • ??__delattr__?????????????????__delattr__???

    def __delattr__(self, name):    print(f"?????{name}")    super().__delattr__(name)
  • ??

    __delattr__????????????????????????????????????????????????????????????????????????????

    转载地址:http://iog.baihongyu.com/

    你可能感兴趣的文章
    python 基于 wordcloud + jieba + matplotlib 生成词云
    查看>>
    python 基于detectron或mask_rcnn的mask遮罩区域进行图片截取
    查看>>
    Python编程:Tkinter图形界面设计(2)
    查看>>
    Python 基础 - Day 5 Learning Note - 模块 之 标准库:time (1)
    查看>>
    Python 基础一
    查看>>
    Python 基础知识点整理与分享,期盼你的交流!
    查看>>
    python 基础第七篇
    查看>>
    Python编程:Tkinter图形界面设计(1)
    查看>>
    Python 基础语法:None
    查看>>
    Python 基础语法:基本数据类型(一)
    查看>>
    python编程读取写入excel_Python读取txt内容写入xls格式excel中的方法
    查看>>
    Python 基础语法:基本数据类型(字典)
    查看>>
    Python 基础语法:基本数据类型(字符串)
    查看>>
    Python 基础语法:基本数据类型(集合)
    查看>>
    Python编程的终极十大工具
    查看>>
    python 堆排序
    查看>>
    python编程方式之一-函数式编程
    查看>>
    python 复习计划
    查看>>
    Python 多处理 apply_async 永远不会在 Windows 7 上返回结果
    查看>>
    Python 多处理 Numpy 随机
    查看>>