我试图查看我的对象是否已存在属性的实例。如下所示,如果我的Dog
对象具有某个属性,我希望通过do_something_if_has_aged
方法执行某些操作。如何检查某个属性是否已被声明?通常你会检查是否存在这样的东西,它返回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的新手。
答案 0 :(得分:4)
看起来您正在寻找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
答案 1 :(得分:3)
不是测试属性,而是在类上设置默认值;如果缺少实例属性,Python会查找类属性:
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_aged
true 您希望进入第一个分支,而不是相反)。
或者您可以在__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.")
但是,动态测试属性不应该是您选择的第一个选项;拥有类默认值更清晰。
答案 2 :(得分:1)
我会重写__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 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')
答案 3 :(得分:1)
要检查使用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.")