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")
请注意,我给了他“鸡蛋”,我得到了一些“煎蛋”。我没有说鸡蛋是配料,也不知道他给我的食物叫什么。他只是返回包含omelettes的food
现在让我们一起讨论链接函数。
所以我们得到了关于我给厨师一些东西的基本信息,他根据我给他的东西给我食物。那么如果我们给他一些他需要在烹饪之前处理的东西呢?假设他不知道如何研磨咖啡豆怎么办。但他的副厨师长也知道怎么做。他会把咖啡豆递给那个人,把咖啡豆磨碎,然后用返回过程做饭。
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
注意到同事如何有一个局部变量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。该函数基本上说明了我的输入是什么,我称之为句子。您可以将您的对象命名为 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 的输入被称为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’]
您只需将局部变量和参数视为只是命名要使用的事物。就像我们与厨师的例子一样,厨师可能会称鸡蛋为原料,但我将其命名为我想要的任何名称,然后将其传递给“鸡蛋”。一切都与事物的范围有关,将功能视为房屋,当您在房屋中时,您可以为房屋中想要的任何物品命名,而在房屋之外,这些相同的名称可能是不同的东西,但在房屋内部,它们就是您希望它们成为的样子。而当你把东西扔出去的时候,你给那个东西起名字与外界无关,因为外界会给它起别的名字。可能会用同样的名字命名它......
如果我说得太多,请提出问题,我会尽力为您解决问题。
编辑
午饭回来,我认为变量是容器,它们保存值,但你不在乎其他人的容器叫什么名字。你只关心你的,当有人给你东西时,你把它放在一个容器里,并给它命名你关心的东西,这将帮助你知道里面有什么。当你送出一个物品时,你不给容器,因为你需要它来存储其他东西..