python函数

定义函数

  def func(x,y,z=v,*args,**kwargs):

    pass

  可以有多个返回值,通常封装为一个元组

  函数可以是对象,也可以做参数传递和返回

    一类对象:可以把对象当中参数和返回值来回传递

 1 # !/usr/bin/python
 2 # encoding:utf8
 3 
 4 
 5 
 6 def __handleTXT(txt):
 7     print txt
 8 def __handleJSON(json):
 9     print json
10 def __handleXML(xml):
11     print xml
12 
13 def decoder(type):
14     if type=='xml':
15         return __handleXML
16     elif type=='json':
17         return __handleJSON
18     else:
19         return __handleTXT()
20 
21 decoder('xml')('<p>This is a plain text without any type of delivery of this example is only to prove that Python function.</p> ')
View Code

 

参数没有类型,python不做类型检查

没有重载,python不会区分参数名字和个数

多个同名函数时,后一个会覆盖前一个

位置参数和关键字参数(函数调用时)

任意数量的参数:*,**

  def fun(*args,**kwargs)

 1 # !/usr/bin/python
 2 # encoding:utf8
 3 
 4 
 5 def add(a,b):
 6     return  a+b
 7 def add(a,b):
 8     return  a*b
 9 print add(5,6)
10 
11 def add(a,b,c):
12     return a+b*c
13 
14 print add(1,2,3)
15 print add(b=2,c=3,a=1)
16 print add(1,c=2,b=3)
17 
18 # 列表
19 x=[1,2,30]
20 print add(x[0],x[1],x[2])
21 # *、**做拆包、分解动作
22 print add(*x)
23 print add(1,2,30)
24 #字典
25 y={'a':10,'b':20,'c':30}
26 print add(y['a'],y['b'],y['c'])
27 # key必须和add方法的一一对应
28 print add(**y)
29 print add(a=10,b=20,c=30)
30 
31 def add(*args):
32     return  sum(args)
33 # args为参数名,*为关键字。传入的参数被处理为列表
34 print add(1,2,3,4,5,6,7,8,9,0,11,12)
35 
36 def add(**kwargs):
37     return sum(kwargs.values())
38 # kwargs为参数名,**为关键字。传入的参数被处理为字典
39 print add(a=1,b=2,c=3,d=4)
40 
41 def add(*args,**kwargs):
42     return sum(args)+sum(kwargs.values())
43 # 必须保持前后字段一致
44 print add(1,2,3,a=1,b=2,c=3)
View Code

 

 

参数传递的是‘引用’

函数内部的修改会影响到外部

函数可以有多个返回值

可以用一个tuple对象接收返回值

也可以用多个变量接收

1 def f(a):
2     a[2]='hh'
3 x=[1,2,3,4,5,6]
4 f(x)
5 print x
View Code

 

变量的作用域

LEGB原则

  L(local):函数本地

  E(enclose):任意上层的嵌套函数

  G(global):全局作用域(模块)

  B(build-in):内置作用域

1 x='global'
2 def outer():
3     # x='enclose'
4     def innter():
5         # x='local'
6         print x
7     innter()
8 outer()
9 print __name__
View Code

 

global

 1 x='global'
 2 def outer():
 3     # x='enclose'
 4     def innter():
 5         global x
 6         x +='e'
 7         # x='local'
 8         print x
 9     innter()
10 outer()
View Code

函数是一类对象

  函数可以有属性

  文档属性

    -  定义函数的第一个没有赋值的字符串,可以通过__doc__访问

 1 # !/usr/bin/python
 2 # encoding:utf8
 3 
 4 
 5 
 6 def f():
 7     pass
 8 f.text='a'
 9 f.test='b'
10 print f.text,f.test
11 
12 def f(a,b,c):
13     """
14     This is a showcase of __doc__ method
15     :param a: This is a common argument
16     :param b: This is a common argument
17     :param c: This is a common argument
18     :return:  This method return values according to the specific method to determine
19     """
20     return a**b**c
21 
22 print f.__doc__
23 print f(1,2,3)
View Code

 

 

函数嵌套和闭包

  python的函数可以嵌套定义

  闭包,能够保留函数定义时的环境信息

  嵌套的函数定义

  内部函数用到了外部函数中的变量

  外部函数返回内部函数

    def outer():

      def inner():

        ...

      return inner

  

1 def outer():
2     print 'outer'
3     def inner():
4         print 'inner'
5     # return inner()
6     return inner
7 x=outer()
8 x()
9 x=outer()()
View Code

 

装饰器

  不带参数的装饰器

    decorator(f)(*args,**kwargs)

    @decorator

    def f():

      pass

   带参数的装饰器

    decorator(name)(f)(*args,**kwargs)

    @decorator(name)

    def f():

      pass

 1 # !/usr/bin/python
 2 # encoding:utf8
 3 
 4 
 5 def decorator(f):
 6     def implenmention(*args,**kwargs):
 7         username=raw_input('name:')
 8         password=raw_input('pw:')
 9         if username=='huang' and password=='hjy123':
10             return f(*args,**kwargs)
11         else:
12             print "test error"
13     return implenmention
14 
15 def search(searchkw):
16     print  'search'
17 @decorator
18 def order(itemids):
19     print 'order',itemids
20 def pay(orderid):
21     print 'pay'
22 
23 order(121)
View Code

迭代器

  可迭代对象:Iterable

    如果一个对象可以用for...in...的方式实现遍历其内容,這个对象就是一个可迭代对象。

  迭代器:Iterable

    遍历可迭代对象内容的方式

  可迭代对象需要提供迭代器

    实现__iter__\__next__

itertools提供比较复杂的迭代方式

  permutations:排列

  combinations:组合

  product:笛卡尔积

  repeat:重复

  chain:链接一组迭代器

    import itertools

    itertools.permutations(X)

 

posted @ 2017-02-17 20:53  阅学习乐  阅读(212)  评论(0)    收藏  举报