1.內置函數補充 callable(object) 檢查對象object是否可調用 1、類是可以被調用的 2、實例是不可以被調用的,除非類中聲明瞭__call__方法 def f1(): print("test") f2 = "test" print(callable(f1)) print(call... ...
1.內置函數補充
callable(object)
檢查對象object是否可調用
1、類是可以被調用的
2、實例是不可以被調用的,除非類中聲明瞭__call__方法
def f1(): print("test") f2 = "test" print(callable(f1)) print(callable(f2))
True
False
chr(i)
返回整數i對應的ASCII字元
print(chr(81))
print(chr(81))
odr(c)
參數c是一個ascii字元,返回值是對應的十進位整
print (ord('b'))print (ord('b'))
隨機驗證碼
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語句來執...
compile(source, filename, mode[, flags[, dont_inherit]])
中文說明:將source編譯為代碼或者AST對象。代碼對象能夠通過exec語句來執行或者eval()進行求值。
參數source:字元串或者AST(Abstract Syntax Trees)對象。
參數 filename:代碼文件名稱,如果不是從文件讀取代碼則傳遞一些可辨認的值。
參數model:指定編譯代碼的種類。可以指定為 ‘exec’,’eval’,’single’。
參數flag和dont_inherit:這兩個參數暫不介紹,可選參數。
版本:在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)
執行python代碼,接收:代碼或者字元串
exec(print("test"))
eval(s)
執行表達式,並且獲取結果
eval(7*8)
dir(class)
快速查看,對象提供了哪些功能
dir(dict)
help(class)
獲取幫助
help(dict)
divmod(d,d)
共:97,每頁顯示10條,需要多少頁
n1 n2 = divmod(97, 10)
isinstance(instance,class)
用於判斷,對象是否是類的實例
s = "test" print(isinstance(s, str))
filter(函數, 可迭代對象)
迴圈第二個參數,讓每個迴圈元素執行函數,如果函數返回值為True,則元素是合法的。
li = [ 1, 2, 3, 4 ] ret = filter(None, li) print(list(ret))
lamda()
預設返回結果為返回值
map(函數,可迭代的對象(可以for迴圈的東西))
可迭代對象每個元素應用到函數中,返回結果為列表。
filter() #函數返回True,將元素添加到結果中
map() #將函數返回值添加到結果中
globals()
所有的全局變數
locals()
所有的局部變數
hash(s)
轉換成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雞湯
推薦書:《林達看美國》
json.loads(s)
將一個字元串,轉換成python的基本數據類型,字元串形式的字典必須是雙引號引用key value
3.裝飾器
裝飾器,我的理解是在不影響原函數的情況下(包括執行過程,返回結果,返回值等),增加其它功能(原函數前或後),即達到在不修改原代碼的前提下,實現增加的功能。
所以,其優點有:
1.代碼修改量小,不影響原函數內部邏輯等;
2.不修改原函數代碼,避免原功能因代碼語法性錯誤;
3.靈活性強,在需要增加功能的函數前加簡單代碼就行;
#def outer(func): # print(123,func) #def outer(func): # return "111" def outer(func): def inner(*args,**kwargs): #支持不定傳參 print("before") r = func(*args,**kwargs) #返回原函數返回值 return r #返回原函數返回值 print("after") return inner # @ + 函數名 # 功能: # 1. 自動執行outer函數並且將其下麵的函數名f1當作參數傳遞 # 2. 將outer函數的返回值,重新賦值給f1 @outerdef f1(): print("F1")