2015-11-20 82 views
0

我需要使用python hasattr来实现我的特定目的。我需要检查一个对象是否有属性,并且不是有另一个属性。在python中使用hasattr而不是hasattr

考虑类对象命名model,我需要检查它是否具有属性称为domain_id

if hasattr(model, 'domain_id'): 

我还需要检查的一个条件,它不应该有属性,叫做type

if not hasattr(model, 'type'): 

如何在这里结合两个检查?

+0

你的问题标题已经有你的答案了。 –

+0

如果hasattr(model,'domain_id')不是hasattr(model,'type'):'work? –

回答

1

就结合两个条件与and:如果两个条件都为真

if hasattr(model, 'domain_id') and not hasattr(model, 'type'): 

if块将只执行。

相关问题