【问题标题】:Delete attribute of a class using its instance使用其实例删除类的属性
【发布时间】:2015-06-06 02:51:25
【问题描述】:

我有一个类 QClass 和一个实例 QQClass

我在运行时以某种方式在QClass 中创建了一个属性an_attribute。 如何使用 del Q.an_attribute 删除 an_attribute

我知道从类中删除该属性会使其所有实例都无法访问它。

更新Q 暴露给用户,他们只能使用del Q.an_attribute。我只能更改QQClass 的代码。

【问题讨论】:

    标签: python


    【解决方案1】:

    python 中的deldelattr 更高效。见https://stackoverflow.com/a/1121068/4698026

    那就去吧

    del type(Q).an_attribute
    

    【讨论】:

      【解决方案2】:

      试试:

      delattr(Q, 'an_attribute')
      

      【讨论】:

        【解决方案3】:

        试试del type(Q).an_attribute

        type(Q) 将返回 QClass,然后你使用 del 与它。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2015-05-16
          • 2020-03-28
          • 1970-01-01
          • 1970-01-01
          • 2016-03-03
          • 1970-01-01
          • 2020-06-02
          • 1970-01-01
          相关资源
          最近更新 更多