摘要:1.內(nèi)置函數(shù)補(bǔ)充callable(object) 檢查對象object是否可調(diào)用 1、類是可以被調(diào)用的 2、實(shí)例是不可以被調(diào)用的,除非類中聲明了__call__方法def f1(): print("test") f2 = "test"print(cal
1.內(nèi)置函數(shù)補(bǔ)充
callable(object)
檢查對象object是否可調(diào)用
1、類是可以被調(diào)用的
2、實(shí)例是不可以被調(diào)用的,除非類中聲明了__call__方法
def f1(): print("test") f2 = "test"print(callable(f1))print(callable(f2))
True
False
chr(i)
返回整數(shù)i對應(yīng)的ASCII字符
print(chr(81))
print(chr(81))
odr(c)
參數(shù)c是一個ascii字符,返回值是對應(yīng)的十進(jìn)制整
print (ord('b'))
print (ord('b'))
隨機(jī)驗(yàn)證碼
import random li = []for i in range(5): temp = random.randrange(65,91) c = chr(temp) li.append(c) result = "".join(li)print(result)
compile(source, filename, mode[, flags[, dont_inherit]])
將source編譯為代碼或者AST對象。代碼對象能夠通過exec語句來執(zhí)...
compile(source, filename, mode[, flags[, dont_inherit]])
中文說明:將source編譯為代碼或者AST對象。代碼對象能夠通過exec語句來執(zhí)行或者eval()進(jìn)行求值。
參數(shù)source:字符串或者AST(Abstract Syntax Trees)對象。
參數(shù) filename:代碼文件名稱,如果不是從文件讀取代碼則傳遞一些可辨認(rèn)的值。
參數(shù)model:指定編譯代碼的種類??梢灾付?‘exec’,’eval’,’single’。
參數(shù)flag和dont_inherit:這兩個參數(shù)暫不介紹,可選參數(shù)。
版本:在python2.3、2.6、2.7、3.2中均有不同,使用時要引起注意,兼容python3
英文說明:
Compile the source into a code or AST object. Code objects can be executed by an exec statement or evaluated by a call to eval(). source can either be a string or an AST object. Refer to the ast module documentation for information on how to work with AST objects.
The filename argument should give the file from which the code was read; pass some recognizable value if it wasn’t read from a file ('' is commonly used).
The mode argument specifies what kind of code must be compiled; it can be 'exec' if source consists of a sequence of statements, 'eval' if it consists of a single expression, or 'single' if it consists of a single interactive statement (in the latter case, expression statements that evaluate to something other than None will be printed).
The optional arguments flags and dont_inherit control which future statements (see PEP 236) affect the compilation of source. If neither is present (or both are zero) the code is compiled with those future statements that are in effect in the code that is calling compile. If the flags argument is given and dont_inherit is not (or is zero) then the future statements specified by the flags argument are used in addition to those that would be used anyway. If dont_inherit is a non-zero integer then the flags argument is it – the future statements in effect around the call to compile are ignored.
Future statements are specified by bits which can be bitwise ORed together to specify multiple statements. The bitfield required to specify a given feature can be found as the compiler_flag attribute on the _Feature instance in the future module.
This function raises SyntaxError if the compiled source is invalid, and TypeError if the source contains null bytes.
Note When compiling a string with multi-line code in 'single' or 'eval' mode, input must be terminated by at least one newline character. This is to facilitate detection of incomplete and complete statements in the code module.
Changed in version 2.3: The flags and dont_inherit arguments were added.
Changed in version 2.6: Support for compiling AST objects.
Changed in version 2.7: Allowed use of Windows and Mac newlines. Also input in 'exec' mode does not have to end in a newline anymore.
exec(r)
執(zhí)行python代碼,接收:代碼或者字符串
exec(print("test"))
eval(s)
執(zhí)行表達(dá)式,并且獲取結(jié)果
eval(7*8)
dir(class)
快速查看,對象提供了哪些功能
dir(dict)
help(class)
獲取幫助
help(dict)
divmod(d,d)
共:97,每頁顯示10條,需要多少頁
n1 n2 = divmod(97, 10)
isinstance(instance,class)
用于判斷,對象是否是類的實(shí)例
s = "test"print(isinstance(s, str))
filter(函數(shù), 可迭代對象)
循環(huán)第二個參數(shù),讓每個循環(huán)元素執(zhí)行函數(shù),如果函數(shù)返回值為True,則元素是合法的。
li = [ 1, 2, 3, 4 ] ret = filter(None, li)print(list(ret))
lamda()
默認(rèn)返回結(jié)果為返回值
map(函數(shù),可迭代的對象(可以for循環(huán)的東西))
可迭代對象每個元素應(yīng)用到函數(shù)中,返回結(jié)果為列表。
filter() #函數(shù)返回True,將元素添加到結(jié)果中
map() #將函數(shù)返回值添加到結(jié)果中
globals()
所有的全局變量
locals()
所有的局部變量
hash(s)
轉(zhuǎn)換成hash值,一般用于字典的key
len(s)
返回一個對象的長度
s = "字符"print(len(s)) b = bytes(s, encoding = "utf-8")print(len(b))
max([list])
返回列表中的最大值
li = [11, 22, 33] r = max([li])
zip()
將列表的每列值組成一個無組
x = [1, 2, 3] y = [4, 5, 6] z = [7, 8, 9] xyz = zip(x, y, z)print xyz
[(1, 4, 7), (2, 5, 8), (3, 6, 9)]
2.Alex雞湯
推薦書:《林達(dá)看美國》
json.loads(s)
將一個字符串,轉(zhuǎn)換成python的基本數(shù)據(jù)類型,字符串形式的字典必須是雙引號引用key value
3.裝飾器
裝飾器,我的理解是在不影響原函數(shù)的情況下(包括執(zhí)行過程,返回結(jié)果,返回值等),增加其它功能(原函數(shù)前或后),即達(dá)到在不修改原代碼的前提下,實(shí)現(xiàn)增加的功能。
所以,其優(yōu)點(diǎn)有:
1.代碼修改量小,不影響原函數(shù)內(nèi)部邏輯等;
2.不修改原函數(shù)代碼,避免原功能因代碼語法性錯誤;
3.靈活性強(qiáng),在需要增加功能的函數(shù)前加簡單代碼就行;
#def outer(func):# print(123,func)#def outer(func):# return "111"def outer(func): def inner(*args,**kwargs): #支持不定傳參 print("before") r = func(*args,**kwargs) #返回原函數(shù)返回值 return r #返回原函數(shù)返回值 print("after") return inner# @ + 函數(shù)名# 功能:# 1. 自動執(zhí)行outer函數(shù)并且將其下面的函數(shù)名f1當(dāng)作參數(shù)傳遞# 2. 將outer函數(shù)的返回值,重新賦值給f1@outerdef f1(): print("F1")