2015-11-20 79 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塊將只執行。

相關問題