callable(object)
檢查對象object是否可調用
一、類是能夠被調用的
二、實例是不能夠被調用的,除非類中聲明瞭__call__方法python
def f1(): print("test") f2 = "test" print(callable(f1)) print(callable(f2))
Trueexpress
Falsejson
chr(i)
返回整數i對應的ASCII字符app
print(chr(81))
Qdom
odr(c)
參數c是一個ascii字符,返回值是對應的十進制整ide
print (ord('b'))
98
函數
隨機驗證碼ui
import random li = [] for i in range(5): temp = random.randrange(65,91) c = chr(temp) li.append(c) result = "".join(li) print(result)
EIJTLlua
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:這兩個參數暫不介紹,可選參數。spa
版本:在python2.三、2.六、2.七、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")')
test
eval(s)
執行表達式,而且獲取結果
print(eval("7*8"))
56
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))
True
filter(函數, 可迭代對象)
循環第二個參數,讓每一個循環元素執行函數,若是函數返回值爲True,則元素是合法的。
li = [ 1, 2, 3, 4 ] ret = filter(None, li) print(list(ret))
[1, 2, 3, 4]
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))
2
6
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
(python3有變化)
[(1, 4, 7), (2, 5, 8), (3, 6, 9)]
推薦書:《林達看美國》
json.loads(s)
將一個字符串,轉換成python的基本數據類型,字符串形式的字典必須是雙引號引用key value
裝飾器,個人理解是在不影響原函數的狀況下(包括執行過程,返回結果,返回值等),增長其它功能(原函數前或後),即達到在不修改原代碼的前提下,實現增長的功能。
因此,其優勢有:
1.代碼修改量小,不影響原函數內部邏輯等;
2.不修改原函數代碼,避免原功能因代碼語法性錯誤;
3.靈活性強,在須要增長功能的函數前加簡單代碼就行;
#def outer(func):# print(123,func)#def outer(func):# return "111"def outer(func): def inner(*args,**kwargs): print("before") func(*args,**kwargs) print("after") return inner # @ + 函數名# 功能:# 1. 自動執行outer函數而且將其下面的函數名f1看成參數傳遞# 2. 將outer函數的返回值,從新賦值給f1 @outerdef f1(): print("F1")