《Composing Programs》学习笔记(1.3)定义新函数(关键词:软件工程/函数)

本文深入探讨Python中的函数定义,包括如何定义用户自定义函数、环境作用域、函数调用、参数绑定和抽象。通过示例解析了函数如何作为代码复用的强大工具,强调了良好命名对函数可读性的重要性,并讨论了中缀运算符的使用和优先级规则。
摘要由CSDN通过智能技术生成

1.3 Defining New Functions

1.3 定义新函数

Video: Show Hide

视频:Show Hide

We have identified(识别;确定) in Python some of the elements that must appear in any powerful programming language:

我们已经确定,在Python中,一些元素必须出现在任何强有力的编程语言中:

1.Numbers and arithmetic(算术,计算) operations are primitive(原始的) built-in data values and functions.
2.Nested function application provides a means of combining operations.
3.Binding names to values provides a limited means of abstraction.

1.数和计算操作是原始的内建的数据值和函数。
2.嵌套函数应用程序提供了组合操作的一种方法。
3.将名字绑定到值,提供了抽象的一种限制的方法。

Now we will learn about function definitions, a much more powerful abstraction technique by which a name can be bound to compound operation, which can then be referred to as(be referred to as 被称为) a unit.

现在我们将会学到关于函数定义,一个更加强有力的抽象技术,通过一个可以被绑定到复合操作的名字,可以被称为一个单元。

We begin by examining how to express the idea of squaring. We might say, “To square something, multiply it by itself.” This is expressed in Python as

我们通过检测如何表达平方的想法,来开始。我们可能说,“要对sth.平方,将它自己相乘就行。”在Python中,这被这样表达

>>> def square(x):
        return mul(x, x)

which defines a new function that has been given the name square. This user-defined function is not built into the interpreter. It represents the compound operation of multiplying something by itself. The x in this definition is called a formal parameter(形式参数), which provides a name for the thing to be multiplied. The definition creates this user-defined function and associates it with the name square.

定义一个新的函数,该函数已经被给了名字square。这个用户定义的函数,不是在解释器中被建立。它代表了,将sth与它自身相乘的复合操作。这个定义中的这个x,被称为一个形式参数,形式参数提供了将会被相乘的这个东西的名字。这个定义创造了这个用户定义的函数,并且将它与名字square相关联。

How to define a function. Function definitions consist of a def statement that indicates(标示;指示;表明) a <name> and a comma-separated(逗号分开) list of named <formal parameters>, then a return statement, called the function body, that specifies(指定;详述) the <return expression> of the function, which is an expression to be evaluated whenever the function is applied:

如何定义一个函数。函数定义由一个def声明(这个声明标记了一个<名称>)和一个逗号隔开的列表(被命名为<形式参数>),然后一个return声明,被调用的函数主体,指定了这个函数的<return表达式>,这个表达式(无论何时函数被应用时)将被求值:

def <name>(<formal parameters>):
    return <return expression>

The second line must be indented(缩进) — most programmers use four spaces to indent. The return expression is not evaluated right away; it is stored as part of the newly defined function and evaluated only when the function is eventually(最后) applied.

第二行必须被锁进——大多数程序员使用4个空格去缩进。return表达式不会立即被求值;它被存储为新定义的函数的一部分,并且,只有当函数最后被应用时,被求值。

Having defined square, we can apply it with a call expression:

有了被定义的square,我们可以用一个调用表达式,来应用它:

>>> square(21)
441
>>> square(add(2, 5))
49
>>> square(square(3))
81

We can also use square as a building block in defining other functions. For example, we can easily define a function sum_squares that, given any two numbers as arguments, returns the sum of their squares:

我们也可以使用square,作为在定义其他函数中的一个构造块。例如,我们可以容易地定义一个函数sum_squares,被传入任意2个参数作为参数,返回它们的平方的和:

>>> def sum_squares(x, y):
        return add(square(x), square(y))
>>> sum_squares(3, 4)
25

User-defined functions are used in exactly(精确地;完全地) the same way as built-in functions. Indeed(确实;实际上), one cannot tell from the definition of sum_squares whether square is built into the interpreter, imported from a module, or defined by the user.

用户定义的函数,以完全相同的方式,被用在内建函数中。实际上,人们不能分清sum_squares的定义,square是否被内建在解释器中,从一个模块中被导入,或者被用户定义。

Both def statements and assignment statements bind names to values, and any existing bindings are lost. For example, g below first refers to a function of no arguments, then a number, and then a different function of two arguments.

def声明和赋值声明都把名字绑定到值上,任何现存的绑定丢失了。例如,下面的g首先引用一个无参函数,然后一个数,再然后一个有着2个参数的不同的函数。

>>> def g():
        return 1
>>> g()
1
>>> g = 2
>>> g
2
>>> def g(h, i):
        return h + i
>>> g(1, 2)
3
1.3.1 Environments
1.3.1 环境

Video: Show Hide

录像: Show Hide

Our subset of Python is now complex enough that the meaning of programs is non-obvious. What if(要是…又怎样) a formal parameter has the same name as a built-in function? Can two functions share names without confusion(无混淆)? To resolve such questions, we must describe environments in more detail.

我们的Python子集现在足够复杂,程序的意义不明显。要是一个形式参数,有着一个内建函数的相同的名字,会怎么样?2个函数是否可以无混淆地共享名字?为了解决这样的问题,我们必须描述环境的更多的细节。

An environment in which an expression is evaluated co

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值