《Python基础教程(第二版)》学习笔记 -> 第六章 抽象

时间:2023-03-09 03:42:56
《Python基础教程(第二版)》学习笔记 -> 第六章 抽象

抽象和结构


本章将会介绍如何让将语句组织成函数,还会详细介绍参数(parameter)和作用域(scope)的概念,以及递归的概念及其在程序中的用途。

创建函数


函数可以调用,它执行某种行为,并返回某个值,可用内建的callable函数来判断函数是否可以调用

>>> import math
>>> x =
>>> y = math.sqrt
>>> callable(x)
False
>>> callable(y)
True#py 3.0中,callable不再可用,

创建一个函数:

>>> def fibs(num):
    result = [0,1]
    for i in range(num-2):
        result.append(result[-2]+result[-1])
    return result

>>> fibs(20)
[0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987, 1597, 2584, 4181]
>>> fibs(10)
[0, 1, 1, 2, 3, 5, 8, 13, 21, 34]
  1. 记录函数
    如果想要给函数写文档,让后面使用该函数的人能理解的话,可以加入注释,或者就是直接写上字符串(也叫做文档字符串),示例如下:
    >>> def square(x):
        'log or description'
        return x*x
    
    >>> square.__doc__
    'log or description'
    >>> square(3)
    9

参数魔法


  1. 值从哪里来
    保证函数在被提供给可接受参数的时候能正常工作就行。
    写在def 语句中函数名后面的变量通常叫做函数的形式参数,而调用函数的时候提供的值是实际参数,或者称为参数。
  2. 参数能改变吗
  3. 关键字参数和默认值
    目前为止我们所是用的参数都叫做:位置参数
    使用参数名提供的参数叫做:关键字参数,它的主要作用在于可以明确每个参数的作用
    >>> def hello_test(greeting='Hello',name='Python'):
        print '%s,%s!'%(greeting,name)
    
    >>> hello_test()
    Hello,Python!
    >>> hello_test('test')
    test,Python!
    >>> hello_test('python','powerful')
    python,powerful!
  4. 收集参数
     >>> def print_params(x,y,z=3,*pospar,**keypar):
         print x,y,z
         print pospar
         print keypar
    
     >>> print_params(1,2,3,7,8,9,foo=2,foo2='python')
     1 2 3
     (7, 8, 9)
     {'foo': 2, 'foo2': 'python'}
     >>> print_params(2,3)
     2 3 3
     ()
     {}

    *号的意思就是“收集其余的位置参数”,一个星号的参数存储在一个tuple中,两个星号的参数存储在一个dict中。

  5. 反转过程
    >>> def with_stars(**kwds):
        print kwds['name'],'is',kwds['age'],'years old'
    
    >>> def without_stars(**kwds):
        print kwds['name'],'is',kwds['age'],'years old'
    
    >>> args= {'name':'python','age':28}
    
    >>> with_stars(**args)
    python is 28 years old
    
    >>> without_stars(**args)
    python is 28 years old

    在with_stars中,定义和调用函数时都使用了星号,而在without_stars中,两处都没有使用,但得到了相同的效果。
    所以星号只在定义函数(允许使用不定数目的参数)或者调用(“分割”字典合作和序列)时才有用。

  6. 练习使用参数
    def story(**kwds):
        return 'Once upon a time, there was a %(job)s called %(name)s.'%kwds
    
    def power(x,y,*others):
        if others:
            print 'Received redundant parameters:',others
        return pow(x,y)
    
    def interval(start,stop=None,step=1):
        'Imitates range() for step >0'
        if stop is None:
            start,stop = 0,start
        result = []
        i =start
        while i<stop:
            result.append(i)
            i +=step
        return result

    运行结果如下:

    >>> print story(job='King',name='Gumby')
    Once upon a time, there was a King called Gumby.>>> print story(name='Sir Robin',job='brave knight')
    Once upon a time, there was a brave knight called Sir Robin.
    >>> params = {'job':'language','name':'Python'}
    >>> print story(**params)
    Once upon a time, there was a language called Python.
    >>> del params['job']
    >>> print story(job='stroke of genius',**params)
    Once upon a time, there was a stroke of genius called Python.

    >>> params = (5,)*2
      >>> power(*params)
      3125
      >>> power(3,3,'Hello, python')
      Received redundant parameters: ('Hello, python',)
      27
      >>> interval(10)
      [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
      >>> interval(1,5)
      [1, 2, 3, 4]
      >>> interval(3,12,4)
      [3, 7, 11]
      >>> power(*interval(3,7))
      #interval(3,7)返回了(3,4,5,6),然后单星号分裂成元组(3,4)和(5,6),所以执行了3的4次方,而(5,6)则是返回的Others
      Received redundant parameters: (5, 6)
      81

     

作用域


  1. 函数内的变量被称为局部变量(local variable)
    函数内部引用全局变量:
    >>> def combine(parameter):
        print parameter + globals()['parameter']
    
    >>> parameter = "berry"
    >>> combine('Shrub')
    Shrubberry
  2. 重新绑定全局变量
    >>> x = 1
    >>> def change_global():
        global x
        x = x+1
        return x
    
    >>> change_global()
    2
    >>> x
    2

递归


  1. 递归,简单来说就是引用自身的意思
  2. 有用的递归函数包含以下几部分:
    ①当函数直接返回值时有基本实例(最小可能性问题);
    ②递归实例,包括一个或者多个问题最小部分的递归调用
  3. 递归实例:阶乘和幂
    阶乘实例:
    >>> def factorial(n):
        if n == 1:
            return 1
        else:
            return n*factorial(n-1)
    
    >>> factorial(5)
    120

    求幂实例:

    >>> def power(x,n):
        if n == 0:
            return 1
        else:
            return x*power(x,n-1)
    
    >>> power(2,5)
    32