Python: Functions 功能
Getting Help 寻求帮助
You saw the abs function in the previous tutorial, but what if you’ve forgotten what it does?
你在前面的教程中看到了 abs 函数,但如果你忘记了它的作用怎么办?
The help() function is possibly the most important Python function you can learn. If you can remember how to use help(), you hold the key to understanding most other function.
help()函数可能是你可以学习的最重要的 Python 函数。如果你能记住如何使用 help(),你就掌握了理解大多数其他函数的关键。
Here is an example:
下面是一个例子。
1 | help(round) |
1 | Help on built-in function round in module builtins: |
help()
displays two things:
- the header of that function
round(number[, ndigits])
. In this case, this tells us thatround()
takes an argument we can describe asnumber
. Additionally, we can optionally give a separate argument which could be described asndigits
.
函数 round(number[, ndigits])的头。在这种情况下,这告诉我们 round()取一个参数,我们可以用 number 来描述。此外,我们可以选择给出一个单独的参数,可以描述为 ndigits。 - A brief English description of what the function does.
简单的英文描述一下这个函数的作用。
Defining Functions
Builtin functions are great, but we can only get so far with them before we need to start defining our own functions. Below is a simple example.
内置函数是很好的,但在我们需要开始定义自己的函数之前,我们只能在这些函数上走得很远。下面是一个简单的例子.Builtin 函数很好,但在我们需要开始定义自己的函数之前,我们只能通过它们走得很远。下面是一个简单的例子。
1 | def least_difference(a, b, c): |
Docstring
1 | def least_difference(a, b, c): |
The docstring is a triple-quoted string (which may span multiple lines) that comes immediately after the header of a function. When we call help() on a function, it shows the docstring.
docstring 是一个三引号的字符串(可以跨越多行),紧接在函数的头后面。当我们在一个函数上调用 help() 时,它会显示 docstring。
1 | help(least_difference) |
1 | Help on function least_difference in module __main__: |
Functions that don’t return 不返回的函数
What would happen if we didn’t include the return keyword in our function?
如果我们的函数中不包含返回关键字,会有什么后果呢?
1 | def least_difference(a, b, c): |
None None None
Python allows us to define such functions. The result of calling them is the special value None
. (This is similar to the concept of “null” in other languages.)
Python 允许我们定义这样的函数。调用它们的结果是特殊值 None
。(这类似于其他语言中的 “null “的概念)。
Functions Applied to Functions
Here’s something that’s powerful, though it can feel very abstract at first. You can supply functions as arguments to other functions. Some example may make this clearer:
这里有一个很强大的东西,虽然一开始会觉得很抽象。你可以将函数作为参数提供给其他函数。一些例子可能会让你更清楚。
1 | def mult_by_five(x): |
5
25
Exercise
1.
Complete the body of the following function according to its docstring.
HINT: Python has a built-in function round
.
1 | def round_to_two_places(num): |
2.
The help for round
says that ndigits
(the second argument) may be negative.
What do you think will happen when it is? Try some examples in the following cell?
Can you think of a case where this would be useful?
1 | # Put your test code here |
3.
In a previous programming problem, the candy-sharing friends Alice, Bob and Carol tried to split candies evenly. For the sake of their friendship, any candies left over would be smashed. For example, if they collectively bring home 91 candies, they’ll take 30 each and smash 1.
Below is a simple function that will calculate the number of candies to smash for any number of total candies.
Modify it so that it optionally takes a second argument representing the number of friends the candies are being split between. If no second argument is provided, it should assume 3 friends, as before.
Update the docstring to reflect this new behaviour.
1 | def to_smash(total_candies): |