这里的总菜鸟在“学习Python艰难之路”中的所有内容都让人感到困惑。如果已经涵盖,请道歉;我搜索过,只能找到关于没有从代码中获得所需结果的帖子。
我的问题涉及exercise 25中两个函数的交互:
def break_words(stuff):
words = stuff.split(' ')
return words
和
def sort_sentence(sentence):
words = break_words(sentence)
return sort_words(words)
所以,在练习结束时,Zed让你在终端中运行它:
>>> sorted_words = ex25.sort_sentence(sentence)
>>> sorted_words
['All', 'come', ’good’, ’things’, ’those’, ’to’, ’wait.’, ’who’]
现在我假设'sort_sentence'中的参数来自以下内容,在练习开始时输入终端:
>>> sentence = "All good things come to those who wait."
但是我们现在知道上面是'sort_sentence'的论据,'sort_sentence'无法在不运行'break_words'的情况下完成,'sentence'再次作为其参数。这就是我感到困惑的地方:'break_words'的参数被标记为'stuff'。这有关系吗?无论“break_words”的参数被标记为什么,“句子”只能从'sorted_words'传递到'break_words'吗?
假设我假设 - 参数标签无关紧要 - 'break_words'应该以'sentence'作为参数运行并返回'words',这是函数'stuff.split'所包含的输出在其中。这是我真的混淆的地方 - 'break_words'返回的'words'与定义为'sort_sentence'的一部分的变量'words'有什么关系?我根本无法弄清楚这些功能如何协同工作。提前感谢您的帮助!
答案 0 :(得分:1)
Python的功能如何或多或少地起作用如下:
def function_name(parameter_name_used_locally_within_function_name):
#do stuff with parameter_name_used_locally_within_function_name
some_new_value = parameter_name_used_locally_within_function_name
return some_new_value
注意参数如何仅在函数function_name
的范围内。因为该变量仅用于该函数而不是在其外部。当我们从函数返回变量时,我们可以将它分配给另一个调用函数的变量:
my_variable = function_name("hello")
my_variable
现在有"hello"
值,因为我们调用了函数,传入了值"hello"
。注意我没有使用指定变量名调用该函数?我们不关心参数名称是什么,我们所知道的是它需要一个函数输入。该参数名称仅用于该函数。请注意我们在调用函数时如何知道该变量的名称而得到some_new_value
的值?
让我举一个更广泛的例子说明发生了什么。功能可以被认为是您给某人做的任务。让我们说这个功能或任务就是让他们为我们做点什么。厨师或任务需要烹饪的成分(这是我们的意见),我们希望得到食物(我们的产出回报)。让我们说我想要一个煎蛋卷,我知道我必须让厨师鸡蛋让我成为一个,我不会在乎他是如何做到的或者他对它做了什么,只要我得到我的输出/煎蛋。他可以把鸡蛋叫做他想要的东西,他可以打破他想要的鸡蛋,他可以在锅里煎炸他喜欢的东西,但只要我拿到煎蛋卷,我就开心。
回到我们的编程世界,函数将类似于:
def cook_me_something(ingredients):
#I don't know how the chef makes things for us nor do I care
if ingredients == "eggs":
food = "omelette"
elif ingredients == "water":
food = "boiled water"
return food
我们称之为:
my_food_to_eat = cook_me_something("eggs")
注意我给了他"鸡蛋"我得到了一些"煎蛋"背部。我没有说鸡蛋是成分,也不知道他给我的食物是什么。他只返回包含food
omelettes
现在让我们谈谈将功能链接在一起。
所以我们得到了基本的关于我给厨师的东西,他根据我给他的东西给我食物。那么如果我们在烹饪之前给了他一些他需要处理的东西呢。如果他不知道如何研磨咖啡豆,那就说吧。但他的联合厨师工人也知道。他会将豆子传给那个人,将咖啡豆磨碎,然后用回流过程做饭。
def cook_me_something(ingredients):
#I don't know how the chef makes things for us nor do I care
if ingredients == "eggs":
food = "omelette"
elif ingredients == "water":
food = "boiled water"
elif ingredients == "coffee beans"
co_worker_finished_product = help_me_co_worker(ingredients)
#makes coffee with the co_worker_finished_product which would be coffee grindings
food = "coffee"
return food
#we have to define that function of the co worker helping:
help_me_co_worker(chef_passed_ingredients):
if chef_passed_ingredients == "coffee beans"
ingredients = "coffee grinding"
return ingredients
注意到co worker是如何拥有局部变量ingredients
的?它与厨师的不同之处在于,因为厨师有自己的食材,而同事也有自己的食材。请注意厨师如何不关心同事称他的成分或他如何处理这些物品。厨师向同事提供了一些东西并期待成品。
它或多或少是如何运作的。只要功能得到他们的输入,他们就会工作并可能提供输出。我们不关心他们在他们的职能中所谓的变量,因为他们自己的项目。
让我们回到你的榜样:
def break_words(stuff):
words = stuff.split(' ')
return words
def sort_sentence(sentence):
words = break_words(sentence)
return sort_words(words)
>>> sentence = "All good things come to those who wait."
>>> sorted_words = ex25.sort_sentence(sentence)
>>> sorted_words
['All', 'come', ’good’, ’things’, ’those’, ’to’, ’wait.’, ’who’]
让我们看看我们是否可以将其分解以供您理解。
您致电sorted_words = ex25.sort_sentence(sentence)
并将sorted_words
设置为sort_sentence()
函数['All', 'come', ’good’, ’things’, ’those’, ’to’, ’wait.’, ’who’]
的输出。您传入了输入sentence
sort_sentence(sentence)
被执行了。您传入的字符串现在在变量内部被称为sentence
。请注意,您可以像这样调用函数,它仍然可以工作:
sorted_words = ex25.sort_sentence("All good things come to those who wait.")
函数sort_sentence()
仍会调用该字符串sentence
。这个函数基本上说了我输入的内容,我称之为句子。您可以将您的对象命名为句子,我将在将其重命名为句子时使用它。
堆栈上的下一步是:
words = break_words(sentence)
现在调用函数break_words,函数sort_sentence
将其称为sentence
。因此,如果你按照跟踪它基本上做了:
words = break_words("All good things come to those who wait.")
堆栈上的下一步是:
words = stuff.split(' ')
return words
请注意,该函数将其输入为stuff
。因此sort_sentence的输入使得sort_sentence称为sentence
,而函数break_words
现在称之为stuff
。
分裂"句子"单词并将其存储在列表中并返回列表"单词"
注意函数sort_sentence
如何将break_words
的输出存储在变量words
中。注意函数break_words
如何返回名为words
的变量?在这种情况下它们是相同的,但如果一个人用不同的方式调用它并不重要。 sort_sentence
可以将输出存储为foo
,它仍然有效。我们正在谈论不同的变量范围。在函数break_words
之外,变量words
可以是任何内容,break_words
也不关心。但是在break_words
里面,变量是函数的输出。
在我家的规则下?在我家外面你可以做任何你想做的事情。
与sort_sentence
返回变量相同,以及我们如何存储从中获取的内容。我们如何存储它或我们称之为它并不重要。
如果您愿意,可以将其重命名为:
def break_words(stuff):
break_words_words = stuff.split(' ')
return break_words_words
def sort_sentence(sentence):
words = break_words(sentence)
return sort_words(words) #not sure where this function sort_words is coming from.
#return words would work normally.
>>> sentence = "All good things come to those who wait."
>>> sorted_words = ex25.sort_sentence(sentence)
>>> sorted_words
['All', 'come', ’good’, ’things’, ’those’, ’to’, ’wait.’, ’who’]
您只需要考虑局部变量和参数,就像命名要使用的东西一样。就像我们与厨师的例子一样,厨师可能称之为鸡蛋,配料,但我把它称之为我想要的东西,只是把它传给了鸡蛋"。这完全取决于事物的范围,将功能视为一个房子,当你在家里时,你可以在房子里命名你想要的物品,在房子外面,那些相同的名字可能是不同的东西。但在屋内,它们就是你想要的。当你抛出一些东西时,你将该项命名为与外界无关,因为外界会将其命名为其他东西。可以将它命名为同样的东西...
如果我只是絮絮叨叨,请提出问题,我会尽力为你解决。
被修改
从午餐回来后,我认为变量是容器,它们保留了价值,但你并不关心其他人的容器的名称。你只关心你的,当有人给你一些东西你把它放在一个容器中并命名它你关心的东西,这将帮助你知道它里面的东西。当你放弃一件物品时,你不会给容器,因为你需要它来储存其他物品..
答案 1 :(得分:0)
论证标签并不重要
从某种意义上来说它很重要"本地"在函数定义中。基本上将它视为您在函数定义中定义的另一个局部变量,但参数的值将赋予该函数。
牢记这一点,您的下一个问题很容易回答:
''是什么从' break_words'返回与...有关 变量'单词'定义为' sort_sentence'?
的一部分
无。如前所述,words
是sort_sentence
的局部变量,因此在离开函数时基本上会被删除("超出范围"是术语)。当然,您可以使用words
作为其他地方变量的名称,例如在另一个函数定义中,以及这里发生的事情。