高階函數(shù):
def f(n): return n*n def foo(a,b,func): func(a)+func(b) ret=func(a)+func(b) return ret foo(1,2,f) #f是函數(shù)foo的實(shí)參,func是函數(shù)foo的形參,因?yàn)閒為函數(shù)名,所以就是變量,將地址傳給了func print(foo(1,2,f)) #輸出的是函數(shù)的返回值52
+22
=5
注意函數(shù)加括號(hào)才是調(diào)用,不加括號(hào)的時(shí)候只是一個(gè)函數(shù)名。
函數(shù)本身是一個(gè)對(duì)象,函數(shù)名是變量
所以:1、函數(shù)名可以進(jìn)行賦值
2、函數(shù)名可以作為函數(shù)參數(shù),還可以作為函數(shù)的返回值
高階函數(shù)的條件:
1、函數(shù)名可以作為參數(shù)輸入
2、函數(shù)名可以作為返回值
遞歸函數(shù)
通過遞歸函數(shù)可實(shí)現(xiàn)階乘
def fact(n): if n==1: return 1 return n*fact(n-1) print(fact(5))
遞歸的特性:
1、調(diào)用自身函數(shù)
2、有一個(gè)結(jié)束條件
3、效率低
因?yàn)榇嬖谛实停抑灰梢允褂眠f歸可以解決的,用循環(huán)都可以,所以一般都用循環(huán)做。
重要的內(nèi)置函數(shù):
filter 過濾
str=["a","b","c","d"]
def fun1(s):
if s!="a":
return s #filter的作用就是將字符串封裝好,一個(gè)個(gè)送到fun1這個(gè)函數(shù)里面去
ret=filter(fun1,str) #過濾完的就是一個(gè)迭代器了 迭代器就像哆啦A夢(mèng)的口袋,用多少,拿多少,不占內(nèi)存
print(list(ret)) #轉(zhuǎn)成列表
>>>>>>["b","c","d"]
map 在元素后面添加字符串
str=["d","a","b"]
def fun2(s):
return s+"alvin"
ret=map(fun2,str) #map左邊括號(hào)左邊的是函數(shù)名
print(ret) #輸出的依舊是一個(gè)元組
print(list(ret))
>>>>>>["dalvin","aalvin","balvin"]
reduce
from functools import reduce
def add1(x,y):
return x+y
print(reduce(add1,range(1,10)))
>>>>>>45
因?yàn)閞educe函數(shù)調(diào)用完成的只是一個(gè)值,所以不要迭代器,而map和filter則是一個(gè)序列,所以需要迭代器。
lambda
from functools import reduce
print(reduce(lambda x,y:x*y,range(1,6))) #實(shí)現(xiàn)的是階乘,x+y也可以,所以代碼比較少和方便
>>>>>>120
裝飾器(函數(shù))
1、作用域:L-E-G-B
2、高階函數(shù)
3、閉包
def outer(): #1 x=10 #2 def inner(): #inner就是內(nèi)部函數(shù) #3 print(x) #外部環(huán)境的變量 #4 return inner #內(nèi)部函數(shù)inner就是一個(gè)閉包 #5 #inner() #局部變量,全局無法調(diào)用 f=outer() #6 f() #7 #代碼執(zhí)行的順序是1-->6-->2-->3-->5-->7-->4 最后輸出的是10 #也許大家都會(huì)有這么一個(gè)疑問,就是在第6步執(zhí)行完了之后就是到了return inner #之后再執(zhí)行第7步的時(shí)候,其實(shí)外層的函數(shù)在到return inner的時(shí)候已經(jīng)執(zhí)行完了 #第7步只是在執(zhí)行print(x),那疑問就是為什么還能輸出外層的x呢? # (外層的代碼在6235時(shí)執(zhí)行完了)。這種現(xiàn)象就叫閉包
所以閉包的定義:如果在一個(gè)內(nèi)部函數(shù)里,對(duì)外部作用域(但不是在全局作用域)的變量進(jìn)行引用,那么內(nèi)部函數(shù)就被認(rèn)為是閉包。
關(guān)于閉包:閉包=內(nèi)部函數(shù)+引用外部環(huán)境的變量
裝飾器:
import time def bar(): print("bar...") time.sleep(3) def foo(): print("foo...") time.sleep(2) def showtime(f):#在括號(hào)里面加入函數(shù)名,就是變量 start=time.time() f() end=time.time() print("spend:%s"%(end-start)) showtime(foo)
再看一個(gè)代碼:
import time def bar(): print("bar...") time.sleep(3) def foo(): print("foo...") time.sleep(2) def showtime(f):#在括號(hào)里面加入函數(shù)名,就是變量 def inner(): start=time.time() f() #閉包 end=time.time() print("spend:%s"%(end-start)) return inner foo=showtime(foo) #showtime(foo)拿到的是inner的內(nèi)存地址 foo() #執(zhí)行inner函數(shù) #其中foo=showtime(foo)等價(jià)于@showtime #代碼還可以寫成: import time def showtime(f):#在括號(hào)里面加入函數(shù)名,就是變量 def inner(): start=time.time() f() #閉包 end=time.time() print("spend:%s"%(end-start)) return inner @showtime def bar(): print("bar...") time.sleep(3) @showtime def foo(): print("foo...") time.sleep(2) foo()
對(duì)于不定長(zhǎng)的參數(shù):功能函數(shù)加參數(shù)
import time def showtime(f):#在括號(hào)里面加入函數(shù)名,就是變量 def inner(*x,**y): start=time.time() f(*x,**y) #閉包 end=time.time() print("spend:%s"%(end-start)) return inner @showtime #@showtime之后然后代碼往下找,是add函數(shù)需要showtime所以add函數(shù)的形參 #也要賦給inner函數(shù)同理f函數(shù)也要調(diào)用,因?yàn)榫褪且胊dd函數(shù)的。 def add(*x,**y): #@showtime就相當(dāng)于把變量定位到了inner sum=0 for i in x: sum+=i print(sum) @showtime def foo(): print("foo...") time.sleep(2)

