Chinaunix

标题: python里的单个下划线 “_” 有何用处 [打印本页]

作者: leooys    时间: 2011-06-29 15:57
标题: python里的单个下划线 “_” 有何用处
  1. import random


  2. articles = ["the", "a", "another", "her", "his"]
  3. subjects = ["cat", "dog", "horse", "man", "woman", "boy", "girl"]
  4. verbs = ["sang", "ran", "jumped", "said", "fought", "swam", "saw",
  5.          "heard", "felt", "slept", "hopped", "hoped", "cried",
  6.          "laughed", "walked"]
  7. adverbs = ["loudly", "quietly", "quickly", "slowly", "well", "badly",
  8.            "rudely", "politely"]

  9. for _ in [1, 2, 3, 4, 5]:
  10.     article = random.choice(articles)
  11.     subject = random.choice(subjects)
  12.     verb = random.choice(verbs)
  13.     if random.randint(0, 1) == 0:
  14.         print(article, subject, verb)
  15.     else:
  16.         adverb = random.choice(adverbs)
  17.         print(article, subject, verb, adverb)
  18. ~                                                                  
复制代码
请教,这个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