检查方法是否已经在 object 中?

Check if method already is in instance in object?

我正在尝试查看我的 object 的属性实例是否已经存在。正如您在下面看到的,如果我的 Dog object 具有特定属性,我想通过 do_something_if_has_aged 方法执行某些操作。如何检查某个属性是否已经声明?通常你会用这样的东西检查是否存在,returns False:

obj = None
if obj:
    print(True)
else:
    print(False)

这是我的最小可重现示例:

>>> class Dog:
    def __init__(self, name, age):
        self.name = name
        self.age = age
    def add_years(self, years):
        self.age += years
        self.has_aged = True
    def do_something_if_has_aged(self):
        if self.has_aged:
            print("The dog has aged and is %d years closer to death" % self.years)
        else:
            print("The dog hasn't aged, apparently.")


>>> dog = Dog('Spot', 3)
>>> dog.age
3
>>> dog.do_something_if_has_aged()
Traceback (most recent call last):
  File "<pyshell#193>", line 1, in <module>
    dog.do_something_if_has_aged()
  File "<pyshell#190>", line 9, in do_something_if_has_aged
    if not self.has_aged:
AttributeError: 'Dog' object has no attribute 'has_aged'
>>> dog.add_years(1)
>>> dog.age
4
>>> dog.do_something_if_has_aged()
The dog hasn't aged, apparently.

很明显这只狗 已经老了

如果标题没有反映我下面要表达的意思,我深表歉意;我是 OOP 新手。

您似乎在寻找 hasattr 内置函数:

>>> class Dog(object):
...     pass
...
>>> a = Dog()
>>> hasattr(a, 'age')
False
>>> a.age = 7
>>> hasattr(a, 'age')
True

您的情况可以修改如下:

def do_something_if_has_aged(self):
    if hasattr(self, 'has_aged'):
        pass # do your logic

我会重写 __init__ 方法以包含 self.has_aged = False 以避免必须进行检查:

class Dog(object):
    def __init__(self, name, age):
        self.name = name
        self.age = age
        self.has_aged = False # Starting value so it is guaranteed to be defined (unless explicitly deleted).

现在,您 class 的其余部分应该可以正常工作了。但是,如果你想查看某个对象上是否已经定义了一个属性,你可以这样写:

class Foo(object):
    def set_bar(self):
        self.bar = True # Define the attribute bar if it is not yet defined.

    def is_bar_set(self):
        return hasattr(self, 'bar')

检查使用 hasattr 是否完全正确,但如果您正在为您的代码寻找快速修复,您可以事先将变量初始化为 false:

class Dog:
  has_aged = False

还有修复你的情况,因为我认为它应该被扭转:

def do_something_if_has_aged(self):
  if self.has_aged:    # instead of not self.has_aged
    print("The dog has aged and is %d years closer to death" % self.years)
  else:
    print("The dog hasn't aged, apparently.")

不测试属性,而是在 class 上设置默认值;如果缺少实例属性 Python 则查找 class 属性:

class Dog:
    has_aged = False  # default for all instances
    def __init__(self, name, age):
        self.name = name
        self.age = age
    def add_years(self, years):
        self.age += years
        self.has_aged = True  # sets an instance attribute
    def do_something_if_has_aged(self):
        if self.has_aged:
            print("The dog has aged and is %d years closer to death" % self.years)
        else:
            print("The dog hasn't aged, apparently.")

(请注意,我必须颠倒你的测试,如果 self.has_agedtrue 你想进入第一个分支,而不是相反)。

或者您可以在 __init__:

中为属性设置默认值
class Dog:
    def __init__(self, name, age):
        self.name = name
        self.age = age
        self.has_aged = False
    def add_years(self, years):
        self.age += years
        self.has_aged = True
    def do_something_if_has_aged(self):
        if self.has_aged:
            print("The dog has aged and is %d years closer to death" % self.years)
        else:
            print("The dog hasn't aged, apparently.")

您还可以通过 hasattr() function:

测试属性是否存在
def do_something_if_has_aged(self):
    if hasattr(self 'has_aged') and self.has_aged:
        print("The dog has aged and is %d years closer to death" % self.years)
    else:
        print("The dog hasn't aged, apparently.")

或使用具有默认值的 getattr() function

def do_something_if_has_aged(self):
    if not getattr(self 'has_aged', False):
        print("The dog has aged and is %d years closer to death" % self.years)
    else:
        print("The dog hasn't aged, apparently.")

但是,动态测试属性不应该是您选择的第一个选项;使用 class 默认值会更干净。