博客
关于我
第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 3.2 中的蛮力脚本
    查看>>
    Python 3.4 多处理递归 Pool.map()
    查看>>
    Python 3.4:未知格式代码“x“
    查看>>
    Python 3.5、ldap3 和 modify_password()
    查看>>
    python 3.6.8 升级至3.9版本升级
    查看>>
    Python 3.9 到 Python 3.12 的发展历程与区别
    查看>>
    python 32位和64位的区别在哪
    查看>>
    Python 3:何时使用 dict,何时使用元组列表?
    查看>>
    Python 3d 绘图 - 轴居中
    查看>>
    python ==》 字典
    查看>>
    python anaconda 安装使用
    查看>>
    python and或or 当参数传递的时候的用法
    查看>>
    Python append() 与列表上的 + 运算符,为什么这些会给出不同的结果?
    查看>>
    Python APP自动化测试工具adb与Monkey使用详解
    查看>>
    Python APP自动化测试框架Appium详解
    查看>>
    Python APP自动化测试框架开发实战
    查看>>
    python argparse模块
    查看>>
    Python asyncio库的学习和使用
    查看>>
    Python AttributeError:“dict“对象没有属性“append“
    查看>>
    Python base64和hashlib模块
    查看>>