0

我创建了一个类

def __init__(self, label):
        self.label = (label)

def add_thing(self,category_type):
    if category_type == somthing or category_type == the other thing:
        self.category = category_type

但是在某个地方出现了一个错误/错误,因为我在其他一些函数中这样做了:

def foo():
    if hasattr(O - the Object created, 'the attribute'):
        do something nice to O.the attribute = gorgeous stuff

add_thing 以前是“属性”

问题是我已经在许多功能中完成了这个hasattr检查。(可以说超过2个)检查是在“属性”上。自然它不存在,所以我需要将它们全部重命名为 add_thing,这很乏味。

我想知道它是我应该以某种方式在所有这些函数上使用的属性描述符来解决这种情况还是其他一些机制?(我不知道我一直在试图弄清楚如何申请财产,但似乎在这里找不到合适的例子)

4

0 回答 0