006 Python 高级特性

空函数生成函数

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

 

● 函数printspa

1 print(type(print))

  print的本质就是一个变量3d

  若是咱们直接给 print 赋值,函数就没法调用了code

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哦 orm

 

● 函数能够看成变量来使用对象

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 里面的每个元素都调用一次。blog

 

模块导入
form

  import functoolsxclass

    #导入模块内全部的函数import

  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")