python成长之路——第四天
生活随笔
收集整理的這篇文章主要介紹了
python成长之路——第四天
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
內置函數: callable:查看對象是否能被調用(對象是函數的話能被調用) #callable
def f1():
pass
f2="a"
print(callable(f1))
print(callable(f2))
#chr() 將ascii碼轉換成對應的字符
#ord() 相反
print(chr(65)) print(ord("B")) 生成一個6位的隨機驗證碼: #!/usr/bin/env python # -*- coding:utf-8 -*-import random li=[] for i in range(6):r=random.randrange(0,5) if r == 2:temp=random.randrange(0,10)li.append(str(temp)) #注意:join方法要求列表里的每個元素必須是字符串 elif r == 4:temp=random.randrange(97,123)k=chr(temp)li.append(k) else:temp=random.randrange(65,91)k=chr(temp)li.append(k) print("".join(li)) V67Dj2A compile() ?將字符串,編譯成python代碼 exec() ? 執行 ? 比eval功能更強大,直接執行python代碼或者字符串(如果接收的是字符串的話,內部會執行compile),沒有返回值 eval() ? ?執行 ? 主要是用來執行表達式的,有返回值 python解釋器執行文件要經過以下幾個步驟: 1.讀取文件內容到內存 ?open—str到內存open完成 2.python解釋器把 ?字符串—》編譯—》特殊代碼 ? compile完成 3.執行代碼exec完成 編譯模式: single:編譯成單行的python程序 eval:編譯成表達式 exec:編譯成和python代碼一模一樣的東西 with open("zy.py","r",encoding="utf8") as f: r = compile(f.read(),"<string>","exec") exec(r) BBDfji zy.py是上面的6位隨機驗證碼程序,這個就模擬了python解釋器的執行過程 s = "print(123)" r = compile(s,"<string>","exec") print(r) print(type(r)) exec(r) <code?object?<module>?at?0x0000000000802DB0,?file?"<string>",?line?1> <class?'code'> 123 s = ''' print(123) print(456) ''' r = compile(s,"<string>","single") exec(r) Traceback?(most?recent?call?last): File?"C:/Users/Administrator/PycharmProjects/untitled/day4.py",?line?14,?in?<module> r?=?compile(s,"<string>","single") File?"<string>",?line?2 print(123) ^ SyntaxError:?multiple?statements?found?while?compiling?a?single?statement s = ''' print(123) print(456) ''' r = compile(s,"<string>","exec") exec(r) 123 456 print(eval("2+3")) a="[1,2,3,4]" li=eval(a) print(type(li)) b='{"k1":"v1","k2":"v2"}' dic=eval(b) print(type(dic)) 5 <class 'list'> <class 'dict'> delattr,getattr,setattr,hasattr ?反射 ?以后講 dir() ?快速查看一個對象提供什么功能,不顯示功能詳細 help() ?顯示功能詳細 ?? divmod() ? #divmod() 得到商和余數,返回值是元組 #共97 每頁顯示10 總共多少頁 print(divmod(97,10)) n1, n2 = divmod(97,10) print("n1",n1) print("n2",n2) (9,?7) n1?9 n2?7 對象是類的實例 則True isinstance() ? #isinstance() 判斷對象是否是某個類的實例 s="alex" r=isinstance(s,list) print(r) False filter(函數,可迭代的對象) def f1(a): if a>22: return Trueli=[11,22,33,44,55] ret = filter(f1,li) print(ret) print(list(ret)) <filter?object?at?0x0000000001142198> [33,?44,?55] filter和lambda組合: li=[11,22,33,44,55] ret = filter(lambda a: a > 33,li) print(list(ret)) [44,?55] map(函數,可迭代對象),對可迭代對象的每個元素都進行處理 li=[11,22,33,44,55] def f(a): return a+100 ret = map(f,li) print(ret) print(list(ret)) <map?object?at?0x0000000000B82128> [111,?122,?133,?144,?155] li=[11,22,33,44,55] ret = map(lambda a: a+100 , li) print(ret) print(list(ret)) <map?object?at?0x00000000011C2128> [111,?122,?133,?144,?155] filter:函數返回True 則元素添加到結果中 map: 將函數返回值添加到結果中 NAME = "alex" def show():a=123 print(globals()) print(locals()) show() {'__package__':?None,?'__loader__':?<_frozen_importlib_external.SourceFileLoader?object?at?0x0000000000A05BA8>,?'__builtins__':?<module?'builtins'?(built-in)>,?'show':?<function?show?at?0x0000000001169C80>,?'__cached__':?None,?'__name__':?'__main__',?'__doc__':?None,?'__spec__':?None,?'__file__':?'C:/Users/Administrator/PycharmProjects/untitled/day4.py',?'NAME':?'alex'} {'a':?123} 如果一個字符串是字典或者列表等形式的,一定是單引號在外邊,雙引號在里面 裝飾器: @ + 函數名 功能: 1.自動執行outer函數并且將其下面的函數名f1當作參數傳遞 2.將outer函數的返回值,重復賦值給f1 詳細過程: 1. 將outer函數加載入內存 2. 執行@outer ?將函數名f1傳給func(func=原f1),執行outer 3. 將inner函數加載入內存 4. 將outer函數的返回值inner,重新賦值給f1,(f1=inner) 5. 當調用f1時,就執行inner(),inner中的func=原f1 適用場景: 主要是權限控制 def outer(func): def inner(*args,**kwargs): print("before")ret = func(*args,**kwargs) print("after") return ret return inner@outer def f1(aa): print("F1") return aa @outer def f2(*args,**kwargs): print("F2") for index,items in enumerate(args): print(index,"-"*5,items) for k in kwargs: print(k,"-"*5,kwargs[k]) f2(1,2,3,a=1,b=2,c=3) before F2 0?-----?1 1?-----?2 2?-----?3 b?-----?2 c?-----?3 a?-----?1 after?
轉載于:https://www.cnblogs.com/meitangyanyan/p/5132919.html
總結
以上是生活随笔為你收集整理的python成长之路——第四天的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 技术分享-bounds的深入认识
- 下一篇: SQL server与Oracle触发器