zoukankan      html  css  js  c++  java
  • python 内置函数 装饰器

    一、内置函数

    1.compile

    compile(source, filename, mode[, flags[, dont_inherit]])
    用来编译一段字符串的源码,结果可以生成字节码或者AST(抽像语法树),字节码可以使用函数exec()来执行,而AST可以使用eval()来继续编译。
    1. >>> str = "for i in range(0,10): print(i)"
    2. >>> c = compile(str,'','exec')
    3. >>> exec(c)
    4. 0
    5. 1
    6. 2
    7. 3
    8. 4
    9. 5
    10. 6
    11. 7
    12. 8
    13. 9
    14. >>> str2 = "3*3 + 4*4"
    15. >>> c2 = compile(str2, '', 'eval')
    16. >>> eval(c2)
    17. 25

    2.exec

    exec(object[, globals[, locals]])
    参数object是一个字符串的语句或者一个编译过的语句的对象名称,参数globals是全局命名空间,用来指定执行语句时可以访问的全局命名空间;参数locals是局部命名空间,用来指定执行语句时可以访问的局部作用域的命名空间。如果参数globals和locals忽略,就会使用调用时所处的命名空间。这两个参数都要求是字典形式来说明命名空间。

    exec没有返回值
    1. >>> exec('if True: print(100)')
    2. 100
    3. >>> exec('''
    4. x = 200
    5. if x > 100:
    6. print(x + 200)
    7. ''')
    8. 400

    3 eval

    eval(expression, globals=None, locals=None)

    该函数是用来动态地执行一个表达式的字符串,或者compile函数编译出来的代码对象。参数expression是一个表达式字符串,或者表示编译出来代码对象的名称;参数globals是全局命名空间,可以指定执行表达式时的全局作用域的范围,比如指定某些模块可以使用。如果本参数缺省,就使用当前调用这个函数的当前全局命名空间;参数locals是局部作用域命名空间,是用来指定执行表达式时访问的局部命名空间。

    1. >>> x = 1
    2. >>> print eval('x+1')
    3. 2

    4 divmod

    divmod(a, b)

    求余函数,返回它们相除后的的商和余数组成的元组。

    1. >>> divmod(10,3)
    2. (3, 1)
    3. >>>

    5 max

    max(iterable, *[, key, default])
    返回可迭代的对象中的最大的元素,或者返回2个或多个参数中的最大的参数,key 指定比较的键。

    1. >>> li = [11,11,33,11,44,44,55]
    2. >>> max(li, key=li.count)
    3. 11

    6 round

    round(number[, ndigits])

    返回浮点数 number 四舍五入到小数点之后 ndigits 位的结果。如果省略ndigits,该参数默认为零。

    1. >>> round(2.345)
    2. 2
    3. >>> round(2.345,2)
    4. 2.35

    7 isinstance


    isinstance(object, classinfo)
    如果参数object 是参数classinfo 的一个实例;或者是其一个(直接的、间接的或者virtual)子类的实例,返回真。

    1. >>> isinstance(li,list)
    2. True
    3. >>>


    8 locals

    提供基于字典访问局部变量的方式。
    每个函数都有着自已的名字空间,叫做局部名字空间,它记录了函数的变量,包括函数的参数
    和局部定义的变量。

    1. def foo(arg, a):
    2. x = 1
    3. y = 'xxxxxx'
    4. for i in range(10):
    5. j = 1
    6. k = i
    7. print locals()
    8. #调用函数的打印结果
    9. foo(1,2)
    10. #{'a': 2, 'i': 9, 'k': 9, 'j'终端: 1, 'arg': 1, 'y': 'xxxxxx', 'x': 1}

    9 map

    map(function,iterable)
    对于迭代对象的每个元素都执行第一个参数中的函数

    1. >>> def func(x):
    2. x = x + x
    3. return x
    4. >>> list(map(func,li))
    5. [2, 4, 6, 8, 10]
    6. >>>


    10 filter


    filter(function, iterable)
    对于迭对象中的没个元素都去执行以一个元素中的函数,如果结果为True,则返回

    1. >>> li
    2. [1, 2, 3, 4, 5]
    3. >>> list(filter(lambda x: x> 3, li))
    4. [4, 5]


    11 range 


    range(start, stop[, step])
    返回一个起于start,到stop,步长为step的序列,start默认是0,step默认是1。

    1. >>> list(range(10))
    2. [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
    3. >>> list(range(10,1,-2))
    4. [10, 8, 6, 4, 2]


    12 zip

    zip(*iterables)
    将多个序列中索引相同的项,组成元组,如果序列长度不同,则以长度短的为标准。

    1. >>> l1 = [1,2,3,4,5]
    2. >>> l2 = ['a','b','c','d']
    3. >>> zip(l1, l2)
    4. <zip object at 0x0000000003419948>
    5. >>> list(zip(l1, l2))
    6. [(1, 'a'), (2, 'b'), (3, 'c'), (4, 'd')]


    二、装饰器


    1.预备知识


    1. #### 第一波 ####
    2. def foo():
    3. print 'foo'
    4. foo #表示是函数
    5. foo() #表示执行foo函数
    6. #### 第二波 ####
    7. def foo():
    8. print 'foo'
    9. foo = lambda x: x + 1
    10. foo() # 执行下面的lambda表达式,而不再是原来的foo函数,因为函数 foo 被重新定义了

    2.装饰器开始


    记住装饰器最容易理解的两句话:
    1.自动执行outer函数并且将下面的函数名f1当作参数传递。
    2.将outer函数的返回值,重新赋值给f1。

    第一步 最简单的函数,准备附加额外功能


    1. >>> def outer():
    2. print("outer called.")
    3. >>> outer()
    4. outer called.

    第二步:使用装饰函数在函数执行前和执行后分别附加额外功能


    1. >>> def outer(func):
    2. print("before outer called.")
    3. func()
    4. print(" after outer called.")
    5. return func
    6. >>> def f1():
    7. print(" f1 called.")
    8. >>> f1 = outer(f1)
    9. before outer called.
    10. f1 called.
    11. after outer called.

    第三步:使用语法糖@来装饰函数


    1. >>> def outer(func):
    2. print("before outer called.")
    3. func()
    4. print(" after outer called.")
    5. return func
    6. >>> def f1():
    7. print(" f1 called.")
    8. >>> @outer
    9. def f1():
    10. print('f1 called')
    11. before outer called.
    12. f1 called
    13. after outer called.

    第四步:使用内嵌包装函数


    1. >>> def outer(func):
    2. def inner():
    3. print("before func called.")
    4. func()
    5. print(" after func called.")
    6. # 不需要返回func,实际上应返回原函数的返回值
    7. return inner
    8. >>>
    9. >>> @outer
    10. def f1():
    11. print('f1 called')
    12. >>> f1()
    13. before func called.
    14. f1 called
    15. after func called.

    第五步:对带参数的函数进行装饰


    1. >>>def outer(func):
    2. def inner(a, b):
    3. print("before func called.")
    4. ret = func(a, b)
    5. print(" after func called. result: %s" % ret)
    6. return ret
    7. return inner
    8. >>>@outer
    9. def f1(a, b):
    10. print(" f1(%s,%s) called." % (a, b))
    11. return a + b
    12. >>>f1()
    13. before func called.
    14. f1(a,b) called
    15. after func called.


     第六步:对参数数量不确定的函数进行装饰


    1. >>>def outer(func):
    2. def inner(*args, **kwargs):
    3. print("before func called.")
    4. ret = func(*args, **kwargs)
    5. print(" after func called. result: %s" % ret)
    6. return ret
    7. return inner
    8. >>>@outer
    9. def f1(a, b):
    10. print(" f1(%s,%s) called." % (a, b))
    11. return a + b
    12. >>>@outer
    13. def f2(a, b, c):
    14. print(" f2(%s,%s,%s) called." % (a, b, c))
    15. return a + b + c
    16. >>>f1()
    17. before func called.
    18. f1(a,b) called
    19. after func called.
    20. >>>f2()
    21. before func called.
    22. f1(a,b,c) called
    23. after func called.








  • 相关阅读:
    手动执行把nconf生成的配置文件附加到nagios下
    创建django工程
    mysql安装、配置、导入数据库
    PHP里<?php和<?通用要在配置里怎么设置_百度知道
    Loganalyzer数据库乱码解决方法:
    RSyslog安装配置
    修改cacti的系统时间
    Mysql5.7.11 安装 cacti0.8.8f ,在导入cacti.sql数据库时出现下记错误,导致数据库导入终止: ERROR 1067 (42000) at line 1847: Invalid default value for 'status_fail_date'
    中间件组件
    cookie与session 组件
  • 原文地址:https://www.cnblogs.com/9527chu/p/5557898.html
Copyright © 2011-2022 走看看