自學Python之路-Python基礎+模塊+面向對象
自學Python之路-Python網絡編程
自學Python之路-Python併發編程+數據庫+前端
自學Python之路-djangohtml
內置參數詳解官方文檔: https://docs.python.org/3/library/functions.html?highlight=built#ascii 前端
abs(x) | 求絕對值 一、參數能夠是整型,也能夠是複數 二、若參數是負數,則返回負數的模 |
complex([real[, imag]]) | 建立一個複數 |
divmod(a, b) | 分別取商和餘數 注意:整型、浮點型均可以 |
float([x]) | 將一個字符串或數轉換爲浮點數。若是無參數將返回0.0 |
int([x[, base]]) | 將一個字符轉換爲int類型,base表示進制 |
long([x[, base]]) | 將一個字符轉換爲long類型 |
pow(x, y[, z]) | 返回x的y次冪 |
range([start], stop[, step]) | 產生一個序列,默認從0開始 |
round(x[, n]) | 四捨五入 |
sum(iterable[, start]) | 對集合求和 |
oct(x) | 將一個數字轉化爲8進制 |
hex(x) | 將整數x轉換爲16進制字符串 |
chr(i) | 返回整數i對應的ASCII字符 |
bin(x) | 將整數x轉換爲二進制字符串 |
bool([x]) | 將x轉換爲Boolean類型 |
basestring() | str和unicode的超,不能直接調用,能夠用做isinstance判斷 |
format(value [, format_spec]) | 格式化輸出字符串 格式化的參數順序從0開始,如「I am {0},I like {1}」 |
unichr(i) | 返回給定int類型的unicode |
enumerate(sequence [, start = 0]) | 返回一個可枚舉的對象,該對象的next()方法將返回一個tuple |
iter(o[, sentinel]) | 生成一個對象的迭代器,第二個參數表示分隔符 |
max(iterable[, args...][key]) | 返回集合中的最大值 |
min(iterable[, args...][key]) | 返回集合中的最小值 |
dict([arg]) | 建立數據字典 |
list([iterable]) | 將一個集合類轉換爲另一個集合類 |
set() | set對象實例化 |
frozenset([iterable]) | 產生一個不可變的set |
str([object]) | 轉換爲string類型 |
sorted(iterable[, cmp[, key[, reverse]]]) | 隊集合排序 |
tuple([iterable]) | 生成一個tuple類型 |
xrange([start], stop[, step]) | xrange()函數與range()相似,但xrnage()並不建立列表,而是返回一個xrange對象,它的行爲與列表類似,可是隻在須要時才計算列表值,當列表很大時,這個特性能爲咱們節省內存 |
all(iterable) | 一、集合中的元素都爲真的時候爲真 二、特別的,若爲空串返回爲True |
any(iterable) | 一、集合中的元素有一個爲真的時候爲真 二、特別的,若爲空串返回爲False |
cmp(x, y) | 若是x < y ,返回負數;x == y, 返回0;x > y,返回正數 |
callable(object) | 檢查對象object是否可調用 一、類是能夠被調用的 二、實例是不能夠被調用的,除非類中聲明瞭__call__方法 |
classmethod() | 一、註解,用來講明這個方式是個類方法 二、類方法便可被類調用,也能夠被實例調用 三、類方法相似於Java中的static方法 四、類方法中不須要有self參數 |
compile(source, filename, mode[, flags[, dont_inherit]]) | 將source編譯爲代碼或者AST對象。代碼對象可以經過exec語句來執行或者eval()進行求值。 一、參數source:字符串或者AST(Abstract Syntax Trees)對象。 二、參數 filename:代碼文件名稱,若是不是從文件讀取代碼則傳遞一些可辨認的值。 三、參數model:指定編譯代碼的種類。能夠指定爲 ‘exec’,’eval’,’single’。 四、參數flag和dont_inherit:這兩個參數暫不介紹 |
dir([object]) | 一、不帶參數時,返回當前範圍內的變量、方法和定義的類型列表; 二、帶參數時,返回參數的屬性、方法列表。 三、若是參數包含方法__dir__(),該方法將被調用。當參數爲實例時。 四、若是參數不包含__dir__(),該方法將最大限度地收集參數信息 |
delattr(object, name) | 刪除object對象名爲name的屬性 |
eval(expression [, globals [, locals]]) | 計算表達式expression的值 |
execfile(filename [, globals [, locals]]) | 用法相似exec(),不一樣的是execfile的參數filename爲文件名,而exec的參數爲字符串。 |
filter(function, iterable) | 構造一個序列,等價於[ item for item in iterable if function(item)] 一、參數function:返回值爲True或False的函數,能夠爲None 二、參數iterable:序列或可迭代對象 |
getattr(object, name [, defalut]) | 獲取一個類的屬性 |
globals() | 返回一個描述當前全局符號表的字典 |
hasattr(object, name) | 判斷對象object是否包含名爲name的特性 |
hash(object) | 若是對象object爲哈希表類型,返回對象object的哈希值 |
id(object) | 返回對象的惟一標識 |
isinstance(object, classinfo) | 判斷object是不是class的實例 |
issubclass(class, classinfo) | 判斷是不是子類 |
len(s) | 返回集合長度 |
locals() | 返回當前的變量列表 |
map(function, iterable, ...) | 遍歷每一個元素,執行function操做 |
memoryview(obj) | 返回一個內存鏡像類型的對象 |
next(iterator[, default]) | 相似於iterator.next() |
object() | 基類 |
property([fget[, fset[, fdel[, doc]]]]) | 屬性訪問的包裝類,設置後能夠經過c.x=value等來訪問setter和getter |
reduce(function, iterable[, initializer]) | 合併操做,從第一個開始是前兩個參數,而後是前兩個的結果與第三個合併進行處理,以此類推 |
reload(module) | 從新加載模塊 |
setattr(object, name, value) | 設置屬性值 |
repr(object) | 將一個對象變幻爲可打印的格式 |
slice() | |
staticmethod | 聲明靜態方法,是個註解 |
super(type[, object-or-type]) | 引用父類 |
type(object) | 返回該object的類型 |
vars([object]) | 返回對象的變量,若無參數與dict()方法相似 |
bytearray([source [, encoding [, errors]]]) | 返回一個byte數組 一、若是source爲整數,則返回一個長度爲source的初始化數組; 二、若是source爲字符串,則按照指定的encoding將字符串轉換爲字節序列; 三、若是source爲可迭代類型,則元素必須爲[0 ,255]中的整數; 四、若是source爲與buffer接口一致的對象,則此對象也能夠被用於初始化bytearray. |
zip([iterable, ...]) |
file(filename [, mode [, bufsize]]) | file類型的構造函數,做用爲打開一個文件,若是文件不存在且mode爲寫或追加時,文件將被建立。添加‘b’到mode參數中,將對文件以二進制形式操做。添加‘+’到mode參數中,將容許對文件同時進行讀寫操做 一、參數filename:文件名稱。 二、參數mode:'r'(讀)、'w'(寫)、'a'(追加)。 三、參數bufsize:若是爲0表示不進行緩衝,若是爲1表示進行行緩衝,若是是一個大於1的數表示緩衝區的大小 。 |
input([prompt]) | 獲取用戶輸入 推薦使用raw_input,由於該函數將不會捕獲用戶的錯誤輸入 |
open(name[, mode[, buffering]]) | 打開文件 與file有什麼不一樣?推薦使用open |
打印函數 | |
raw_input([prompt]) | 設置輸入,輸入都是做爲字符串處理 |
內置函數,通常都是由於使用頻率比較頻繁或是是元操做,因此經過內置函數的形式提供出來,經過對python的內置函數分類分析能夠看出來:基本的數據操做基本都是一些數學運算(固然除了加減乘除)、邏輯操做、集合操做、基本IO操做,而後就是對於語言自身的反射操做,還有就是字符串操做,也是比較經常使用的,尤爲須要注意的是反射操做。python
2.1 abs 絕對值
數據庫
print(abs(-1)) print(abs(1))
輸出
1
1 express
2.2 all 全部的都爲真,他才爲真
django
print(all([1,2,'1'])) print(all([1,2,'1',''])) print(all('')) #若是可迭代對象是空,就返回True
執行結果:
編程
True
False
Truepython3.x
2.3 any 集合中的元素有一個爲真的時候爲真, 若爲空串返回爲False
數組
print(any([0,''])) print(any([0,'',1]))
輸出網絡
False
True
2.4 bin 把十進制轉成二進制
print(bin(3))
輸出 0b11
2.5 bool 布爾值 空,None, 0的布爾值爲False,其他都爲True
print(bool('')) #空字符串,返回值None print(bool(None)) print(bool(0))
執行結果:
False
False
False
2.6 bytes 把字符串轉成字節
name='你好,carlos' print(bytes(name,encoding='utf-8')) #手動把字符串編碼,轉成二進制 print(bytes(name,encoding='utf-8').decode('utf-8')) #須要把字符串進行編碼,再解碼(用什麼編碼,就用什麼解碼)
輸出
b'\xe4\xbd\xa0\xe5\xa5\xbd,carlos'
你好,carlos
name='你好,carlos' print(bytes(name,encoding='gbk')) #手動把字符串編碼,轉成二進制 print(bytes(name,encoding='gbk').decode('gbk')) #須要把字符串進行編碼,再解碼(用什麼編碼,就用什麼解碼)
輸出:
b'\xc4\xe3\xba\xc3,carlos'
你好,carlos
name='你好' print(bytes(name,encoding='ascii')) #ascii不能編碼中文,會報錯
2.7 ascll 碼對應的編碼
print(chr(89)) # ascll 碼對應的編碼
輸出
Y
2.8 dir 顯示函數內置屬性和方法
print(dir(dict)) #打印內置屬性和方法
輸出
['__class__', '__contains__', '__delattr__', '__delitem__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__iter__', '__le__', '__len__', '__lt__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__setitem__', '__sizeof__', '__str__', '__subclasshook__', 'clear', 'copy', 'fromkeys', 'get', 'items', 'keys', 'pop', 'popitem', 'setdefault', 'update', 'values']
2.9 divmod 取商得餘數,用於作分頁顯示功能
print(divmod(258, 8)) # 取商得餘數,用於作分頁顯示
輸出(32, 2)
2.10 eval 把字符串中的數據結構給提取出來
dic={'name':'alex'} dic_str=str(dic) #字典類型轉成字符串 print(dic_str) d1=eval(dic_str) #eval:把字符串中的數據結構給提取出來 print(d1)
輸出
{'name': 'alex'}
{'name': 'alex'}
2.11 可hash的數據類型即不可變數據類型,不可hash的數據類型便可變數據類型
#hash的做用:去網上下載軟件,判斷是否被人修改,經過比對hash值,就知道 print(hash('12sdfdsaf3123123sdfasdfasdfasdfasdfasdfasdfasdfasfasfdasdf')) print(hash('12sdfdsaf31231asdfasdfsadfsadfasdfasdf23')) name='carlos' print(hash(name)) print(hash(name)) print('--->before',hash(name)) name='gary' print('--->after',hash(name))
輸出
733648477
895994456
-794251862
-794251862
--->before -794251862
--->after 245391844
2.12 help 查看函數用法的說細信息
print(help(all))
2.13 bin、hex、oct 進制轉換
print(bin(20)) #10進制->2進制 print(hex(6)) #10進制->16進制 print(oct(78)) #10進制->8進制
輸出
0b10100
0x6
0o116
2.14 isinstance判斷類型,返回True或Flase
print(isinstance(667,int)) #判斷是否是int類型 print(isinstance('carlos',str)) #判斷字符串 print(isinstance([],list)) #判斷列表 print(isinstance({},dict)) #判斷字典 print(isinstance({5,2},set)) #判斷集合
輸出
True
True
True
True
True
2.15 globals 全局變量
name='fagaggagsregha' print(globals()) #全局變量
輸出
{'__name__': '__main__', '__doc__': None, '__package__': None, '__loader__': <_frozen_importlib_external.SourceFileLoader object at 0x00296570>, '__spec__': None, '__annotations__': {}, '__builtins__': <module 'builtins' (built-in)>, '__file__': 'C:/Users/Administrator/PycharmProjects/untitled/aaaa.py', '__cached__': None, 'name': 'fagaggagsregha'}
name='fagaggagsregha' print(__file__) #直接打印文件名
輸出
C:/Users/Administrator/PycharmProjects/untitled/aaaa.py
def test(): age='1111111111111111111111111111111111111111111111111111111111111' print(globals()) #打印全局變量 print(locals()) #打印上一層的變量 test()
globals 打印全局變量,locals 打印上一層的變量
輸出
{'__name__': '__main__', '__doc__': None, '__package__': None, '__loader__': <_frozen_importlib_external.SourceFileLoader object at 0x002B6570>, '__spec__': None, '__annotations__': {}, '__builtins__': <module 'builtins' (built-in)>, '__file__': 'C:/Users/Administrator/PycharmProjects/untitled/aaaa.py', '__cached__': None, 'test': <function test at 0x005B5300>}
{'age': '1111111111111111111111111111111111111111111111111111111111111'}
2.16 max 最大值 和 min最小值
l=[555,23,-100,-1,45] print(max(l)) print(min(l))
輸出
555
-100
max 高級用法
說明:
一、max函數處理的是可迭代對象,至關於一個for循環取出每一個元素進行比較。
注意:不一樣類型之間不能進行比較
二、每一個元素間進行比較,是從每一個元素的第一位置依次比較,若是這一個位置分出大小,後面的都不須要比較了,直接得出這倆元素的大小。
age_dic={'alex_age':18,'wupei_age':20,'zsc_age':100,'lhf_age':30} print(max(age_dic.values())) #取出最大年齡 print(max(age_dic)) #默認比較的是字典的key
輸出
100
zsc_age
取出年齡最大的key和values
age_dic={'alex_age':18,'wupei_age':20,'zsc_age':100,'lhf_age':30} for item in zip(age_dic.values(),age_dic.keys()): #[(18,'alex_age') (20,'wupeiqi_age') () () ()] print(item)#取出年齡最大的key和values print('=======>',list(max(zip(age_dic.values(),age_dic.keys())))) #max和zip聯合使用
輸出
(18, 'alex_age')
(20, 'wupei_age')
(100, 'zsc_age')
(30, 'lhf_age')
=======> [100, 'zsc_age']
l=[ (5,'e'), (1,'b'), (3,'a'), (4,'d'), ] l1=['a10','b12','c10',100] #不一樣類型之間不能進行比較 l1=['a10','a2','a10'] #不一樣類型之間不能進行比較 print(list(max(l))) print('--->',list(max(l1)))
輸出
[5, 'e']
---> ['a', '2']
2.17 zip 將對象逐一配對
print(list(zip(('a','n','c'),(1,2,3)))) print(list(zip(('a','n','c'),(1,2,3,4)))) print(list(zip(('a','n','c','d'),(1,2,3))))
輸出
[('a', 1), ('n', 2), ('c', 3)]
[('a', 1), ('n', 2), ('c', 3)]
[('a', 1), ('n', 2), ('c', 3)]
p={'name':'alex','age':18,'gender':'none'} print(list(zip(p.keys(),p.values()))) print(list(p.keys())) #取keys print(list(p.values())) #values print(list(zip(['a','b'],'12345'))) #列表,只要是序列就能夠打印出來
輸出
[('name', 'alex'), ('age', 18), ('gender', 'none')]
['name', 'age', 'gender']
['alex', 18, 'none']
[('a', '1'), ('b', '2')]
max總結:
l=[1,3,100,-1,2] print(max(l)) #比較出最大值 dic={'age1':18,'age2':10} print(max(dic)) #比較的是key print(max(dic.values())) #比較的是key,可是不知道是那個key對應的值 print(max(zip(dic.values(),dic.keys()))) #結合zip使用
輸出
100
age2
18
(18, 'age1')
people=[ {'name':'alex','age':1000}, {'name':'carlos','age':10000}, {'name':'gary','age':9000}, {'name':'amy','age':18},] # max(people,key=lambda dic:dic['age']) print('agaggdgafadg',max(people,key=lambda dic:dic['age'])) #提取年齡中的values,再進行比較 #上面題分解步驟,先取出ret的值,再給max進行比較 people=[ {'name':'alex','age':1000}, {'name':'carlos','age':10000}, {'name':'gary','age':9000}, {'name':'amy','age':18},] ret=[] for item in people: ret.append(item['age']) print(ret) max(ret)
輸出
agaggdgafadg {'name': 'carlos', 'age': 10000}
[1000, 10000, 9000, 18]
chr : 返回一個字符串,其ASCII碼是一個整型.好比chr(97)返回字符串'a'。參數i的範圍在0- 255之間。
ord: 參數是一個ascii字符,返回值是對應的十進制整數
pow: 幾的幾回方
print(chr(97)) #ascll碼應對的編碼 print(ord('a')) #ascll碼應對的數字 print(pow(3,3)) #3**3 幾的幾回方,至關於3的3次方 print(pow(3,3,2)) #3**3%2 3的3次方,取餘
輸出
a
97
27
1
2.18 reversed 反轉
l=[1,2,3,4] print(list(reversed(l))) print(l)
輸出
[4, 3, 2, 1]
[1, 2, 3, 4]
2.19 round 四捨五入
print(round(3.5)) # 四捨五入
輸出 4
2.20 set 集合
print(set('hello')) # 集合
輸出 {'l', 'o', 'h', 'e'}
2.21 slice 切片
l='hello' s1=slice(3,5) #切片 取3到5的元素 s2=slice(1,4,2) #切片,指定步長爲2 print(l[3:5]) print(l[s1]) #切片 print(l[s2]) print(s2.start) #開始 print(s2.stop) #結束 print(s2.step) #步長
輸出
lo
lo
el
1
4
2
2.22 sorted 排序
l=[3,2,1,5,7] l1=[3,2,'a',1,5,7] print(sorted(l)) #排序 # print(sorted(l1)) #直接運行會報錯,由於排序本質就是在比較大小,不一樣類型之間不能夠比較大小
輸出[1, 2, 3, 5, 7]
people=[ {'name':'alex','age':1000}, {'name':'wupei','age':10000}, {'name':'yuanhao','age':9000}, {'name':'linhaifeng','age':18},] print(sorted(people,key=lambda dic:dic['age'])) #按年齡進行排序
輸出
[{'name': 'linhaifeng', 'age': 18}, {'name': 'alex', 'age': 1000}, {'name': 'yuanhao', 'age': 9000}, {'name': 'wupei', 'age': 10000}]
name_dic={ 'abyuanhao': 11900, 'alex':1200, 'wupei':300, } print(sorted(name_dic)) #按key排序 print(sorted(name_dic,key=lambda key:name_dic[key])) #取出字典的values print(sorted(zip(name_dic.values(),name_dic.keys()))) #按價格從低到高排序
輸出
['abyuanhao', 'alex', 'wupei']
['wupei', 'alex', 'abyuanhao']
[(300, 'wupei'), (1200, 'alex'), (11900, 'abyuanhao')]
2.23 str , type
str 轉換成字符型
type 查看某一個東西的數據類型
eval 轉換數據類型
print(str('1')) #str 轉換成字符型 print(type(str({'a':1}))) #type 查看數據類型 dic_str=str({'a':1}) print(type(eval(dic_str))) #eval 轉換數據類型
輸出
1
<class 'str'>
<class 'dict'>
msg='123' if type(msg) is str: msg=int(msg) res=msg+1 # int類型+1 print(res)
輸出 124
2.24 vars 跟一個列表或多個字典
def test(): msg='我就是一個天才' print(locals()) #打印出上一層的值,若是上一層沒有,再往上找 print(vars()) #若是沒有參數,跟locals同樣,若是有參數,查看某一個方法,顯示成字典的方式 test() print(vars(int))
輸出
{'msg': '我就是一個天才'}
{'msg': '我就是一個天才'}
{'__repr__': <slot wrapper '__repr__' of 'int' objects>, '__hash__': <slot wrapper '__hash__' of 'int' objects>, '__str__': <slot wrapper '__str__' of 'int' objects>, '__getattribute__': <slot wrapper '__getattribute__' of 'int' objects>, '__lt__': <slot wrapper '__lt__' of 'int' objects>, '__le__': <slot wrapper '__le__' of 'int' objects>, '__eq__': <slot wrapper '__eq__' of 'int' objects>, '__ne__': <slot wrapper '__ne__' of 'int' objects>, '__gt__': <slot wrapper '__gt__' of 'int' objects>, '__ge__': <slot wrapper '__ge__' of 'int' objects>, '__add__': <slot wrapper '__add__' of 'int' objects>, '__radd__': <slot wrapper '__radd__' of 'int' objects>, '__sub__': <slot wrapper '__sub__' of 'int' objects>, '__rsub__': <slot wrapper '__rsub__' of 'int' objects>, '__mul__': <slot wrapper '__mul__' of 'int' objects>, '__rmul__': <slot wrapper '__rmul__' of 'int' objects>, '__mod__': <slot wrapper '__mod__' of 'int' objects>, '__rmod__': <slot wrapper '__rmod__' of 'int' objects>, '__divmod__': <slot wrapper '__divmod__' of 'int' objects>, '__rdivmod__': <slot wrapper '__rdivmod__' of 'int' objects>, '__pow__': <slot wrapper '__pow__' of 'int' objects>, '__rpow__': <slot wrapper '__rpow__' of 'int' objects>, '__neg__': <slot wrapper '__neg__' of 'int' objects>, '__pos__': <slot wrapper '__pos__' of 'int' objects>, '__abs__': <slot wrapper '__abs__' of 'int' objects>, '__bool__': <slot wrapper '__bool__' of 'int' objects>, '__invert__': <slot wrapper '__invert__' of 'int' objects>, '__lshift__': <slot wrapper '__lshift__' of 'int' objects>, '__rlshift__': <slot wrapper '__rlshift__' of 'int' objects>, '__rshift__': <slot wrapper '__rshift__' of 'int' objects>, '__rrshift__': <slot wrapper '__rrshift__' of 'int' objects>, '__and__': <slot wrapper '__and__' of 'int' objects>, '__rand__': <slot wrapper '__rand__' of 'int' objects>, '__xor__': <slot wrapper '__xor__' of 'int' objects>, '__rxor__': <slot wrapper '__rxor__' of 'int' objects>, '__or__': <slot wrapper '__or__' of 'int' objects>, '__ror__': <slot wrapper '__ror__' of 'int' objects>, '__int__': <slot wrapper '__int__' of 'int' objects>, '__float__': <slot wrapper '__float__' of 'int' objects>, '__floordiv__': <slot wrapper '__floordiv__' of 'int' objects>, '__rfloordiv__': <slot wrapper '__rfloordiv__' of 'int' objects>, '__truediv__': <slot wrapper '__truediv__' of 'int' objects>, '__rtruediv__': <slot wrapper '__rtruediv__' of 'int' objects>, '__index__': <slot wrapper '__index__' of 'int' objects>, '__new__': <built-in method __new__ of type object at 0x5EAA7370>, 'conjugate': <method 'conjugate' of 'int' objects>, 'bit_length': <method 'bit_length' of 'int' objects>, 'to_bytes': <method 'to_bytes' of 'int' objects>, 'from_bytes': <method 'from_bytes' of 'int' objects>, '__trunc__': <method '__trunc__' of 'int' objects>, '__floor__': <method '__floor__' of 'int' objects>, '__ceil__': <method '__ceil__' of 'int' objects>, '__round__': <method '__round__' of 'int' objects>, '__getnewargs__': <method '__getnewargs__' of 'int' objects>, '__format__': <method '__format__' of 'int' objects>, '__sizeof__': <method '__sizeof__' of 'int' objects>, 'real': <attribute 'real' of 'int' objects>, 'imag': <attribute 'imag' of 'int' objects>, 'numerator': <attribute 'numerator' of 'int' objects>, 'denominator': <attribute 'denominator' of 'int' objects>, '__doc__': "int(x=0) -> integer\nint(x, base=10) -> integer\n\nConvert a number or string to an integer, or return 0 if no arguments\nare given. If x is a number, return x.__int__(). For floating point\nnumbers, this truncates towards zero.\n\nIf x is not a number or if base is given, then x must be a string,\nbytes, or bytearray instance representing an integer literal in the\ngiven base. The literal can be preceded by '+' or '-' and be surrounded\nby whitespace. The base defaults to 10. Valid bases are 0 and 2-36.\nBase 0 means to interpret the base from the string as an integer literal.\n>>> int('0b100', base=0)\n4"}
2.25 import 模塊
①先建立一個文件
②定義函數
③調用模塊
def say_hi(): print('hello carlos') import aaaa aaaa.say_hi()
輸出
hello carlos
hello carlos
2.26 __import__ :導入一個字符串類型模塊,就要用__import__
①先建立一個文件
②定義函數
③調用模塊
def say_hi(): print('hello carlos') module_name='aaaa' m=__import__(module_name) #有字符串的模塊 m.say_hi()
輸出
hello carlos
hello carlos
平常使用的大多數函數,以及開發中建立的函數,都屬於自定義函數。這極大的提升了代碼的重用性和可讀性。