Python学习笔记(十五)- 函数基础(Function Basics)

1.编写函数有什么意义?
答:函数是避免 Python 中代码冗余的最基本方法 - 将代码转换为函数意味着我们将来只有一个操作代码的副本可以更新(可以不用在程序各个部分修改相同的内容)。 函数也是 Python 中代码重用的基本单元 - 函数中的代码使其成为可重用的工具,可在各种程序中调用。 最后,函数允许我们将复杂系统划分为可管理的零件,每个零件都可以单独开发。

2. Python什么时候创建一个函数?
答:当 Python 到达并运行 def 语句时,会创建一个函数; 此语句创建一个函数对象并为其指定函数的名称。 这通常发生在封闭模块文件(enclosing module file)由另一个模块导入时(回想一下导入操作从上到下运行文件中的代码,包括任何 def 语句),但是当 def 以交互方式键入或嵌套在其他语句中时,也会发生这种情况, 比如 使用 if 语句嵌套 def 语句的时候。

3.如果函数中没有 return 语句,函数会返回什么?
答:如果控制流(control flow)脱离函数体的末尾而没有运行到 return 语句(某个函数运行到最后一句,退出时),则函数默认返回 None 对象。 这些函数通常使用表达式语句调用,因为将 None 结果赋给变量通常是没有意义的。 没有表达式的 return 语句也会返回 None(直接写个 return 即可)。

4.嵌套在函数定义语句中的代码何时运行?
答:函数体(嵌套在函数定义语句中的代码)在稍后使用调用表达式调用函数时运行。 每次调用函数时,函数体都会重新运行。

5.检查传递给函数的对象类型会导致什么问题?
答:检查传递给函数的对象类型会有效地破坏函数的灵活性,从而限制函数仅对特定类型起作用。 如果没有这样的对象类型检查,该函数可能能够处理整个范围的对象类型 - 任何支持该函数所期望的接口的对象都将起作用。 (这个术语接口(interface)表示函数代码运行的方法集【the set of methods】和表达式运算符集【the set of expression operators】。)

标注:转载《Learning Python 5th Edition》[奥莱理]
1. What is the point of coding functions?
2. At what time does Python create a function?
3. What does a function return if it has no return statement in it?
4. When does the code nested inside the function definition statement run?
5. What's wrong with checking the types of objects passed into a function?

1. Functions are the most basic way of avoiding code redundancy in Python—factoring code into functions means that we have only one copy of an operation's code to update in the future. Functions are also the basic unit of code reuse in Python—wrapping code in functions makes it a reusable tool, callable in a variety of programs. Finally, functions allow us to divide a complex system into manageable parts, each of which may be developed individually.
2. A function is created when Python reaches and runs the def statement; this statement creates a function object and assigns it the function's name. This normally happens when the enclosing module file is imported by another module (recall that imports run the code in a file from top to bottom, including any defs), but it can also occur when a def is typed interactively or nested in other statements, such as ifs.
3. A function returns the None object by default if the control flow falls off the end of the function body without running into a return statement. Such functions are usually called with expression statements, as assigning their None results to variables is generally pointless. A return statement with no expression in it also returns None.
4. The function body (the code nested inside the function definition statement) is run when the function is later called with a call expression. The body runs anew each time the function is called.
5. Checking the types of objects passed into a function effectively breaks the function's flexibility, constraining the function to work on specific types only. Without such checks, the function would likely be able to process an entire range of object types—any objects that support the interface expected by the function will work. (The term interface means the set of methods and expression operators the function's code runs.)

猜你喜欢

转载自blog.csdn.net/Enderman_xiaohei/article/details/87866713
今日推荐