有人可以解释以下代码中word
语句if word:
上正在执行的检查吗?
def simplify(text, space=" \t\n\r\f", delete=""):
result = []
word = ""
for char in text:
if char in delete:
continue
elif char in space:
if word:
result.append(word)
word = ""
else:
word += char
if word:
result.append(word)
return " ".join(result)
答案 0 :(得分:7)
python中的非空字符串始终为True,否则为False。因此,如果word仍为空字符串,则为False,否则为True。
答案 1 :(得分:1)
为什么不在Python的REPL中尝试自己呢(这是同类语言和语言之一):
Python 2.7.6 (default, Mar 22 2014, 22:59:56)
[GCC 4.8.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> word = ""
>>> if word: print "I'm here"
...
>>> word = "not empty"
>>> if word: print "I'm here"
...
I'm here