CS入门学习笔记2-MIT 6.00.1x

Lecture 4 Functions

1. Function 的用处及语法
使用def作为定义的关键词

    def max(x, y):
       if x > y:
           return x
       else:
           return y

2. return
若函数执行完毕且无return作为输出标志,则会返回None, 如下面的代码将返回None:

    def f(x, y):
       '''
       x: int or float.
       y: int or float
       '''
       x + y - 2

3. .docstring
docstring可用于在function中解释此函数的含义及所用变量,单引号与双引号皆可。
docstring的解释

4. environment的概念与函数的关系
函数属于一个procedure object,即使在全局环境E1中已经定义过x,y的值,当调用函数max时,使用的x,y的值为局部环境E2中的。

在这里插入图片描述procedure(function)之所以可以被称作为black box,也正是因为会生成一个相对独立的environment,所有的computation都是建立在这个独立environment的基础上,所以local environment中的数值不会被改变。

- 关于function与environment的进一步延伸

在这里插入图片描述当使用z直接调用f(x)时,由于在函数中未对x的值进行定义,函数直接借用了全局环境中的x=3, 并放置于本函数的新环境中。此种模式叫做:static scoping,亦叫做 lexical scoping

在这里插入图片描述* 【知识补充】 关于static scoping 与dynamic scoping
在这里插入图片描述在这里插入图片描述
*【script写法】- 计算 x^n 如何用函数表达

def square(x):
    return x * x
def twoPower(x, n):
    while n > 1:
        x = square (x)
        n = n/2
    return x

5. 将函数单独存储为文件并在其他文件中引用
例如 circle.py
方法一:

import circle
print circle.pi
print circle.area(3)

方法二:

from circle import *
print area(3)

但需注意,如果引用文件中有同样名称的变量在本环境中已被定义,如在circle中pi=3.14,但本环境设定pi=3, 则 直接调用pi时,数值仍为3,只有当写明circle.pi时,才会是3.14

6. 小结
· 每一个函数在被调用时,都会生成一个独立的frame;
· 全局环境中的变量数值不会因为各个function被调用而被改变;
· 有两层函数嵌套时,两层函数中相同变量名的变量也不一定有相同的value;
· 以上内容可以帮助我们判断scoping of variables

7. 待解决疑问
a. 函数之间如何比较大小?
在未定义函数时,输入 a > b 返回error;
进行下述定义后: 再输入a > b 则返回true。这是如何进行的比较??

    def a(x, y, z):
    if x:
        return y
    else:
        return z

     def b(q, r):
     return a(q>r, q, r)

8. 课后题整理
(1). Write a Python function, clip(lo, x, hi) that returns lo if x is less than lo; hi if x is greater than hi; and x otherwise. For this problem, you can assume that lo < hi.

Don’t use any conditional statements for this problem. Instead, use the built in Python functions min and max. You may wish to read the documentation on min and the documentation on max, and play around with these functions a bit in your interpreter, before beginning this problem.

答案1(条件语句):

def clip(lo, x, hi):
    if min(lo, x, hi) == x :
        result = lo
    elif max (lo, x, hi) == x:
        result = hi
    else:
        result = x
    return result

答案二:

def clip(lo, x, hi):
   return min(max(lo, x), hi)

应该说,第一个答案是我根本没动脑子想,就直接顺着题目意思写完的,但其实分析一下便会发现,这是一道求三个数中中间数的题目,因此function中一行语句便可完成。
!!!切记,要先动脑子再动手!!!

def fourthPower(x):
    '''
    x: int or float.
    '''
    return square(x) * square(x)

【修订】:上述这种写法实在有点蠢,应改为 return square(square(x)) 会更加简洁

def odd(x):
    '''
    x: int or float.

    returns: True if x is odd, False otherwise
    '''
    judge = (x % 2 == 1)
    return judge

isVowel 不用 in

def isVowel(char):
    '''
    char: a single letter of any case

    returns: True if char is a vowel and False otherwise.
    '''
    return char == 'a' or char == 'e' or char == 'i' or char == 'o' or char == 'u' or char == 'A' or char == 'E' or char == 'I' or char == 'O' or char == 'U' 

isVowel 用 in

def isVowel2(char):
    '''
    char: a single letter of any case

    returns: True if char is a vowel and False otherwise.
    '''
    return char in 'aeiouAEIOU'

当str.upper未添加后面的括号时,返回的是一个function
python内置的处理string用语句:
https://docs.python.org/3/library/stdtypes.html#string-methods

猜你喜欢

转载自blog.csdn.net/qq_43198462/article/details/93479073