学习PYTHON 入门日志 (六)

1. 2005-06-08 函数

继续:函数 
21:函数说明
       注意缩进,注意冒号分隔,参数不用类型说明
       函数会根据需要来调整参数的类型的。(我这样说
       对吗?)
       def <function_name>(<parameters_list>):
                 <code block>
       函数名称只是一个变量,可以赋给别的变量,象c中的
       函数指针,另外从这里开始我要体会一下什么是面向对象。
       据说Python中就把 函数当作对象来用的,所以才可以这么
       方便的赋值给别的变量。

22:参数的设置
       默认参数,和c++的很象
       >>> def myrange(start=0,stop,step=1):
       ... print stop,start,step
       ...
       这样的声明在 c++里是不可以的

       >>> def printValues(name,height=170,weight=70):
       ... print("%s's height is %d cm,"
       ... " his weigth is %d kg\n"%(name,height,weight))
       ...
       >>> printValues("Charles",weight=85)
       Charles's height is 170 cm, his weigth is 85 kg
       这样的调用也是在c++里面不可以的

       参数个数可变
       就是把后面的参数转换成tuple或 dictionary 来操作
       >>> def printf(format,*arg):
       ... print format%arg
       ...
       >>> printf ("%d is greater than %d",1,2)
       1 is greater than 2

       >>> def printf(format,**keyword):
       ... for k in keyword.keys():
       ... print "keyword[%s] is %s"%(k,keyword[k])
       ...
       >>> printf("ok",One=1,Two=2,Three=3)
       keyword[Three] is 3
       keyword[Two] is 2
       keyword[One] is 1

       注意**dictionary  和*tuple 必须是最后一个参数
       注意**对应dictionary ,*对应tuple。

       在这里就要体会python的灵活了,没有很苛刻的语法的限制。

23:Doc String函数描述
       可以输出函数的说明,但是注意只能输出一段,我更希望多输出几段

        >>> def testfun():
        ...   """
        ... this function do nothing, just demostrate the use of the
        ... doc string.
        ...   """
        ...   pass
        ...   """
        ... continue:haha
        ...   """
        ...
        >>> print testfun.__doc__
        this function do nothing, just demostrate the use of the
        doc string.
        >>>
         同样我们可以获得其它常用函数的__doc__ 如:
         >>> print [].count.__doc__
         L.count(value) -> integer -- return number of occurrences of value
         >>>
         我应该把我写的函数都做好__doc__的工作

24:lambda函数
       我不会LISP编程,我以前没见过lambda函数
       资料中讲可以认为是一个简单的匿名函数,
       我不知道该怎么理解。先死记硬背下来了。
       >>> f= lambda e,t : e*t
       >>> f(43,2)
       86
       >>>
       >>> f("sd",2)
       'sdsd'
       >>>

25:函数的作用域
       LGB准则就是Python找变量的次序,
       先        Local name space
       再        Global name space
       后        Builde in name space

       用global 语句可以修改作用域
       注意  一定是在local name space中使用这个变量之前
       >>> def fun():
       ...     a=4
       ...     print a
       ...     global a
       ...     print a
       ...     a = 5
       ...
       ...
       <stdin>:1: SyntaxWarning: name 'a' is assigned to before
global declaration
       >>>

26:嵌套函数
       Python中函数是可以嵌套的,就是函数里面可以定义函数如:
       >>> def fun(a,b):
       ...     def aa(x,y):
       ...         return x+y
       ...     return aa(a,b)
       ...
       >>> fun("pp","mm")
       'ppmm'
       >>>
       注意里层的函数是不能访问外层的变量的

27:参数传递
       python是按照 值传递 来传递参数的,但是也可以修改参数所指的值 如:

       >>> def change(x,y):
       ...     x=2
       ...     y[0]="new"
       ...
       >>> x=1
       >>> y=["old1","old2"]
       >>> change(x,y)
       >>> x
       1
       >>> y
       ['new', 'old2']
       >>>
       这个也好理解的。y[0]的作用范围不仅仅在change的函数里面的

邹胖小 2005年6月7日 祝大家快乐安康   编程要的就是苦练勤学