Python 学习笔记(十三)Python函数(二)

参数和变量

1 >>> def foo(a,b):  #函数是一个对象
2     return a+b
3 
4 >>> p =foo #对象赋值语句。将foo函数赋值给p这个变量
5 >>> foo(4,5)
6 9
7 >>> p(4,5) 变量p就指向了foo这个函数
8 9
9 >>> 

 按引用传递参数

  按照顺序进行赋值,函数中的变量a就指向了x,x是第一个实参,a这个参数指向了x所引用的对象,并不是把3这个数复制一个放到函数中,这种调用对象的方式,称之为按引用传递。

 1 >>> x,y=3,4 #x,y两个变量分别指向了3和4
 2 >>> foo(x,y) #函数中有两个参数a和b
 3 7
 4 >>> def bar(x):
 5     x =1
 8 >>> m = 9
 9 >>> bar(m)
10 >>> m
11 9
12 >>> 

  一个程序的所有的变量并不是在哪个位置都可以访问的。访问权限决定于这个变量是在哪里赋值的。

  变量的作用域决定了在哪一部分程序你可以访问哪个特定的变量名称。两种最基本的变量作用域如下:

  全局变量和局部变量

  定义在函数内部的变量拥有一个局部作用域,定义在函数外的拥有全局作用域。

  局部变量只能在其被声明的函数内部访问,而全局变量可以在整个程序范围内访问。调用函数时,所有在函数内声明的变量名称都将被加入到作用域中

 1 >>> x =2  #全局变量
 2 >>> def foo():
 3     x =9     #局部变量
 4     print "this x is the fun:",x
 5 
 6     
 7 >>> foo()
 8 this x is the fun: 9
 9 >>> x
10 2
  函数中的x 与函数外面的x,是不一样的。我们把像x=9这样的x,作用于某个函数体范围内的,称之为局部变量。 11 >>> def bar(): 12 global x #在函数体中声明全局变量 13 x =9 14 print "this x is the fun:",x 15 16 17 >>> x 18 2 19 >>> bar() 20 this x is the fun: 9 21 >>> x 22 9 23 >>>

命名空间:命名空间是对作用域的一种特殊抽象

参数收集和传值

收集方式1:*args  

  * args 是以元组的形式接收参数

 1 >>> def foo(*arg):
 2     print arg
 3 
 4     
 5 >>> foo(1,2,3)
 6 (1, 2, 3)     #元组形式接收参数
 7 >>> foo("baidu","ali","qq","weixin")
 8 ('baidu', 'ali', 'qq', 'weixin')
 9 >>> foo("web",[1,2,3,"pythom"])
10 ('web', [1, 2, 3, 'pythom'])
11 >>> def foo(x,*arg):
12     print "x:",x
13     print "arg:",arg
14 
15     
16 >>> foo(1,2,3)
17 x: 1
18 arg: (2, 3)
19 >>> foo(7)
20 x: 7
21 arg: ()
22 >>> 

收集方式2:**kargs 是以字典形式接收参数

 1 >>> def foo(**karg):
 2     print karg
 3 
 4     
 5 >>> foo(a=1,b=2,c=3)
 6 {'a': 1, 'c': 3, 'b': 2}
 7 >>> def foo(x,*arg,**karg):
 8     print x
 9     print arg
10     print karg
11 
12     
13 >>> foo(1)
14 1
15 ()
16 {}
17 >>> foo(1,2)
18 1
19 (2,)
20 {}
21 >>> foo(1,2,3)
22 1
23 (2, 3)
24 {}
25 >>> foo(1,2,3,name="python")
26 1
27 (2, 3)
28 {'name': 'python'}
29 >>> 
>>> def book(author,name):
    print "{0} has a book :{1}".format(author,name)

    
>>> bars={"name":"learn python with cc","author":"cc"}
>>> book(**bars)
cc has a book :learn python with cc
>>> 

特殊函数

zip() 补充

 1 >>> colors =["red","green","blue"]
 2 >>> values=[234,12,89,65]
 3 >>> zip(colors,values)
 4 [('red', 234), ('green', 12), ('blue', 89)]
 5 >>> dots=[(1,2),(3,4),(5,6)]
 6 >>> x,y=zip(*dots)
 7 >>> x
 8 (1, 3, 5)
 9 >>> y
10 (2, 4, 6)
11 >>> seq =range(1,10)
12 >>> zip(*[iter(seq)]*3)
13 [(1, 2, 3), (4, 5, 6), (7, 8, 9)]
14 >>> x =iter(range(1,10))
15 >>> x
16 <listiterator object at 0x0000000003E8F860>
17 >>> list(x)
18 [1, 2, 3, 4, 5, 6, 7, 8, 9]
19 >>> zip(x,x,x)
20 []
21 >>> x=iter(range(1,10))
22 >>> zip(x,x,x)
23 [(1, 2, 3), (4, 5, 6), (7, 8, 9)]
24 >>> 

lambda  lambda x: x+y lambda 变量: 表达式

map、reduce、filter

 1 >>> def foo(x):
 2     x+-3
 3     return x
 4 
 5 >>> foo(4)
 6 4
 7 >>> n =range(10)
 8 >>> n
 9 [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
10 >>> [i+3 for i in n ]
11 [3, 4, 5, 6, 7, 8, 9, 10, 11, 12]
12 >>> lam =lambda x:x+3
13 >>> n2=[]
14 >>> for i in n
15 SyntaxError: invalid syntax
16 >>> for i in n:
17     n2.append(lam(i))
18 
19     
20 >>> n2
21 [3, 4, 5, 6, 7, 8, 9, 10, 11, 12]
22 >>> g =lambda x,y:x+y
23 >>> g(3,4)
24 7
25 >>> lambda x:x+4
26 <function <lambda> at 0x0000000003E91438>
27 >>> n
28 [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
29 >>> map(foo,n)
30 [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
31 >>> map(lambda x:x+3,n)
32 [3, 4, 5, 6, 7, 8, 9, 10, 11, 12]
33 >>> lst1 =[1,2,3,4,5]
34 >>> lst2=[6,7,8,9,0]
35 >>> map(lambda x,y:x+y,lst1,lst2)
36 [7, 9, 11, 13, 5]
37 >>> reduce(lambda x,y:x+y,lst1)
38 15
39 >>> n =range(-5,5)
40 >>> n
41 [-5, -4, -3, -2, -1, 0, 1, 2, 3, 4]
42 >>> filter(lambda x:x>0,n)
43 [1, 2, 3, 4]
44 >>> [x for x in n if x>0]
45 [1, 2, 3, 4]
46 >>> 
原文地址:https://www.cnblogs.com/wangruihua-521/p/8566895.html