为什么有些参数需要定义,而另一些则不需要? (艰难地学习 Python,例如 25)
我正在努力学习《Learn Python the Hard Way ex.25》,但我就是无法集中注意力。这是脚本:
def break_words(stuff):
"""this function will break waords up for us."""
words = stuff.split(' ')
return words
def sort_words(words):
"""Sorts the words."""
return sorted(words)
def print_first_word(words):
"""Prints the first word after popping it off."""
word = words.pop(0)
print word
def print_last_word(words):
"""Prints the last word after popping it off."""
word = words.pop(-1)
print word
def sort_sentence(sentence):
"""Takes in a full sentence and returns the sorted words."""
words = break_words(sentence)
return sort_words(words)
def print_first_and_last(sentence):
"""Prints the first and last words of the sentence."""
words = break_words(sentence)
print_first_word(words)
print_last_word(words)
def print_first_and_last_sorted(sentence):
"""Sorts the words, then prints the first and last ones."""
words = sort_sentence(sentence)
print_first_word(words)
print_last_word(words)
运行脚本时,如果我使用命令break_words(**),break_words 将使用我创建的任何参数。所以我可以输入
sentence = "My balogna has a first name, it's O-S-C-A-R"
然后运行break_words(sentence)并最终得到解析的“'My''balogna''has'(...)。
但是其他函数(如sort_words)只会接受名为“words”的函数” 我必须输入 words = break_words(sentence)
或 sort_words 起作用的东西。
为什么我可以在break_words的括号中传递任何参数,但只能传递实际上归因于“句子”和“单词”的参数,专门用于sort_words、print_first_and_last等?我觉得这是我在继续阅读本书之前应该理解的基本内容,但我就是无法理解它。
Working my way through Learn Python the Hard Way ex.25, and I just can't wrap my head around something. Here's the script:
def break_words(stuff):
"""this function will break waords up for us."""
words = stuff.split(' ')
return words
def sort_words(words):
"""Sorts the words."""
return sorted(words)
def print_first_word(words):
"""Prints the first word after popping it off."""
word = words.pop(0)
print word
def print_last_word(words):
"""Prints the last word after popping it off."""
word = words.pop(-1)
print word
def sort_sentence(sentence):
"""Takes in a full sentence and returns the sorted words."""
words = break_words(sentence)
return sort_words(words)
def print_first_and_last(sentence):
"""Prints the first and last words of the sentence."""
words = break_words(sentence)
print_first_word(words)
print_last_word(words)
def print_first_and_last_sorted(sentence):
"""Sorts the words, then prints the first and last ones."""
words = sort_sentence(sentence)
print_first_word(words)
print_last_word(words)
When running the script, break_words will use any argument I create if I use the command break_words(**). So I can type
sentence = "My balogna has a first name, it's O-S-C-A-R"
and then run break_words(sentence) and end up with a parsed "'My' 'balogna' 'has' (...).
But other functions (like sort_words) will only accept a function with the name "words." I must type
words = break_words(sentence)
or something for sort_words to work.
Why can I pass any argument in the parentheses for break_words, but only arguments that are actually attributed to "sentence" and "words" specifically for sort_words, print_first_and_last, etc.? I feel like this is something fundamental that I should understand before I move on in the book, and I just can't get my head around it.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
它与每个函数接受作为其参数的值的类型有关。
break_words 返回一个列表。 sort_words 使用内置函数sorted(),它期望传递一个列表。这意味着您传递给 sort_words 的参数应该是一个列表。
也许下面的例子说明了这一点:
请注意,Python 默认是有帮助的,尽管这有时会令人困惑。因此,如果您将字符串传递给sorted(),它会将其视为字符列表。
It's about the type of value that each function accepts as its parameter.
break_words returns a list. sort_words uses the built-in function sorted(), which expects to be passed a list. This means that the parameter you pass to sort_words should be a list.
Maybe the following example illustrates this:
Note that python defaults to being helpful even though this can at times be confusing. So if you pass a string to sorted(), it will treat it as a list of characters.