圖的意思:foo()函數(shù)原本執(zhí)行的時(shí)候是粉色的框框,但是有@showtime是,就要執(zhí)行裝飾器里面的內(nèi)容
就是藍(lán)色的框框,其中粉色框的函數(shù)也包含在藍(lán)色的框里面了,所以圖中執(zhí)行的順序就是上述這個(gè)樣子。
考慮在裝飾器上加參數(shù):在套一層函數(shù)引入?yún)?shù)flag
import time def logger(flag): def showtime(f):#在括號(hào)里面加入函數(shù)名,就是變量 def inner(*x,**y): start=time.time() f(*x,**y) #閉包 end=time.time() print("spend:%s"%(end-start)) if flag=="true": print("日志記錄") return inner return showtime @logger("true") #這行代碼其實(shí)分為兩部分,@是一部分,logger("true")是一部分。先執(zhí)行的是logger(true) #@是看logger(true)返回回來的值,他返回回來的是showtime,所以原來的代碼就相當(dāng)于 #@showtime 其實(shí)加logger()這個(gè)函數(shù)目的就是為了引用flag這個(gè)參數(shù) def add(*x,**y): sum=0 for i in x: sum+=i print(sum) @logger("") def foo(): print("foo...") time.sleep(2) add(1,2,5,7)
裝飾器的登錄應(yīng)用
login_status=False def logger(flag): def login(f): def inner(): global login_status if login_status == False: nameuser = input("input your name:> ") print (nameuser) passwd = input ("input your password: > ") print (passwd) if flag == "jingdong": # 打開文件 with open("jingdong","r",encoding="utf8") as file_read: menu_dict=eval(file_read.read().strip()) for i in menu_dict: if nameuser ==i and passwd == menu_dict[i]: print ("welcome jingdong") login_status = True else: print("輸入有誤") break elif flag == "weixin": with open("weixin", "r", encoding="utf8") as file_read: menu_dict = eval(file_read.read().strip()) for i in menu_dict: if nameuser == i and passwd == menu_dict[i]: print("welcome jingdong") login_status = True f() return inner return login @logger("jingdong") def home(): print("welcome to homepage") @logger("weixin") def finance(): print("welcome to finance") @logger("jingdong") def book(): print("welcome to bookpage") with open("home_menu","r",encoding="utf8") as page_read: menu_dict=eval(page_read.read().strip()) menu_dict_super=[] good_list=[] while True: for i,v in enumerate(menu_dict,1): print(i,">>>",v) user_choice = input('請(qǐng)輸入選擇:返回上一級(jí)[q]').strip() if user_choice in menu_dict: menu_dict_super.append(menu_dict) if user_choice=="home": home() elif user_choice=="finance": finance() elif user_choice=="book": book() if type(menu_dict)==list: print("您選擇的商品是:",user_choice) else: menu_dict=menu_dict[user_choice] good_list.append(user_choice) elif user_choice=="q": if menu_dict_super: menu_dict=menu_dict_super.pop() else: print("請(qǐng)輸入正確的商品")
其中文檔,比如jingdong中的就是{“mengheng”:“456”}
浙公網(wǎng)安備 33010602011771號(hào)