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")
相關文章
相關標籤/搜索