006 Python 高级特性

空函数生成

1 # -*- coding: UTF-8 -*-
2 def myFoo():
3     poss #加上poss 就可以生成空函数了

● 函数print

1 print(type(print))

  print的本质就是一个变量

  如果我们直接给 print 赋值,函数就无法调用了

1 # -*- coding: UTF-8 -*-
2 num = 10
3 other = num    #other  int 类型 num 此时 other 是一个整型
4 print(other)
5 other = print    #other 输出类型的 函数 print 是一个函数
6 other(66666)    #此时 other 拥有了 print的功能

  感觉好牛X哦 

● 函数可以当作变量来使用

1 # -*- coding: UTF-8 -*-
2 def foo(x,f):
3     f(x)
4 foo(10,print)

1 # -*- coding: UTF-8 -*-
2 temp = map(print,[1,2,3,4,5,6,7])
3 print(type(temp))
4 print(temp)        #迭代对象
5 print(list(temp))    #可以转换成list

1 # -*- coding: UTF-8 -*-
2 def foo(x):
3     return x**2        #计算每一个数求立方
4     
5 print(list(map(foo,list(range(10)))))

  map 里面的每一个元素都调用一次。

模块导入

  import functoolsx

    #导入模块内所有的函数

  form functools import reduce

    #从模块内导入 一个函数

返回函数 延迟调用

1 # -*- coding: UTF-8 -*-
2 def sum(*args):
3     n_sum = 0
4     for num in args:
5         n_sum += num
6     return n_sum
7 
8 num = sum(1,2,3,4,5)
9 print(num)

 函数绑定

1 # -*- coding: UTF-8 -*-
2 functools.partial(int,base=2)
3 int2 = functools.partial(int, base=2)
4 int2("1010101001001010101")
原文地址:https://www.cnblogs.com/sdk123/p/7218654.html