Chinaunix
标题:
python里的单个下划线 “_” 有何用处
[打印本页]
作者:
leooys
时间:
2011-06-29 15:57
标题:
python里的单个下划线 “_” 有何用处
import random
articles = ["the", "a", "another", "her", "his"]
subjects = ["cat", "dog", "horse", "man", "woman", "boy", "girl"]
verbs = ["sang", "ran", "jumped", "said", "fought", "swam", "saw",
"heard", "felt", "slept", "hopped", "hoped", "cried",
"laughed", "walked"]
adverbs = ["loudly", "quietly", "quickly", "slowly", "well", "badly",
"rudely", "politely"]
for _ in [1, 2, 3, 4, 5]:
article = random.choice(articles)
subject = random.choice(subjects)
verb = random.choice(verbs)
if random.randint(0, 1) == 0:
print(article, subject, verb)
else:
adverb = random.choice(adverbs)
print(article, subject, verb, adverb)
~
复制代码
请教,这个for语句里的下划线的作用
作者:
flying_away
时间:
2011-06-29 16:51
应该是一个变量吧.
作者:
meistudios
时间:
2011-06-30 03:21
一般用作你不关心其具体值的变量。
作者:
leooys
时间:
2011-06-30 09:24
谢谢,我最初奇怪的是,怎么在循环体里没有使用这个变量。明白了。
欢迎光临 Chinaunix (http://bbs.chinaunix.net/)
Powered by Discuz! X3.2