[Python 学习笔记] 3: 三元表达式?
2009-10-13 00:00:00 来源:WEB开发网核心提示:方式1>>>s="">>>a=sor"notnone">>>a'notnone'>>>s=None>>>a=sor"notnone">>>
方式1
>>> s = ""
>>> a = s or "not none"
>>> a
'not none'
>>> s = None
>>> a = s or "not none"
>>> a
'not none'
>>> s = "aa"
>>> a = s or "not none"
>>> a
'aa'
方式2
>>> s = None
>>> a = "not null" if s == None else s
>>> a
'not null'
>>> s = "aa"
>>> a = "not null" if s == None else s
>>> a
'aa'
好像 方式2 更接近 "? :" 这种三元表达式。
更多精彩
赞助商链接