我有一個Python類如下Python類屬性不評估,如果條件
class Application(models.Model):
name = models.CharField(help_text="Application's name",max_length=200)
current_status = models.IntegerField(null=True, blank=True)
class Meta:
ordering = ["name"]
@property
def status(self):
"""Returns the current ApplicationStatus object of this Application."""
try:
return ApplicationStatus.objects.get(id = self.current_status)
except ApplicationStatus.DoesNotExist as e:
print e
return None
在另一大類我檢查的財產狀況,如下面的語句
app = Application()
if app.status is None:
#do some thing
else:
print app.status
雖然我確保應用程序的狀態不是無,其他打印語句打印無,當我嘗試訪問狀態像app.status.id時,應用程序會引發異常NoneType has no property id
。
當我改變了條件:
app = Application()
st = app.status
if st is None:
#do some thing
else:
#do another thing
它工作正常。
有人可以告訴我爲什麼python屬性不在print語句中評估嗎?
對不起Dave,編輯帖子以反映實際問題 – Fanooos 2010-11-10 10:21:19