一:類型和運(yùn)算
–1.1 尋求幫助:
dir(obj) # 簡(jiǎn)單的列出對(duì)象obj所包含的方法名稱,返回一個(gè)字符串列表help(obj.func) # 查詢obj.func的具體介紹和用法
– 1.2 測(cè)試類型的三種方法,推薦第三種
if type(L) == type([]): print('L is list')if type(L) == list: print('L is list')if isinstance(L, list): print('L is list')
– 1.3 Python數(shù)據(jù)類型:哈希類型、不可哈希類型
# 哈希類型,即在原地不能改變的變量類型,不可變類型??衫胔ash函數(shù)查看其hash值,也可以作為字典的key '數(shù)字類型:int, float, decimal.Decimal, fractions.Fraction, complex' '字符串類型:str, bytes' '元組:tuple' '凍結(jié)集合:frozenset' '布爾類型:True, False' 'None'# 不可hash類型:原地可變類型:list、dict和set。它們不可以作為字典的key。
– 1.4 數(shù)字常量
1234, -1234, 0, 999999999 # 整數(shù)1.23, 1., 3.14e-10, 4E210, 4.0e+210 # 浮點(diǎn)數(shù)0o177, 0x9ff, 0X9FF, 0b101010 # 八進(jìn)制、十六進(jìn)制、二進(jìn)制數(shù)字3+4j, 3.0+4.0j, 3J # 復(fù)數(shù)常量,也可以用complex(real, image)來創(chuàng)建hex(I), oct(I), bin(I) # 將十進(jìn)制數(shù)轉(zhuǎn)化為十六進(jìn)制、八進(jìn)制、二進(jìn)制表示的“字符串”int(str, base) # 將字符串轉(zhuǎn)化為整數(shù),base為進(jìn)制數(shù)# 2.x中,有兩種整數(shù)類型:一般整數(shù)(32位)和長(zhǎng)整數(shù)(無窮精度)??梢杂胠或L結(jié)尾,迫使一般整數(shù)成為長(zhǎng)整數(shù)float('inf'), float('-inf'), float('nan') # 無窮大, 無窮小, 非數(shù)
– 1.5 數(shù)字的表達(dá)式操作符
yield x # 生成器函數(shù)發(fā)送協(xié)議lambda args: expression # 生成匿名函數(shù)x if y else z # 三元選擇表達(dá)式x and y, x or y, not x # 邏輯與、邏輯或、邏輯非x in y, x not in y # 成員對(duì)象測(cè)試x is y, x is not y # 對(duì)象實(shí)體測(cè)試x<=y, x="">y, x>=y, x==y, x!=y # 大小比較,集合子集或超集值相等性操作符1 < a="">< 3="" #="" python中允許連續(xù)比較x|y,="" x&y,="" x^y="" #="">>y # 位操作:x左移、右移y位+, -, *, /, //, %, ** # 真除法、floor除法:返回不大于真除法結(jié)果的整數(shù)值、取余、冪運(yùn)算-x, +x, ~x # 一元減法、識(shí)別、按位求補(bǔ)(取反)x[i], x[i:j:k], x(……) # 索引、分片、調(diào)用int(3.14), float(3) # 強(qiáng)制類型轉(zhuǎn)換=y,>
– 1.6 整數(shù)可以利用bit_length函數(shù)測(cè)試所占的位數(shù)
a = 1; a.bit_length() # 1a = 1024; a.bit_length() # 11
– 1.7 repr和str顯示格式的區(qū)別
'''repr格式:默認(rèn)的交互模式回顯,產(chǎn)生的結(jié)果看起來它們就像是代碼。str格式:打印語句,轉(zhuǎn)化成一種對(duì)用戶更加友好的格式。'''
– 1.8 數(shù)字相關(guān)的模塊
# math模塊# Decimal模塊:小數(shù)模塊 import decimal from decimal import Decimal Decimal('0.01') + Decimal('0.02') # 返回Decimal('0.03') decimal.getcontext().prec = 4 # 設(shè)置全局精度為4 即小數(shù)點(diǎn)后邊4位# Fraction模塊:分?jǐn)?shù)模塊 from fractions import Fraction x = Fraction(4, 6) # 分?jǐn)?shù)類型 4/6 x = Fraction('0.25') # 分?jǐn)?shù)類型 1/4 接收字符串類型的參數(shù)
– 1.9 集合set
'''set是一個(gè)無序不重復(fù)元素集, 基本功能包括關(guān)系測(cè)試和消除重復(fù)元素。set支持union(聯(lián)合), intersection(交), difference(差)和sysmmetric difference(對(duì)稱差集)等數(shù)學(xué)運(yùn)算。set支持x in set, len(set), for x in set。set不記錄元素位置或者插入點(diǎn), 因此不支持indexing, slicing, 或其它類序列的操作'''s = set([3,5,9,10]) # 創(chuàng)建一個(gè)數(shù)值集合,返回{3, 5, 9, 10}t = set('Hello') # 創(chuàng)建一個(gè)唯一字符的集合返回{}a = t | s t.union(s) # t 和 s的并集b = t & s t.intersection(s) # t 和 s的交集c = t – s t.difference(s) # 求差集(項(xiàng)在t中, 但不在s中) d = t ^ s t.symmetric_difference(s) # 對(duì)稱差集(項(xiàng)在t或s中, 但不會(huì)同時(shí)出現(xiàn)在二者中)t.add('x') t.remove('H') # 增加/刪除一個(gè)itemt.update([10,37,42]) # 利用[......]更新s集合x in s, x not in s # 集合中是否存在某個(gè)值s.issubset(t) s.issuperset(t) s.copy() s.discard(x) s.clear(){x**2 for x in [1, 2, 3, 4]} # 集合解析,結(jié)果:{16, 1, 4, 9}{x for x in 'spam'} # 集合解析,結(jié)果:{'a', 'p', 's', 'm'}
– 1.10 集合frozenset,不可變對(duì)象
'''set是可變對(duì)象,即不存在hash值,不能作為字典的鍵值。同樣的還有l(wèi)ist、tuple等frozenset是不可變對(duì)象,即存在hash值,可作為字典的鍵值frozenset對(duì)象沒有add、remove等方法,但有union/intersection/difference等方法'''a = set([1, 2, 3])b = set()b.add(a) # error: set是不可哈希類型b.add(frozenset(a)) # ok,將set變?yōu)閒rozenset,可哈希
– 1.11 布爾類型bool
type(True) # 返回isinstance(False, int) # bool類型屬于整形,所以返回TrueTrue == 1, True is 1 # 輸出(True, False)
– 1.12 動(dòng)態(tài)類型簡(jiǎn)介
'''變量名通過引用,指向?qū)ο?。Python中的“類型”屬于對(duì)象,而不是變量,每個(gè)對(duì)象都包含有頭部信息,比如'類型標(biāo)示符' '引用計(jì)數(shù)器'等'''#共享引用及在原處修改:對(duì)于可變對(duì)象,要注意盡量不要共享引用!#共享引用和相等測(cè)試: L = [1], M = [1], L is M # 返回False L = M = [1, 2, 3], L is M # 返回True,共享引用#增強(qiáng)賦值和共享引用:普通+號(hào)會(huì)生成新的對(duì)象,而增強(qiáng)賦值+=會(huì)在原處修改 L = M = [1, 2] L = L + [3, 4] # L = [1, 2, 3, 4], M = [1, 2] L += [3, 4] # L = [1, 2, 3, 4], M = [1, 2, 3, 4]
– 1.13 常見字符串常量和表達(dá)式
S = '' # 空字符串S = 'spam’s' # 雙引號(hào)和單引號(hào)相同S = 's\np\ta\x00m' # 轉(zhuǎn)義字符S = '''spam''' # 三重引號(hào)字符串,一般用于函數(shù)說明S = r'\temp' # Raw字符串,不會(huì)進(jìn)行轉(zhuǎn)義,抑制轉(zhuǎn)義S = b'Spam' # Python3中的字節(jié)字符串S = u'spam' # Python2.6中的Unicode字符串s1+s2, s1*3, s[i], s[i:j], len(s) # 字符串操作'a %s parrot' % 'kind' # 字符串格式化表達(dá)式'a {0} parrot'.format('kind') # 字符串格式化方法for x in s: print(x) # 字符串迭代,成員關(guān)系[x*2 for x in s] # 字符串列表解析','.join(['a', 'b', 'c']) # 字符串輸出,結(jié)果:a,b,c
– 1.14 內(nèi)置str處理函數(shù):
str.upper() str.lower() str.swapcase() str.capitalize() str.title() # 全部大寫,全部小寫、大小寫轉(zhuǎn)換,首字母大寫,每個(gè)單詞的首字母都大寫str.ljust(width) # 獲取固定長(zhǎng)度,右對(duì)齊,左邊不夠用空格補(bǔ)齊str.rjust(width) # 獲取固定長(zhǎng)度,左對(duì)齊,右邊不夠用空格補(bǔ)齊str.center(width) # 獲取固定長(zhǎng)度,中間對(duì)齊,兩邊不夠用空格補(bǔ)齊str.zfill(width) # 獲取固定長(zhǎng)度,右對(duì)齊,左邊不足用0補(bǔ)齊str.find('t',start,end) # 查找字符串,可以指定起始及結(jié)束位置搜索str.rfind('t') # 從右邊開始查找字符串str.count('t') # 查找字符串出現(xiàn)的次數(shù)#上面所有方法都可用index代替,不同的是使用index查找不到會(huì)拋異常,而find返回-1str.replace('old','new') # 替換函數(shù),替換old為new,參數(shù)中可以指定maxReplaceTimes,即替換指定次數(shù)的old為newstr.strip() str.lstrip() str.rstrip() str.strip('d') str.lstrip('d') str.rstrip('d')str.startswith('start') # 是否以start開頭str.endswith('end') # 是否以end結(jié)尾str.isalnum() str.isalpha() str.isdigit() str.islower() str.isupper() # 判斷字符串是否全為字符、數(shù)字、大寫、小寫
– 1.15 三重引號(hào)編寫多行字符串塊,并且在代碼折行處嵌入換行字符\n
mantra = '''hello world hello python hello my friend'''# mantra為'''hello world \n hello python \n hello my friend'''
– 1.16 索引和分片:
S[0], S[len(S) – 1], S[-1] # 索引S[1:3], S[1:], S[:-1], S[1:10:2] # 分片,第三個(gè)參數(shù)指定步長(zhǎng)
– 1.17 字符串轉(zhuǎn)換工具:
int('42'), str(42) # 返回(42, '42')float('4.13'), str(4.13) # 返回(4.13, '4.13')ord('s'), chr(115) # 返回(115, 's')int('1001', 2) # 將字符串作為二進(jìn)制數(shù)字,轉(zhuǎn)化為數(shù)字,返回13bin(13), oct(13), hex(13) # 將整數(shù)轉(zhuǎn)化為二進(jìn)制/八進(jìn)制/十六進(jìn)制字符串,返回('1001', '0o15', '0xd')
– 1.18 另類字符串連接
name = 'wang' 'hong' #單行,name = 'wanghong'name = 'wang' \ 'hong' #多行,name = 'wanghong'
– 1.19 Python中的字符串格式化實(shí)現(xiàn)1(字符串格式化表達(dá)式)
'''基于C語言的'print'模型,并且在大多數(shù)的現(xiàn)有的語言中使用。通用結(jié)構(gòu):%[(name)][flag][width].[precision]typecode''''this is %d %s bird' % (1, 'dead') # 一般的格式化表達(dá)式'%s---%s---%s' % (42, 3.14, [1, 2, 3]) # 字符串輸出:'42---3.14---[1, 2, 3]''%d...%6d...%-6d...%06d' % (1234, 1234, 1234, 1234) # 對(duì)齊方式及填充:'1234... 1234...1234 ...001234'x = 1.23456789'%e | %f | %g' % (x, x, x) # 對(duì)齊方式:'1.234568e+00 | 1.234568 | 1.23457''%6.2f*%-6.2f*%06.2f*%+6.2f' % (x, x, x, x) # 對(duì)齊方式:' 1.23*1.23 *001.23* +1.23''%(name1)d---%(name2)s' % {'name1':23, 'name2':'value2'} # 基于字典的格式化表達(dá)式'%(name)s is %(age)d' % vars() # vars()函數(shù)調(diào)用返回一個(gè)字典,包含了所有本函數(shù)調(diào)用時(shí)存在的變量
– 1.20 Python中的字符串格式化實(shí)現(xiàn)2(字符串格式化調(diào)用方法)
# 普通調(diào)用'{0}, {1} and {2}'.format('spam', 'ham', 'eggs') # 基于位置的調(diào)用'{motto} and {pork}'.format(motto = 'spam', pork = 'ham') # 基于Key的調(diào)用'{motto} and {0}'.format(ham, motto = 'spam') # 混合調(diào)用# 添加鍵 屬性 偏移量 (import sys)'my {1[spam]} runs {0.platform}'.format(sys, {'spam':'laptop'}) # 基于位置的鍵和屬性'{config[spam]} {sys.platform}'.format(sys = sys, config = {'spam':'laptop'}) # 基于Key的鍵和屬性'first = {0[0]}, second = {0[1]}'.format(['A', 'B', 'C']) # 基于位置的偏移量# 具體格式化'{0:e}, {1:.3e}, {2:g}'.format(3.14159, 3.14159, 3.14159) # 輸出'3.141590e+00, 3.142e+00, 3.14159''{fieldname:format_spec}'.format(......)# 說明:''' fieldname是指定參數(shù)的一個(gè)數(shù)字或關(guān)鍵字, 后邊可跟可選的'.name'或'[index]'成分引用 format_spec ::= [[fill]align][sign][#][0][width][,][.precision][type] fill ::=
#填充字符 align ::= '<' |="" '="">' | '=' | '^' #對(duì)齊方式 sign ::= '+' | '-' | ' ' #符號(hào)說明 width ::= integer #字符串寬度 precision ::= integer #浮點(diǎn)數(shù)精度 type ::= 'b' | 'c' | 'd' | 'e' | 'E' | 'f' | 'F' | 'g' | 'G' | 'n' | 'o' | 's' | 'x' | 'X' | '%''''# 例子: '={0:10} = {1:10}'.format('spam', 123.456) # 輸出'=spam = 123.456' '={0:>10}='.format('test') # 輸出'= test=' '={0:<10}='.format('test') #="" 輸出'="test" ='="" '="{0:^10}='.format('test')" #="" 輸出'="test" ='="" '{0:x},="" {1:o},="" {2:b}'.format(255,="" 255,="" 255)="" #="" 輸出'ff,="" 377,="" 11111111'="" 'my="" name="" is="" {0:{1}}.'.format('fred',="" 8)="" #="" 輸出'my="" name="" is="" fred="" .'="">10}='.format('test')>'>– 1.21 常用列表常量和操作
L = [[1, 2], 'string', {}] # 嵌套列表L = list('spam') # 列表初始化L = list(range(0, 4)) # 列表初始化list(map(ord, 'spam')) # 列表解析len(L) # 求列表長(zhǎng)度L.count(value) # 求列表中某個(gè)值的個(gè)數(shù)L.append(obj) # 向列表的尾部添加數(shù)據(jù),比如append(2),添加元素2L.insert(index, obj) # 向列表的指定index位置添加數(shù)據(jù),index及其之后的數(shù)據(jù)后移L.extend(interable) # 通過添加iterable中的元素來擴(kuò)展列表,比如extend([2]),添加元素2,注意和append的區(qū)別L.index(value, [start, [stop]]) # 返回列表中值value的第一個(gè)索引L.pop([index]) # 刪除并返回index處的元素,默認(rèn)為刪除并返回最后一個(gè)元素L.remove(value) # 刪除列表中的value值,只刪除第一次出現(xiàn)的value的值L.reverse() # 反轉(zhuǎn)列表L.sort(cmp=None, key=None, reverse=False) # 排序列表a = [1, 2, 3], b = a[10:] # 注意,這里不會(huì)引發(fā)IndexError異常,只會(huì)返回一個(gè)空的列表[]a = [], a += [1] # 這里實(shí)在原有列表的基礎(chǔ)上進(jìn)行操作,即列表的id沒有改變a = [], a = a + [1] # 這里最后的a要構(gòu)建一個(gè)新的列表,即a的id發(fā)生了變化
– 1.22 用切片來刪除序列的某一段
a = [1, 2, 3, 4, 5, 6, 7]a[1:4] = [] # a = [1, 5, 6, 7]a = [0, 1, 2, 3, 4, 5, 6, 7]del a[::2] # 去除偶數(shù)項(xiàng)(偶數(shù)索引的),a = [1, 3, 5, 7]
– 1.23 常用字典常量和操作
D = {}D = {'spam':2, 'tol':{'ham':1}} # 嵌套字典D = dict.fromkeys(['s', 'd'], 8) # {'d': 8, 's': 8}D = dict(name = 'tom', age = 12) # {'age': 12, 'name': 'tom'}D = dict([('name', 'tom'), ('age', 12)]) # {'age': 12, 'name': 'tom'}D = dict(zip(['name', 'age'], ['tom', 12])) # {'age': 12, 'name': 'tom'}D.keys() D.values() D.items() # 字典鍵、值以及鍵值對(duì)D.get(key, default) # get函數(shù)D.update(D_other) # 合并字典,如果存在相同的鍵值,D_other的數(shù)據(jù)會(huì)覆蓋掉D的數(shù)據(jù)D.pop(key, [D]) # 刪除字典中鍵值為key的項(xiàng),返回鍵值為key的值,如果不存在,返回默認(rèn)值D,否則異常D.popitem() # pop字典中的一項(xiàng)(一個(gè)鍵值對(duì))D.setdefault(k[, d]) # 設(shè)置D中某一項(xiàng)的默認(rèn)值。如果k存在,則返回D[k],否則設(shè)置D[k]=d,同時(shí)返回D[k]。del D # 刪除字典del D['key'] # 刪除字典的某一項(xiàng)if key in D: if key not in D: # 測(cè)試字典鍵是否存在# 字典注意事項(xiàng):(1)對(duì)新索引賦值會(huì)添加一項(xiàng)(2)字典鍵不一定非得是字符串,也可以為任何的不可變對(duì)象
– 1.24 字典解析
D = {k:8 for k in ['s', 'd']} # {'d': 8, 's': 8}D = {k:v for (k, v) in zip(['name', 'age'], ['tom', 12])}
– 1.25 字典的特殊方法__missing__:當(dāng)查找找不到key時(shí),會(huì)執(zhí)行該方法
class Dict(dict): def __missing__(self, key): self[key] = [] return self[key]dct = Dict()dct['foo'].append(1) # 這有點(diǎn)類似于collections.defalutdictdct['foo'] # [1]
– 1.26 元組和列表的唯一區(qū)別在于元組是不可變對(duì)象,列表時(shí)可變對(duì)象
a = [1, 2, 3] # a[1] = 0, OKa = (1, 2, 3) # a[1] = 0, Errora = ([1, 2]) # a[0][1] = 0, OKa = [(1, 2)] # a[0][1] = 0, Error
– 1.27 元組的特殊語法: 逗號(hào)和圓括號(hào)
D = (12) # 此時(shí)D為一個(gè)整數(shù) 即D = 12D = (12, ) # 此時(shí)D為一個(gè)元組 即D = (12, )
– 1.28 文件基本操作
output = open(r'C:\spam', 'w') # 打開輸出文件,用于寫input = open('data', 'r') # 打開輸入文件,用于讀。打開的方式可以為'w', 'r', 'a', 'wb', 'rb', 'ab'等fp.read([size]) # size為讀取的長(zhǎng)度,以byte為單位fp.readline([size]) # 讀一行,如果定義了size,有可能返回的只是一行的一部分fp.readlines([size]) # 把文件每一行作為一個(gè)list的一個(gè)成員,并返回這個(gè)list。其實(shí)它的內(nèi)部是通過循環(huán)調(diào)用readline()來實(shí)現(xiàn)的。如果提供size參數(shù),size是表示讀取內(nèi)容的總長(zhǎng)。fp.readable() # 是否可讀fp.write(str) # 把str寫到文件中,write()并不會(huì)在str后加上一個(gè)換行符fp.writelines(seq) # 把seq的內(nèi)容全部寫到文件中(多行一次性寫入)fp.writeable() # 是否可寫fp.close() # 關(guān)閉文件。fp.flush() # 把緩沖區(qū)的內(nèi)容寫入硬盤fp.fileno() # 返回一個(gè)長(zhǎng)整型的”文件標(biāo)簽“fp.isatty() # 文件是否是一個(gè)終端設(shè)備文件(unix系統(tǒng)中的)fp.tell() # 返回文件操作標(biāo)記的當(dāng)前位置,以文件的開頭為原點(diǎn)fp.next() # 返回下一行,并將文件操作標(biāo)記位移到下一行。把一個(gè)file用于for … in file這樣的語句時(shí),就是調(diào)用next()函數(shù)來實(shí)現(xiàn)遍歷的。fp.seek(offset[,whence]) # 將文件打操作標(biāo)記移到offset的位置。whence可以為0表示從頭開始計(jì)算,1表示以當(dāng)前位置為原點(diǎn)計(jì)算。2表示以文件末尾為原點(diǎn)進(jìn)行計(jì)算。fp.seekable() # 是否可以seekfp.truncate([size]) # 把文件裁成規(guī)定的大小,默認(rèn)的是裁到當(dāng)前文件操作標(biāo)記的位置。for line in open('data'): print(line) # 使用for語句,比較適用于打開比較大的文件open('f.txt', encoding = 'latin-1') # Python3.x Unicode文本文件open('f.bin', 'rb') # Python3.x 二進(jìn)制bytes文件# 文件對(duì)象還有相應(yīng)的屬性:buffer closed encoding errors line_buffering name newlines等
– 1.29 其他
# Python中的真假值含義:1. 數(shù)字如果非零,則為真,0為假。 2. 其他對(duì)象如果非空,則為真# 通常意義下的類型分類:1. 數(shù)字、序列、映射。 2. 可變類型和不可變類型
二:語法和語句
– 2.1 賦值語句的形式
spam = 'spam' # 基本形式spam, ham = 'spam', 'ham' # 元組賦值形式[spam, ham] = ['s', 'h'] # 列表賦值形式a, b, c, d = 'abcd' # 序列賦值形式a, *b, c = 'spam' # 序列解包形式(Python3.x中才有)spam = ham = 'no' # 多目標(biāo)賦值運(yùn)算,涉及到共享引用spam += 42 # 增強(qiáng)賦值,涉及到共享引用
– 2.2 序列賦值 序列解包
[a, b, c] = (1, 2, 3) # a = 1, b = 2, c = 3a, b, c, d = 'spam' # a = 's', b = 'p'a, b, c = range(3) # a = 0, b = 1a, *b = [1, 2, 3, 4] # a = 1, b = [2, 3, 4]*a, b = [1, 2, 3, 4] # a = [1, 2, 3], b = 4a, *b, c = [1, 2, 3, 4] # a = 1, b = [2, 3], c = 4# 帶有*時(shí) 會(huì)優(yōu)先匹配*之外的變量 如a, *b, c = [1, 2] # a = 1, c = 2, b = []
– 2.3 print函數(shù)原型
print(value, ..., sep=' ', end='\n', file=sys.stdout, flush=False)# 流的重定向print('hello world') # 等于sys.stdout.write('hello world')temp = sys.stdout # 原有流的保存sys.stdout = open('log.log', 'a') # 流的重定向print('hello world') # 寫入到文件log.logsys.stdout.close()sys.stdout = temp # 原有流的復(fù)原
– 2.4 Python中and或or總是返回對(duì)象(左邊的對(duì)象或右邊的對(duì)象) 且具有短路求值的特性
1 or 2 or 3 # 返回 11 and 2 and 3 # 返回 3
– 2.5 if/else三元表達(dá)符(if語句在行內(nèi))
A = 1 if X else 2A = 1 if X else (2 if Y else 3)# 也可以使用and-or語句(一條語句實(shí)現(xiàn)多個(gè)if-else)result = (a > 20 and 'big than 20' or a > 10 and 'big than 10' or a > 5 and 'big than 5')
– 2.6 Python的while語句或者for語句可以帶else語句 當(dāng)然也可以帶continue/break/pass語句
while a > 1: ......else: ......# else語句會(huì)在循環(huán)結(jié)束后執(zhí)行,除非在循環(huán)中執(zhí)行了break,同樣的還有for語句for i in range(5): ......else: ......
– 2.7 for循環(huán)的元組賦值
for (a, b) in [(1, 2), (3, 4)]: # 最簡(jiǎn)單的賦值for ((a, b), c) in [((1, 2), 3), ((4, 5), 6)]: # 自動(dòng)解包賦值for ((a, b), c) in [((1, 2), 3), ('XY', 6)]: # 自動(dòng)解包 a = X, b = Y, c = 6for (a, *b) in [(1, 2, 3), (4, 5, 6)]: # 自動(dòng)解包賦值
– 2.8 列表解析語法
M = [[1,2,3], [4,5,6], [7,8,9]]res = [sum(row) for row in M] # G = [6, 15, 24] 一般的列表解析 生成一個(gè)列表res = [c * 2 for c in 'spam'] # ['ss', 'pp', 'aa', 'mm']res = [a * b for a in [1, 2] for b in [4, 5]] # 多解析過程 返回[4, 5, 8, 10]res = [a for a in [1, 2, 3] if a < 2]="" #="" 帶判斷條件的解析過程res="[a" if="" a=""> 0 else 0 for a in [-1, 0, 1]] # 帶判斷條件的高級(jí)解析過程# 兩個(gè)列表同時(shí)解析:使用zip函數(shù)for teama, teamb in zip(['Packers', '49ers'], ['Ravens', 'Patriots']): print(teama + ' vs. ' + teamb)# 帶索引的列表解析:使用enumerate函數(shù)for index, team in enumerate(['Packers', '49ers', 'Ravens', 'Patriots']): print(index, team) # 輸出0, Packers \n 1, 49ers \n ......
– 2.9 生成器表達(dá)式
G = (sum(row) for row in M) # 使用小括號(hào)可以創(chuàng)建所需結(jié)果的生成器generator objectnext(G), next(G), next(G) # 輸出(6, 15, 24)G = {sum(row) for row in M} # G = {6, 15, 24} 解析語法還可以生成集合和字典G = {i:sum(M[i]) for i in range(3)} # G = {0: 6, 1: 15, 2: 24}
– 2.10 文檔字符串:出現(xiàn)在Module的開端以及其中函數(shù)或類的開端 使用三重引號(hào)字符串
'''module document'''def func(): ''' function document ''' print()class Employee: ''' class document ''' print()print(func.__doc__) # 輸出函數(shù)文檔字符串print(Employee.__doc__) # 輸出類的文檔字符串
– 2.11 命名慣例:
'''以單一下劃線開頭的變量名(_X)不會(huì)被from module import*等語句導(dǎo)入前后有兩個(gè)下劃線的變量名(__X__)是系統(tǒng)定義的變量名,對(duì)解釋器有特殊意義以兩個(gè)下劃線開頭但不以下劃線結(jié)尾的變量名(__X)是類的本地(私有)變量'''
– 2.12 列表解析 in成員關(guān)系測(cè)試 map sorted zip enumerate內(nèi)置函數(shù)等都使用了迭代協(xié)議
'first line' in open('test.txt') # in測(cè)試 返回True或Falselist(map(str.upper, open('t'))) # map內(nèi)置函數(shù)sorted(iter([2, 5, 8, 3, 1])) # sorted內(nèi)置函數(shù)list(zip([1, 2], [3, 4])) # zip內(nèi)置函數(shù) [(1, 3), (2, 4)]
– 2.13 del語句: 手動(dòng)刪除某個(gè)變量
del X
– 2.14 獲取列表的子表的方法:
x = [1,2,3,4,5,6]x[:3] # 前3個(gè)[1,2,3]x[1:5] # 中間4個(gè)[2,3,4,5]x[-3:] # 最后3個(gè)[4,5,6]x[::2] # 奇數(shù)項(xiàng)[1,3,5]x[1::2] # 偶數(shù)項(xiàng)[2,4,6]
– 2.15 手動(dòng)迭代:iter和next
L = [1, 2]I = iter(L) # I為L(zhǎng)的迭代器I.next() # 返回1I.next() # 返回2I.next() # Error:StopIteration
– 2.16 Python中的可迭代對(duì)象
'''1.range迭代器2.map、zip和filter迭代器3.字典視圖迭代器:D.keys()), D.items()等4.文件類型'''
三:函數(shù)語法規(guī)則
– 3.1 函數(shù)相關(guān)的語句和表達(dá)式
myfunc('spam') # 函數(shù)調(diào)用def myfunc(): # 函數(shù)定義return None # 函數(shù)返回值global a # 全局變量nonlocal x # 在函數(shù)或其他作用域中使用外層(非全局)變量yield x # 生成器函數(shù)返回lambda # 匿名函數(shù)
– 3.2 Python函數(shù)變量名解析:LEGB原則,即:
'''local(functin) --> encloseing function locals --> global(module) --> build-in(python)說明:以下邊的函數(shù)maker為例 則相對(duì)于action而言 X為L(zhǎng)ocal N為Encloseing'''
– 3.3 嵌套函數(shù)舉例:工廠函數(shù)
def maker(N): def action(X): return X ** N return actionf = maker(2) # pass 2 to Nf(3) # 9, pass 3 to X
– 3.4 嵌套函數(shù)舉例:lambda實(shí)例
def maker(N): action = (lambda X: X**N) return actionf = maker(2) # pass 2 to Nf(3) # 9, pass 3 to X
– 3.5 nonlocal和global語句的區(qū)別
# nonlocal應(yīng)用于一個(gè)嵌套的函數(shù)的作用域中的一個(gè)名稱 例如:start = 100def tester(start): def nested(label): nonlocal start # 指定start為tester函數(shù)內(nèi)的local變量 而不是global變量start print(label, start) start += 3 return nested# global為全局的變量 即def之外的變量def tester(start): def nested(label): global start # 指定start為global變量start print(label, start) start += 3 return nested
– 3.6 函數(shù)參數(shù),不可變參數(shù)通過“值”傳遞,可變參數(shù)通過“引用”傳遞
def f(a, b, c): print(a, b, c)f(1, 2, 3) # 參數(shù)位置匹配f(1, c = 3, b = 2) # 參數(shù)關(guān)鍵字匹配def f(a, b = 1, c = 2): print(a, b, c)f(1) # 默認(rèn)參數(shù)匹配f(1, 2) # 默認(rèn)參數(shù)匹配f(a = 1, c = 3) # 關(guān)鍵字參數(shù)和默認(rèn)參數(shù)的混合# Keyword-Only參數(shù):出現(xiàn)在*args之后 必須用關(guān)鍵字進(jìn)行匹配def keyOnly(a, *b, c): print('') # c就為keyword-only匹配 必須使用關(guān)鍵字c = value匹配def keyOnly(a, *, b, c): ...... # b c為keyword-only匹配 必須使用關(guān)鍵字匹配def keyOnly(a, *, b = 1): ...... # b有默認(rèn)值 或者省略 或者使用關(guān)鍵字參數(shù)b = value
– 3.7 可變參數(shù)匹配: * 和 **
def f(*args): print(args) # 在元組中收集不匹配的位置參數(shù)f(1, 2, 3) # 輸出(1, 2, 3)def f(**args): print(args) # 在字典中收集不匹配的關(guān)鍵字參數(shù)f(a = 1, b = 2) # 輸出{'a':1, 'b':2}def f(a, *b **c): print(a, b, c) # 兩者混合使用f(1, 2, 3, x = 4, y = 5) # 輸出1, (2, 3), {'x':4, 'y':5}
– 3.8 函數(shù)調(diào)用時(shí)的參數(shù)解包: * 和 ** 分別解包元組和字典
func(1, *(2, 3)) <==> func(1, 2, 3)func(1, **{'c':3, 'b':2}) <==> func(1, b = 2, c = 3)func(1, *(2, 3), **{'c':3, 'b':2}) <==> func(1, 2, 3, b = 2, c = 3)==>==>==>
– 3.9 函數(shù)屬性:(自己定義的)函數(shù)可以添加屬性
def func():.....func.count = 1 # 自定義函數(shù)添加屬性print.count = 1 # Error 內(nèi)置函數(shù)不可以添加屬性
– 3.10 函數(shù)注解: 編寫在def頭部行 主要用于說明參數(shù)范圍、參數(shù)類型、返回值類型等
def func(a:'spam', b:(1, 10), c:float) -> int : print(a, b, c)func.__annotations__ # {'c':, 'b':(1, 10), 'a':'spam', 'return':}# 編寫注解的同時(shí) 還是可以使用函數(shù)默認(rèn)值 并且注解的位置位于=號(hào)的前邊def func(a:'spam'='a', b:(1, 10)=2, c:float=3) -> int : print(a, b, c)
– 3.11 匿名函數(shù):lambda
f = lambda x, y, z : x + y + z # 普通匿名函數(shù),使用方法f(1, 2, 3)f = lambda x = 1, y = 1: x + y # 帶默認(rèn)參數(shù)的lambda函數(shù)def action(x): # 嵌套lambda函數(shù) return (lambda y : x + y)f = lambda: a if xxx() else b # 無參數(shù)的lambda函數(shù),使用方法f()
– 3.12 lambda函數(shù)與map filter reduce函數(shù)的結(jié)合
list(map((lambda x: x + 1), [1, 2, 3])) # [2, 3, 4]list(filter((lambda x: x > 0), range(-4, 5))) # [1, 2, 3, 4]functools.reduce((lambda x, y: x + y), [1, 2, 3]) # 6functools.reduce((lambda x, y: x * y), [2, 3, 4]) # 24
– 3.13 生成器函數(shù):yield VS return
def gensquare(N): for i in range(N): yield i** 2 # 狀態(tài)掛起 可以恢復(fù)到此時(shí)的狀態(tài)for i in gensquare(5): # 使用方法 print(i, end = ' ') # [0, 1, 4, 9, 16]x = gensquare(2) # x是一個(gè)生成對(duì)象next(x) # 等同于x.__next__() 返回0next(x) # 等同于x.__next__() 返回1next(x) # 等同于x.__next__() 拋出異常StopIteration
– 3.14 生成器表達(dá)式:小括號(hào)進(jìn)行列表解析
G = (x ** 2 for x in range(3)) # 使用小括號(hào)可以創(chuàng)建所需結(jié)果的生成器generator objectnext(G), next(G), next(G) # 和上述中的生成器函數(shù)的返回值一致#(1)生成器(生成器函數(shù)/生成器表達(dá)式)是單個(gè)迭代對(duì)象G = (x ** 2 for x in range(4))I1 = iter(G) # 這里實(shí)際上iter(G) = Gnext(I1) # 輸出0next(G) # 輸出1next(I1) # 輸出4#(2)生成器不保留迭代后的結(jié)果gen = (i for i in range(4))2 in gen # 返回True3 in gen # 返回True1 in gen # 返回False,其實(shí)檢測(cè)2的時(shí)候,1已經(jīng)就不在生成器中了,即1已經(jīng)被迭代過了,同理2、3也不在了
– 3.15 本地變量是靜態(tài)檢測(cè)的
X = 22 # 全局變量X的聲明和定義def test(): print(X) # 如果沒有下一語句 則該句合法 打印全局變量X X = 88 # 這一語句使得上一語句非法 因?yàn)樗沟肵變成了本地變量 上一句變成了打印一個(gè)未定義的本地變量(局部變量) if False: # 即使這樣的語句 也會(huì)把print語句視為非法語句 因?yàn)? X = 88 # Python會(huì)無視if語句而仍然聲明了局部變量Xdef test(): # 改進(jìn) global X # 聲明變量X為全局變量 print(X) # 打印全局變量X X = 88 # 改變?nèi)肿兞縓
– 3.16 函數(shù)的默認(rèn)值是在函數(shù)定義的時(shí)候?qū)嵗?而不是在調(diào)用的時(shí)候 例子:
def foo(numbers=[]): # 這里的[]是可變的 numbers.append(9) print(numbers)foo() # first time, like before, [9]foo() # second time, not like before, [9, 9]foo() # third time, not like before too, [9, 9, 9]# 改進(jìn):def foo(numbers=None): if numbers is None: numbers = [] numbers.append(9) print(numbers)# 另外一個(gè)例子 參數(shù)的默認(rèn)值為不可變的:def foo(count=0): # 這里的0是數(shù)字, 是不可變的 count += 1 print(count)foo() # 輸出1foo() # 還是輸出1foo(3) # 輸出4foo() # 還是輸出1
四:函數(shù)例子
– 4.1 數(shù)學(xué)運(yùn)算類
abs(x) # 求絕對(duì)值,參數(shù)可以是整型,也可以是復(fù)數(shù),若參數(shù)是復(fù)數(shù),則返回復(fù)數(shù)的模complex([real[, imag]]) # 創(chuàng)建一個(gè)復(fù)數(shù)divmod(a, b) # 分別取商和余數(shù),注意:整型、浮點(diǎn)型都可以float([x]) # 將一個(gè)字符串或數(shù)轉(zhuǎn)換為浮點(diǎn)數(shù)。如果無參數(shù)將返回0.0int([x[, base]]) # 將一個(gè)字符串或浮點(diǎn)數(shù)轉(zhuǎn)換為int類型,base表示進(jìn)制long([x[, base]]) # 將一個(gè)字符串或浮點(diǎn)數(shù)轉(zhuǎn)換為long類型pow(x, y) # 返回x的y次冪range([start], stop[, step]) # 產(chǎn)生一個(gè)序列,默認(rèn)從0開始round(x[, n]) # 四舍五入sum(iterable[, start]) # 對(duì)集合求和oct(x) # 將一個(gè)數(shù)字轉(zhuǎn)化為8進(jìn)制字符串hex(x) # 將一個(gè)數(shù)字轉(zhuǎn)換為16進(jìn)制字符串chr(i) # 返回給定int類型對(duì)應(yīng)的ASCII字符unichr(i) # 返回給定int類型的unicodeord(c) # 返回ASCII字符對(duì)應(yīng)的整數(shù)bin(x) # 將整數(shù)x轉(zhuǎn)換為二進(jìn)制字符串bool([x]) # 將x轉(zhuǎn)換為Boolean類型
– 4.2 集合類操作
basestring() # str和unicode的超類,不能直接調(diào)用,可以用作isinstance判斷format(value [, format_spec]) # 格式化輸出字符串,格式化的參數(shù)順序從0開始,如“I am {0},I like {1}”enumerate(sequence[, start=0]) # 返回一個(gè)可枚舉的對(duì)象,注意它有第二個(gè)參數(shù)iter(obj[, sentinel]) # 生成一個(gè)對(duì)象的迭代器,第二個(gè)參數(shù)表示分隔符max(iterable[, args...][key]) # 返回集合中的最大值min(iterable[, args...][key]) # 返回集合中的最小值dict([arg]) # 創(chuàng)建數(shù)據(jù)字典list([iterable]) # 將一個(gè)集合類轉(zhuǎn)換為另外一個(gè)集合類set() # set對(duì)象實(shí)例化frozenset([iterable]) # 產(chǎn)生一個(gè)不可變的settuple([iterable]) # 生成一個(gè)tuple類型str([object]) # 轉(zhuǎn)換為string類型sorted(iterable[, cmp[, key[, reverse]]]) # 集合排序 L = [('b',2),('a',1),('c',3),('d',4)] sorted(L, key=lambda x: x[1]), reverse=True) # 使用Key參數(shù)和reverse參數(shù) sorted(L, key=lambda x: (x[0], x[1])) # 使用key參數(shù)進(jìn)行多條件排序,即如果x[0]相同,則比較x[1]
– 4.3 邏輯判斷
all(iterable) # 集合中的元素都為真的時(shí)候?yàn)檎妫貏e的,若為空串返回為Trueany(iterable) # 集合中的元素有一個(gè)為真的時(shí)候?yàn)檎妫貏e的,若為空串返回為Falsecmp(x, y) # 如果x < y="" ,返回負(fù)數(shù);x="=" y,="" 返回0;x=""> y,返回正數(shù)'''IO操作'''file(filename [, mode [, bufsize]]) # file類型的構(gòu)造函數(shù)。input([prompt]) # 獲取用戶輸入,推薦使用raw_input,因?yàn)樵摵瘮?shù)將不會(huì)捕獲用戶的錯(cuò)誤輸入raw_input([prompt]) # 設(shè)置輸入,輸入都是作為字符串處理open(name[, mode[, buffering]]) # 打開文件,與file有什么不同?推薦使用open
– 4.4 其他
callable(object) # 檢查對(duì)象object是否可調(diào)用classmethod(func) # 用來說明這個(gè)func是個(gè)類方法staticmethod(func) # 用來說明這個(gè)func為靜態(tài)方法dir([object]) # 不帶參數(shù)時(shí),返回當(dāng)前范圍內(nèi)的變量、方法和定義的類型列表;帶參數(shù)時(shí),返回參數(shù)的屬性、方法列表。help(obj) # 返回obj的幫助信息eval(expression) # 計(jì)算表達(dá)式expression的值,并返回exec(str) # 將str作為Python語句執(zhí)行execfile(filename) # 用法類似exec(),不同的是execfile的參數(shù)filename為文件名,而exec的參數(shù)為字符串。filter(function, iterable) # 構(gòu)造一個(gè)序列,等價(jià)于[item for item in iterable if function(item)],function返回值為True或False的函數(shù) list(filter(bool, range(-3, 4)))# 返回[-3, -2, -1, 1, 2, 3], 沒有0hasattr(object, name) # 判斷對(duì)象object是否包含名為name的特性getattr(object, name [, defalut]) # 獲取一個(gè)類的屬性setattr(object, name, value) # 設(shè)置屬性值delattr(object, name) # 刪除object對(duì)象名為name的屬性globals() # 返回一個(gè)描述當(dāng)前全局符號(hào)表的字典hash(object) # 如果對(duì)象object為哈希表類型,返回對(duì)象object的哈希值id(object) # 返回對(duì)象的唯一標(biāo)識(shí),一串?dāng)?shù)字isinstance(object, classinfo) # 判斷object是否是class的實(shí)例 isinstance(1, int) # 判斷是不是int類型 isinstance(1, (int, float)) # isinstance的第二個(gè)參數(shù)接受一個(gè)元組類型issubclass(class, classinfo) # 判斷class是否為classinfo的子類locals() # 返回當(dāng)前的變量列表map(function, iterable, ...) # 遍歷每個(gè)元素,執(zhí)行function操作 list(map(abs, range(-3, 4))) # 返回[3, 2, 1, 0, 1, 2, 3]next(iterator[, default]) # 類似于iterator.next()property([fget[, fset[, fdel[, doc]]]]) # 屬性訪問的包裝類,設(shè)置后可以通過c.x=value等來訪問setter和getterreduce(function, iterable[, initializer]) # 合并操作,從第一個(gè)開始是前兩個(gè)參數(shù),然后是前兩個(gè)的結(jié)果與第三個(gè)合并進(jìn)行處理,以此類推 def add(x,y):return x + y reduce(add, range(1, 11)) # 返回55 (注:1+2+3+4+5+6+7+8+9+10 = 55) reduce(add, range(1, 11), 20) # 返回75reload(module) # 重新加載模塊repr(object) # 將一個(gè)對(duì)象變幻為可打印的格式slice(start, stop[, step]) # 產(chǎn)生分片對(duì)象type(object) # 返回該object的類型vars([object]) # 返回對(duì)象的變量名、變量值得字典 a = Class(); # Class為一個(gè)空類 a.name = 'qi', a.age = 9 vars(a) # {'name':'qi', 'age':9}zip([iterable, ...]) # 返回對(duì)應(yīng)數(shù)組 list(zip([1, 2, 3], [4, 5, 6])) # [(1, 4), (2, 5), (3, 6)] a = [1, 2, 3], b = ['a', 'b', 'c'] z = zip(a, b) # 壓縮:[(1, 'a'), (2, 'b'), (3, 'c')] zip(*z) # 解壓縮:[(1, 2, 3), ('a', 'b', 'c')]unicode(string, encoding, errors) # 將字符串string轉(zhuǎn)化為unicode形式,string為encoded string。
五:模塊Moudle
– 5.1 Python模塊搜索路徑:
'''(1)程序的主目錄 (2)PYTHONPATH目錄 (3)標(biāo)準(zhǔn)鏈接庫目錄 (4)任何.pth文件的內(nèi)容'''
– 5.2 查看全部的模塊搜索路徑
import syssys.path
– 5.3模塊的使用代碼
import module1, module2 # 導(dǎo)入module1 使用module1.printer()from module1 import printer # 導(dǎo)入module1中的printer變量 使用printer()from module1 imoprt * # 導(dǎo)入module1中的全部變量 使用不必添加module1前綴
重載模塊reload: 這是一個(gè)內(nèi)置函數(shù) 而不是一條語句
from imp import reload
reload(module)
– 5.4 模塊的包導(dǎo)入:使用點(diǎn)號(hào)(.)而不是路徑(dir1\dir2)進(jìn)行導(dǎo)入
import dir1.dir2.mod # d導(dǎo)入包(目錄)dir1中的包dir2中的mod模塊 此時(shí)dir1必須在Python可搜索路徑中from dir1.dir2.mod import * # from語法的包導(dǎo)入
__init__.py包文件:每個(gè)導(dǎo)入的包中都應(yīng)該包含這么一個(gè)文件
“””
該文件可以為空
首次進(jìn)行包導(dǎo)入時(shí) 該文件會(huì)自動(dòng)執(zhí)行
高級(jí)功能:在該文件中使用all列表來定義包(目錄)以from*的形式導(dǎo)入時(shí) 需要導(dǎo)入什么
“”“
– 5.5 包相對(duì)導(dǎo)入:使用點(diǎn)號(hào)(.) 只能使用from語句
from . import spam # 導(dǎo)入當(dāng)前目錄下的spam模塊(錯(cuò)誤: 當(dāng)前目錄下的模塊, 直接導(dǎo)入即可)from .spam import name # 導(dǎo)入當(dāng)前目錄下的spam模塊的name屬性(錯(cuò)誤: 當(dāng)前目錄下的模塊, 直接導(dǎo)入即可,不用加.)from .. import spam # 導(dǎo)入當(dāng)前目錄的父目錄下的spam模塊
– 5.6 包相對(duì)導(dǎo)入與普通導(dǎo)入的區(qū)別
from string import * # 這里導(dǎo)入的string模塊為sys.path路徑上的 而不是本目錄下的string模塊(如果存在也不是)from .string import * # 這里導(dǎo)入的string模塊為本目錄下的(不存在則導(dǎo)入失敗) 而不是sys.path路徑上的
– 5.7 模塊數(shù)據(jù)隱藏:最小化from*的破壞
_X # 變量名前加下劃線可以防止from*導(dǎo)入時(shí)該變量名被復(fù)制出去__all__ = ['x', 'x1', 'x2'] # 使用__all__列表指定from*時(shí)復(fù)制出去的變量名(變量名在列表中為字符串形式)
可以使用__name__進(jìn)行模塊的單元測(cè)試:當(dāng)模塊為頂層執(zhí)行文件時(shí)值為’__main__’ ,當(dāng)模塊被導(dǎo)入時(shí)為模塊名
if __name__ == '__main__': doSomething# 模塊屬性中還有其他屬性,例如:__doc__ # 模塊的說明文檔__file__ # 模塊文件的文件名,包括全路徑__name__ # 主文件或者被導(dǎo)入文件__package__ # 模塊所在的包
– 5.8 import語句from語句的as擴(kuò)展
import modulename as namefrom modulename import attrname as name
– 5.9 得到模塊屬性的幾種方法 假設(shè)為了得到name屬性的值
M.nameM.__dict__['name']sys.modules['M'].namegetattr(M, 'name')
六:類與面向?qū)ο?/p>
– 6.1 最普通的類
class C1(C2, C3): spam = 42 # 數(shù)據(jù)屬性 def __init__(self, name): # 函數(shù)屬性:構(gòu)造函數(shù) self.name = name def __del__(self): # 函數(shù)屬性:析構(gòu)函數(shù) print('goodbey ', self.name) I1 = C1('bob')
– 6.2 Python的類沒有基于參數(shù)的函數(shù)重載
class FirstClass: def test(self, string): print(string) def test(self): # 此時(shí)類中只有一個(gè)test函數(shù) 即后者test(self) 它覆蓋掉前者帶參數(shù)的test函數(shù) print('hello world')
– 6.3 子類擴(kuò)展超類: 盡量調(diào)用超類的方法
class Manager(Person): def giveRaise(self, percent, bonus = .10): self.pay = int(self.pay*(1 + percent + bonus)) # 不好的方式 復(fù)制粘貼超類代碼 Person.giveRaise(self, percent + bonus) # 好的方式 盡量調(diào)用超類方法
– 6.4 類內(nèi)省工具
bob = Person('bob')bob.__class__ # bob.__class__.__name__ # 'Person'bob.__dict__ # {'pay':0, 'name':'bob', 'job':'Manager'}
返回1中 數(shù)據(jù)屬性spam是屬于類 而不是對(duì)象
I1 = C1('bob'); I2 = C2('tom') # 此時(shí)I1和I2的spam都為42 但是都是返回的C1的spam屬性C1.spam = 24 # 此時(shí)I1和I2的spam都為24I1.spam = 3 # 此時(shí)I1新增自有屬性spam 值為2 I2和C1的spam還都為24
– 6.5 類方法調(diào)用的兩種方式
instance.method(arg...)class.method(instance, arg...)
– 6.6 抽象超類的實(shí)現(xiàn)方法
# (1)某個(gè)函數(shù)中調(diào)用未定義的函數(shù) 子類中定義該函數(shù) def delegate(self): self.action() # 本類中不定義action函數(shù) 所以使用delegate函數(shù)時(shí)就會(huì)出錯(cuò)# (2)定義action函數(shù) 但是返回異常 def action(self): raise NotImplementedError('action must be defined')# (3)上述的兩種方法還都可以定義實(shí)例對(duì)象 實(shí)際上可以利用@裝飾器語法生成不能定義的抽象超類 from abc import ABCMeta, abstractmethod class Super(metaclass = ABCMeta): @abstractmethod def action(self): pass x = Super() # 返回 TypeError: Can't instantiate abstract class Super with abstract methods action
OOP和繼承: “is - a”的關(guān)系
class A(B): passa = A()isinstance(a, B) # 返回True, A是B的子類 a也是B的一種# OOP和組合: 'has- a'的關(guān)系pass# OOP和委托: '包裝'對(duì)象 在Python中委托通常是以'__getattr__'鉤子方法實(shí)現(xiàn)的, 這個(gè)方法會(huì)攔截對(duì)不存在屬性的讀取# 包裝類(或者稱為代理類)可以使用__getattr__把任意讀取轉(zhuǎn)發(fā)給被包裝的對(duì)象class wrapper: def __init__(self, object): self.wrapped = object def __getattr(self, attrname): print('Trace: ', attrname) return getattr(self.wrapped, attrname)# 注:這里使用getattr(X, N)內(nèi)置函數(shù)以變量名字符串N從包裝對(duì)象X中取出屬性 類似于X.__dict__[N]x = wrapper([1, 2, 3])x.append(4) # 返回 'Trace: append' [1, 2, 3, 4]x = wrapper({'a':1, 'b':2})list(x.keys()) # 返回 'Trace: keys' ['a', 'b']
– 6.7 類的偽私有屬性:使用__attr
class C1: def __init__(self, name): self.__name = name # 此時(shí)類的__name屬性為偽私有屬性 原理 它會(huì)自動(dòng)變成self._C1__name = name def __str__(self): return 'self.name = %s' % self.__nameI = C1('tom')print(I) # 返回 self.name = tomI.__name = 'jeey' # 這里無法訪問 __name為偽私有屬性I._C1__name = 'jeey' # 這里可以修改成功 self.name = jeey
– 6.8 類方法是對(duì)象:無綁定類方法對(duì)象 / 綁定實(shí)例方法對(duì)象
class Spam: def doit(self, message): print(message) def selfless(message) print(message)obj = Spam()x = obj.doit # 類的綁定方法對(duì)象 實(shí)例 + 函數(shù)x('hello world')x = Spam.doit # 類的無綁定方法對(duì)象 類名 + 函數(shù)x(obj, 'hello world')x = Spam.selfless # 類的無綁定方法是函數(shù) 在3.0之前無效x('hello world')
– 6.9 獲取對(duì)象信息: 屬性和方法
a = MyObject()dir(a) # 使用dir函數(shù)hasattr(a, 'x') # 測(cè)試是否有x屬性或方法 即a.x是否已經(jīng)存在setattr(a, 'y', 19) # 設(shè)置屬性或方法 等同于a.y = 19getattr(a, 'z', 0) # 獲取屬性或方法 如果屬性不存在 則返回默認(rèn)值0#這里有個(gè)小技巧,setattr可以設(shè)置一個(gè)不能訪問到的屬性,即只能用getattr獲取setattr(a, 'can't touch', 100) # 這里的屬性名帶有空格,不能直接訪問getattr(a, 'can't touch', 0) # 但是可以用getattr獲取
– 6.10 為類動(dòng)態(tài)綁定屬性或方法: MethodType方法
# 一般創(chuàng)建了一個(gè)class的實(shí)例后, 可以給該實(shí)例綁定任何屬性和方法, 這就是動(dòng)態(tài)語言的靈活性class Student(object): passs = Student()s.name = 'Michael' # 動(dòng)態(tài)給實(shí)例綁定一個(gè)屬性def set_age(self, age): # 定義一個(gè)函數(shù)作為實(shí)例方法 self.age = agefrom types import MethodTypes.set_age = MethodType(set_age, s) # 給實(shí)例綁定一個(gè)方法 類的其他實(shí)例不受此影響s.set_age(25) # 調(diào)用實(shí)例方法Student.set_age = MethodType(set_age, Student) # 為類綁定一個(gè)方法 類的所有實(shí)例都擁有該方法
七:類的高級(jí)話題
多重繼承: “混合類”, 搜索方式”從下到上 從左到右 廣度優(yōu)先”
class A(B, C): pass
– 7.1 類的繼承和子類的初始化
# 1.子類定義了__init__方法時(shí),若未顯示調(diào)用基類__init__方法,python不會(huì)幫你調(diào)用。# 2.子類未定義__init__方法時(shí),python會(huì)自動(dòng)幫你調(diào)用首個(gè)基類的__init__方法,注意是首個(gè)。# 3.子類顯示調(diào)用基類的初始化函數(shù):class FooParent(object): def __init__(self, a): self.parent = 'I\'m the Parent.' print('Parent:a=' + str(a)) def bar(self, message): print(message + ' from Parent')class FooChild(FooParent): def __init__(self, a): FooParent.__init__(self, a) print('Child:a=' + str(a)) def bar(self, message): FooParent.bar(self, message) print(message + ' from Child')fooChild = FooChild(10)fooChild.bar('HelloWorld')
– 7.2 實(shí)例方法 / 靜態(tài)方法 / 類方法
class Methods: def imeth(self, x): print(self, x) # 實(shí)例方法:傳入的是實(shí)例和數(shù)據(jù),操作的是實(shí)例的屬性 def smeth(x): print(x) # 靜態(tài)方法:只傳入數(shù)據(jù) 不傳入實(shí)例,操作的是類的屬性而不是實(shí)例的屬性 def cmeth(cls, x): print(cls, x) # 類方法:傳入的是類對(duì)象和數(shù)據(jù) smeth = staticmethod(smeth) # 調(diào)用內(nèi)置函數(shù),也可以使用@staticmethod cmeth = classmethod(cmeth) # 調(diào)用內(nèi)置函數(shù),也可以使用@classmethodobj = Methods()obj.imeth(1) # 實(shí)例方法調(diào)用 <__main__.methods object...=""> 1Methods.imeth(obj, 2) # <__main__.methods object...=""> 2Methods.smeth(3) # 靜態(tài)方法調(diào)用 3obj.smeth(4) # 這里可以使用實(shí)例進(jìn)行調(diào)用Methods.cmeth(5) # 類方法調(diào)用 5obj.cmeth(6) # 6
函數(shù)裝飾器:是它后邊的函數(shù)的運(yùn)行時(shí)的聲明 由@符號(hào)以及后邊緊跟的”元函數(shù)”(metafunction)組成
@staticmethod def smeth(x): print(x)# 等同于: def smeth(x): print(x) smeth = staticmethod(smeth)# 同理 @classmethod def cmeth(cls, x): print(x)# 等同于 def cmeth(cls, x): print(x) cmeth = classmethod(cmeth)
類修飾器:是它后邊的類的運(yùn)行時(shí)的聲明 由@符號(hào)以及后邊緊跟的”元函數(shù)”(metafunction)組成
def decorator(aClass):..... @decorator class C:....# 等同于: class C:.... C = decorator(C)
– 7.3 限制class屬性: __slots__屬性
class Student: __slots__ = ('name', 'age') # 限制Student及其實(shí)例只能擁有name和age屬性# __slots__屬性只對(duì)當(dāng)前類起作用, 對(duì)其子類不起作用# __slots__屬性能夠節(jié)省內(nèi)存# __slots__屬性可以為列表list,或者元組tuple
– 7.4 類屬性高級(jí)話題: @property
# 假設(shè)定義了一個(gè)類:C,該類必須繼承自object類,有一私有變量_xclass C(object): def __init__(self): self.__x = None# 第一種使用屬性的方法 def getx(self): return self.__x def setx(self, value): self.__x = value def delx(self): del self.__x x = property(getx, setx, delx, '')# property函數(shù)原型為property(fget=None,fset=None,fdel=None,doc=None)# 使用c = C()c.x = 100 # 自動(dòng)調(diào)用setx方法y = c.x # 自動(dòng)調(diào)用getx方法del c.x # 自動(dòng)調(diào)用delx方法# 第二種方法使用屬性的方法 @property def x(self): return self.__x @x.setter def x(self, value): self.__x = value @x.deleter def x(self): del self.__x# 使用c = C()c.x = 100 # 自動(dòng)調(diào)用setter方法y = c.x # 自動(dòng)調(diào)用x方法del c.x # 自動(dòng)調(diào)用deleter方法
– 7.5 定制類: 重寫類的方法
# (1)__str__方法、__repr__方法: 定制類的輸出字符串# (2)__iter__方法、next方法: 定制類的可迭代性class Fib(object): def __init__(self): self.a, self.b = 0, 1 # 初始化兩個(gè)計(jì)數(shù)器a,b def __iter__(self): return self # 實(shí)例本身就是迭代對(duì)象,故返回自己 def next(self): self.a, self.b = self.b, self.a + self.b if self.a > 100000: # 退出循環(huán)的條件 raise StopIteration() return self.a # 返回下一個(gè)值for n in Fib(): print(n) # 使用# (3)__getitem__方法、__setitem__方法: 定制類的下標(biāo)操作[] 或者切片操作sliceclass Indexer(object): def __init__(self): self.data = {} def __getitem__(self, n): # 定義getitem方法 print('getitem:', n) return self.data[n] def __setitem__(self, key, value): # 定義setitem方法 print('setitem:key = {0}, value = {1}'.format(key, value)) self.data[key] = valuetest = Indexer()test[0] = 1; test[3] = '3' # 調(diào)用setitem方法print(test[0]) # 調(diào)用getitem方法# (4)__getattr__方法: 定制類的屬性操作class Student(object): def __getattr__(self, attr): # 定義當(dāng)獲取類的屬性時(shí)的返回值 if attr=='age': return 25 # 當(dāng)獲取age屬性時(shí)返回25 raise AttributeError('object has no attribute: %s' % attr) # 注意: 只有當(dāng)屬性不存在時(shí) 才會(huì)調(diào)用該方法 且該方法默認(rèn)返回None 需要在函數(shù)最后引發(fā)異常s = Student()s.age # s中age屬性不存在 故調(diào)用__getattr__方法 返回25# (5)__call__方法: 定制類的'可調(diào)用'性class Student(object): def __call__(self): # 也可以帶參數(shù) print('Calling......')s = Student()s() # s變成了可調(diào)用的 也可以帶參數(shù)callable(s) # 測(cè)試s的可調(diào)用性 返回True# (6)__len__方法:求類的長(zhǎng)度def __len__(self): return len(self.data)
– 7.6 動(dòng)態(tài)創(chuàng)建類type()
# 一般創(chuàng)建類 需要在代碼中提前定義 class Hello(object): def hello(self, name='world'): print('Hello, %s.' % name) h = Hello() h.hello() # Hello, world type(Hello) # Hello是一個(gè)type類型 返回 type(h) # h是一個(gè)Hello類型 返回# 動(dòng)態(tài)類型語言中 類可以動(dòng)態(tài)創(chuàng)建 type函數(shù)可用于創(chuàng)建新類型 def fn(self, name='world'): # 先定義函數(shù) print('Hello, %s.' % name) Hello = type('Hello', (object,), dict(hello=fn)) # 創(chuàng)建Hello類 type原型: type(name, bases, dict) h = Hello() # 此時(shí)的h和上邊的h一致
八:異常相關(guān)
– 8.1 捕獲異常:
try: except: # 捕獲所有的異常 等同于except Exception: except name: # 捕獲指定的異常 except name, value: # 捕獲指定的異常和額外的數(shù)據(jù)(實(shí)例) except (name1, name2): except (name1, name2), value: except name4 as X: else: # 如果沒有發(fā)生異常 finally: # 總會(huì)執(zhí)行的部分# 引發(fā)異常: raise子句(raise IndexError) raise # raise instance of a class, raise IndexError() raise # make and raise instance of a class, raise IndexError raise # reraise the most recent exception
– 8.2 Python3.x中的異常鏈: raise exception from otherException
except Exception as X: raise IndexError('Bad') from X
assert子句: assert < test="">, < data="">
assert x < 0,="" ‘x="" must="" be="">
– 8.3 with/as環(huán)境管理器:作為常見的try/finally用法模式的替代方案
with expression [as variable], expression [as variable]:# 例子: with open('test.txt') as myfile: for line in myfile: print(line)# 等同于: myfile = open('test.txt') try: for line in myfile: print(line) finally: myfile.close()
– 8.4 用戶自定義異常: class Bad(Exception):…..
'''Exception超類 / except基類即可捕獲到其所有子類Exception超類有默認(rèn)的打印消息和狀態(tài) 當(dāng)然也可以定制打印顯示:'''class MyBad(Exception): def __str__(self): return '定制的打印消息'try: MyBad()except MyBad as x: print(x)
– 8.5 用戶定制異常數(shù)據(jù)
class FormatError(Exception): def __init__(self, line ,file): self.line = line self.file = filetry: raise FormatError(42, 'test.py')except FormatError as X: print('Error at ', X.file, X.line)# 用戶定制異常行為(方法):以記錄日志為例class FormatError(Exception): logfile = 'formaterror.txt' def __init__(self, line ,file): self.line = line self.file = file def logger(self): open(self.logfile, 'a').write('Error at ', self.file, self.line)try: raise FormatError(42, 'test.py')except FormatError as X: X.logger()
– 8.6 關(guān)于sys.exc_info:允許一個(gè)異常處理器獲取對(duì)最近引發(fā)的異常的訪問
try: ......except: # 此時(shí)sys.exc_info()返回一個(gè)元組(type, value, traceback) # type:正在處理的異常的異常類型 # value:引發(fā)的異常的實(shí)例 # traceback:堆棧信息
– 8.7 異常層次
BaseException+-- SystemExit+-- KeyboardInterrupt+-- GeneratorExit+-- Exception +-- StopIteration +-- ArithmeticError +-- AssertionError +-- AttributeError +-- BufferError +-- EOFError +-- ImportError +-- LookupError +-- MemoryError +-- NameError +-- OSError +-- ReferenceError +-- RuntimeError +-- SyntaxError +-- SystemError +-- TypeError +-- ValueError +-- Warning
九:Unicode和字節(jié)字符串
– 9.1 Python的字符串類型
'''Python2.x'''# 1.str表示8位文本和二進(jìn)制數(shù)據(jù)# 2.unicode表示寬字符Unicode文本'''Python3.x'''# 1.str表示Unicode文本(8位或者更寬)# 2.bytes表示不可變的二進(jìn)制數(shù)據(jù)# 3.bytearray是一種可變的bytes類型
– 9.2 字符編碼方法
'''ASCII''' # 一個(gè)字節(jié),只包含英文字符,0到127,共128個(gè)字符,利用函數(shù)可以進(jìn)行字符和數(shù)字的相互轉(zhuǎn)換ord('a') # 字符a的ASCII碼為97,所以這里返回97chr(97) # 和上邊的過程相反,返回字符'a''''Latin-1''' # 一個(gè)字節(jié),包含特殊字符,0到255,共256個(gè)字符,相當(dāng)于對(duì)ASCII碼的擴(kuò)展chr(196) # 返回一個(gè)特殊字符:?'''Unicode''' # 寬字符,一個(gè)字符包含多個(gè)字節(jié),一般用于亞洲的字符集,比如中文有好幾萬字'''UTF-8''' # 可變字節(jié)數(shù),小于128的字符表示為單個(gè)字節(jié),128到0X7FF之間的代碼轉(zhuǎn)換為兩個(gè)字節(jié),0X7FF以上的代碼轉(zhuǎn)換為3或4個(gè)字節(jié)# 注意:可以看出來,ASCII碼是Latin-1和UTF-8的一個(gè)子集# 注意:utf-8是unicode的一種實(shí)現(xiàn)方式,unicode、gbk、gb2312是編碼字符集
– 9.3 查看Python中的字符串編碼名稱,查看系統(tǒng)的編碼
import encodingshelp(encoding)import syssys.platform # 'win64'sys.getdefaultencoding() # 'utf-8'sys.getdefaultencoding() # 返回當(dāng)前系統(tǒng)平臺(tái)的編碼類型sys.getsizeof(object) # 返回object占有的bytes的大小
源文件字符集編碼聲明: 添加注釋來指定想要的編碼形式 從而改變默認(rèn)值 注釋必須出現(xiàn)在腳本的第一行或者第二行
'''說明:其實(shí)這里只會(huì)檢查#和coding:utf-8,其余的字符都是為了美觀加上的'''# _*_ coding: utf-8 _*_# coding = utf-8
編碼: 字符串 –> 原始字節(jié) #解碼: 原始字節(jié) –> 字符串
– 9.4 Python3.x中的字符串應(yīng)用
s = '...' # 構(gòu)建一個(gè)str對(duì)象,不可變對(duì)象b = b'...' # 構(gòu)建一個(gè)bytes對(duì)象,不可變對(duì)象s[0], b[0] # 返回('.', 113)s[1:], b[1:] # 返回('..', b'..')B = B''' xxxx yyyy '''# B = b'\nxxxx\nyyyy\n'# 編碼,將str字符串轉(zhuǎn)化為其raw bytes形式: str.encode(encoding = 'utf-8', errors = 'strict') bytes(str, encoding)# 編碼例子: S = 'egg' S.encode() # b'egg' bytes(S, encoding = 'ascii') # b'egg'# 解碼,將raw bytes字符串轉(zhuǎn)化為str形式: bytes.decode(encoding = 'utf-8', errors = 'strict') str(bytes_or_buffer[, encoding[, errors]])# 解碼例子: B = b'spam' B.decode() # 'spam' str(B) # 'b'spam'',不帶編碼的str調(diào)用,結(jié)果為打印該bytes對(duì)象 str(B, encoding = 'ascii')# 'spam',帶編碼的str調(diào)用,結(jié)果為轉(zhuǎn)化該bytes對(duì)象
– 9.5 Python2.x的編碼問題
u = u'漢'print repr(u) # u'\xba\xba's = u.encode('UTF-8')print repr(s) # '\xc2\xba\xc2\xba'u2 = s.decode('UTF-8')print repr(u2) # u'\xba\xba'# 對(duì)unicode進(jìn)行解碼是錯(cuò)誤的s2 = u.decode('UTF-8') # UnicodeEncodeError: 'ascii' codec can't encode characters in position 0-1: ordinal not in range(128)# 同樣,對(duì)str進(jìn)行編碼也是錯(cuò)誤的u2 = s.encode('UTF-8') # UnicodeDecodeError: 'ascii' codec can't decode byte 0xc2 in position 0: ordinal not in range(128)
– 9.6 bytes對(duì)象
B = b'abc'B = bytes('abc', 'ascii')B = bytes([97, 98, 99])B = 'abc'.encode()# bytes對(duì)象的方法調(diào)用基本和str類型一致 但:B[0]返回的是ASCII碼值97, 而不是b'a'
文本文件: 根據(jù)Unicode編碼來解釋文件內(nèi)容,要么是平臺(tái)的默認(rèn)編碼,要么是指定的編碼類型
二進(jìn)制文件:表示字節(jié)值的整數(shù)的一個(gè)序列 open(‘bin.txt’, ‘rb’)
– 9.7 Unicode文件
s = 'A\xc4B\xe8C' # s = 'A?BèC' len(s) = 5#手動(dòng)編碼 l = s.encode('latin-1') # l = b'A\xc4B\xe8C' len(l) = 5 u = s.encode('utf-8') # u = b'A\xc3\x84B\xc3\xa8C' len(u) = 7#文件輸出編碼 open('latindata', 'w', encoding = 'latin-1').write(s) l = open('latindata', 'rb').read() # l = b'A\xc4B\xe8C' len(l) = 5 open('uft8data', 'w', encoding = 'utf-8').write(s) u = open('uft8data', 'rb').read() # u = b'A\xc3\x84B\xc3\xa8C' len(u) = 7#文件輸入編碼 s = open('latindata', 'r', encoding = 'latin-1').read() # s = 'A?BèC' len(s) = 5 s = open('latindata', 'rb').read().decode('latin-1') # s = 'A?BèC' len(s) = 5 s = open('utf8data', 'r', encoding = 'utf-8').read() # s = 'A?BèC' len(s) = 5 s = open('utf8data', 'rb').read().decode('utf-8') # s = 'A?BèC' len(s) = 5
十:其他
– 10.1 60個(gè)字符解決FizzBuzz:
'''寫一個(gè)程序, 打印數(shù)字1到100, 3的倍數(shù)打印“Fizz”來替換這個(gè)數(shù), 5的倍數(shù)打印“Buzz”, 既是3又是5的倍數(shù)的打印“FizzBuzz”'''for x in range(101): print('fizz'[x%3*4::]+'buzz'[x%5*4::] or x) # 解釋:最主要用到列表(字符串)的子表
– 10.2 Python實(shí)現(xiàn)任意深度的賦值
例如a[0] = ‘value1’; a[1][2] = ‘value2’; a[3][4][5] = ‘value3’
class MyDict(dict): def __setitem__(self, key, value): # 該函數(shù)不做任何改動(dòng) 這里只是為了輸出 print('setitem:', key, value, self) super().__setitem__(key, value) def __getitem__(self, item): # 主要技巧在該函數(shù) print('getitem:', item, self) # 輸出信息 # 基本思路: a[1][2]賦值時(shí) 需要先取出a[1] 然后給a[1]的[2]賦值 if item not in self: # 如果a[1]不存在 則需要新建一個(gè)dict 并使得a[1] = dict temp = MyDict() # 新建的dict: temp super().__setitem__(item, temp) # 賦值a[1] = temp return temp # 返回temp 使得temp[2] = value有效 return super().__getitem__(item) # 如果a[1]存在 則直接返回a[1]# 例子: test = MyDict() test[0] = 'test' print(test[0]) test[1][2] = 'test1' print(test[1][2]) test[1][3] = 'test2' print(test[1][3])
– 10.3 Python中的多維數(shù)組
lists = [0] * 3 # 擴(kuò)展list,結(jié)果為[0, 0, 0]lists = [[]] * 3 # 多維數(shù)組,結(jié)果為[[], [], []],但有問題,往下看lists[0].append(3) # 期望看到的結(jié)果[[3], [], []],實(shí)際結(jié)果[[3], [3], [3]],原因:list*n操作,是淺拷貝,如何避免?往下看lists = [[] for i in range(3)] # 多維數(shù)組,結(jié)果為[[], [], []]lists[0].append(3) # 結(jié)果為[[3], [], []]lists[1].append(6) # 結(jié)果為[[3], [6], []]lists[2].append(9) # 結(jié)果為[[3], [6], [9]]lists = [[[] for j in range(4)] for i in range(3)]
lists # 3行4列,且每一個(gè)元素為[]