文章目錄
- 前言
- 一、程式、模塊和包
- 1.自定義模塊和包
- 2.Python標準庫
- 二、檔案IO
- 1.檔案輸入輸出基本介紹
- 2.讀寫文本檔案
- 3.讀寫二進制檔案
- 4.使用with自動關閉檔案
- 5.獲取和改變位置
- 6.結構化文本檔案
- 三、面向物件
- 1.用class定義類
- 2.繼承
- 3.子父類呼叫
- 4.鴨子型別
- 5.特殊方法
- 總結
前言
本文先介紹了Python中程式、模塊和包的基本使用,并在此基礎上介紹了Python標準庫,然后詳細介紹了Python中的檔案IO操作,包括文本檔案、二進制檔案的讀寫和其他IO操作,最后介紹了面向物件,包括類的定義、繼承的使用、鴨子型別和魔法方法,

一、程式、模塊和包
1.自定義模塊和包
之前我們使用的.ipynb檔案都不是純Python檔案,純Python檔案應該是.py檔案,
一個純Python檔案,如report.py可能如下:
def get_description():
"""Return random weather, just like the pros"""
from random import choice
possibilities = ['rain', 'snow', 'sleet', 'fog', 'sun', 'who knows']
return choice(possibilities)
def get_desc():
"""Return random weather, just like the pros"""
from random import choice
possibilities = ['rain', 'snow', 'sleet', 'fog', 'sun', 'who knows']
return choice(possibilities)
這個代碼中包含了兩個函式,
如需本節同步
ipynb檔案和Python檔案,可以直接點擊加QQ群963624318 在群檔案夾商業資料分析從入門到入職中下載即可,
.ipynb檔案也可以另存為.py檔案,依次選擇檔案 → 下載 → Python(.py)即可,就會保存為與.ipynb檔案同名的.py檔案,
可以在Python IDE如PyCharm中運行Python檔案,也可以在命令列中運行,假如你的Python檔案在E:\Test目錄下,名為test.py,則可以先在命令列中通過命令cd E:\Test切換到Python檔案所在目錄,然后再執行python test.py即可運行當前檔案,
可以在其他程式中直接使用自己定義的Python代碼,
例如可以在當前的.ipynb中使用之前的report.py中的get_description()函式(需要保證.ipynb檔案與report.py位于同級目錄),如下:
import report
report.get_description()
輸出:
'rain'
可以看到,正常執行,
之前我們可能已經遇到過類似如下的情況:
import math
math.sqrt(2)
通過import關鍵字匯入math,然后就可以使用它進行各種操作了,
但是并不是可以任意匯入的,如下:
import corley
此時報錯:
---------------------------------------------------------------------------
ModuleNotFoundError Traceback (most recent call last)
<ipython-input-2-fb30571dbbc8> in <module>
----> 1 import corley
ModuleNotFoundError: No module named 'corley'
即提示沒有名為“corley”的模塊,
但是math不在當前目錄,也能正常使用,這是因為當前Python的模塊搜索路徑還包括其他路徑,可以通過以下代碼查看當前的模塊搜索路徑:
import sys
sys.path
輸出:
['XXX',
'E:\\Anaconda3\\python38.zip',
'E:\\Anaconda3\\DLLs',
'E:\\Anaconda3\\lib',
'E:\\Anaconda3',
'',
'E:\\Anaconda3\\lib\\site-packages',
'E:\\Anaconda3\\lib\\site-packages\\win32',
'E:\\Anaconda3\\lib\\site-packages\\win32\\lib',
'E:\\Anaconda3\\lib\\site-packages\\Pythonwin',
'E:\\Anaconda3\\lib\\site-packages\\IPython\\extensions',
'C:\\Users\\LENOVO\\.ipython']
除了第一個目錄是檔期那目錄,其他目錄都是Anaconda的相關路徑,math模塊可能就在某個路徑下,
某些模塊可能有長名稱,在這種情況下,可以在匯入時重命名模塊,這樣可以節省一些時間,
如下:
import report as rp
rp.get_description()
輸出:
'sun'
可以看到,能達到同樣的效果,
有些模塊中可能有大量的定義,如果不需要全部匯入,則可以從中匯入需要使用的內容,
如下:
from report import get_description
get_description()
輸出:
'fog'
也可以進行命名,如下:
from report import get_description as get_weather
get_weather()
輸出:
'who knows'
從一行代碼到多行函式,再到獨立程式,再到同一目錄中的多個模塊,為了使Python應用程式更具規模,可以將模塊組織成稱為包的檔案層次結構,
例如,當前目錄下有一個子目錄為source,下有兩個檔案,daily.py如下:
def forecast():
'fake daily forecast'
return 'like yesterday'
weekly.py如下:
def forecast():
"""Fake weekly forecast"""
return ['snow', 'more snow', 'sleet', 'freezing rain', 'rain', 'fog', 'hail']
此時可以從包source中的模塊daily和weekly中匯入forecast,
如下:
from source import daily, weekly
print('Daily forecast:', daily.forecast())
print('Weekly forecast:', weekly.forecast())
輸出:
Daily forecast: like yesterday
Weekly forecast: ['snow', 'more snow', 'sleet', 'freezing rain', 'rain', 'fog', 'hail']
從Python3.3開始引入了隱式名稱空間包,它允許我們創建一個不帶__init__.py檔案的包,
2.Python標準庫
Python的一個突出的宣告是它有“batteris included”——一個大型的標準模塊庫,它執行許多有用的任務,并被分開保存以避免核心語言膨脹,
不時地對Python的標準庫進行一些探索總是很有幫助的,
Python的標準庫非常廣泛,提供了廣泛的工具,如https://docs.python.org/3/library/列出的長目錄所示,該庫包含內置模塊(用C撰寫),這些模塊提供對系統功能(如Python程式員無法訪問的檔案I/O)的訪問,以及用Python撰寫的模塊,它們為日常編程中出現的許多問題提供標準化解決方案,其中一些模塊被明確設計為鼓勵和增強Python程式的可移植性,方法是將平臺細節抽象到平臺無關的api中,
用于Windows平臺的Python安裝程式通常包括整個標準庫,并且通常還包括許多附加組件,對于類Unix的作業系統,Python通常是作為包的集合提供的,因此可能需要使用作業系統提供的打包工具來獲取部分或全部可選組件,
例如datetime庫的簡單使用如下:
import datetime
dt = datetime.date(2020, 9, 28)
dt.weekday()
輸出:
0
如果匯入一個庫報錯ModuleNotFoundError,那說明這個庫不是Python標準庫,而是第三方庫,需要下載安裝之后才能匯入,
安裝第三方庫可以使用conda或pip命令:
如安裝jieba庫(中文分詞庫)則可以執行conda install jieba或pip install jieba來安裝這個庫,也可以到pip官網https://pypi.org/project/pip/查找所需要的庫并下載安裝,
二、檔案IO
1.檔案輸入輸出基本介紹
當程式運行時,所有生成的資料都存盤在RAM中,RAM速度快,但有兩個限制:
- 昂貴(因此容量小)
- 需要恒定電源
磁盤驅動器比RAM慢,但更便宜,并且更重要的是,即使斷電也能保存資料,為了保持資料的持久性,我們需要將其作為檔案存盤在磁盤驅動程式中,
簡單的持久性是一種最簡單的平面檔案,它只是存盤在檔案名下的位元組序列,可以將檔案讀入記憶體并從記憶體寫入檔案(在磁盤驅動程式上),
在讀或寫檔案之前,必須先打開它:
fileobj = open(filename, mode)
mode是一個字串,指示檔案的型別以及要對其執行的操作:
mode的第一個字母表示操作:
| 字符 | 含義 |
|---|---|
| r | 讀 |
| w | 寫入(如果檔案不存在,創建一個;如果檔案存在,則重寫它) |
| x | 寫入(僅當檔案不存在時) |
| a | 如果檔案存在,則追加(在結尾后寫入) |
mode的第二個字母表示檔案的型別:
| 字符 | 含義 |
|---|---|
| t(或無) | 純文本 |
| b | 二進制 |
查看open()函式如下:
?open
輸出:
Signature:
open(
file,
mode='r',
buffering=-1,
encoding=None,
errors=None,
newline=None,
closefd=True,
opener=None,
)
Docstring:
Open file and return a stream. Raise OSError upon failure.
file is either a text or byte string giving the name (and the path
if the file isn't in the current working directory) of the file to
be opened or an integer file descriptor of the file to be
wrapped. (If a file descriptor is given, it is closed when the
returned I/O object is closed, unless closefd is set to False.)
mode is an optional string that specifies the mode in which the file
is opened. It defaults to 'r' which means open for reading in text
mode. Other common values are 'w' for writing (truncating the file if
it already exists), 'x' for creating and writing to a new file, and
'a' for appending (which on some Unix systems, means that all writes
append to the end of the file regardless of the current seek position).
In text mode, if encoding is not specified the encoding used is platform
dependent: locale.getpreferredencoding(False) is called to get the
current locale encoding. (For reading and writing raw bytes use binary
mode and leave encoding unspecified.)
2.讀寫文本檔案
簡單使用如下:
text = '''\
First line
Second line
Third line
End
'''
print(len(text))
fout = open('new_file.txt', 'wt')
ret = fout.write(text)
print('return value of write() =', ret)
fout.close()
輸出:
38
return value of write() = 38
write()方法的回傳值為檔案的長度,
同時可以看到,在同級目錄下多了一個檔案即為new_file.txt,內容如下:
First line
Second line
Third line
End
再讀取檔案如下:
fin = open('new_file.txt', 'r')
lines = fin.readlines()
print(lines)
fin.close()
輸出:
['First line\n', 'Second line\n', 'Third line\n', 'End\n']
可以看到,將所有行讀出并存到串列中,
再追加內容,如下:
text = '''\
1
2
3
'''
print(len(text))
fout = open('new_file.txt', 'a')
ret = fout.write(text)
print('return value of write() =', ret)
fout.close()
輸出:
6
return value of write() = 6
再次查看new_file.txt,如下:
First line
Second line
Third line
End
1
2
3
可以看到,內容追加到之前的內容后面,
還可以通過print()函式實作將文本輸出到檔案中,如下:
text = '''\
hello
world
'''
fout = open('file.txt', 'w')
print(text, file=fout)
fout.close()
此時可以看到目錄中多了一個檔案為file.txt,內容為:
hello
world
這就是通過print()函式將內容輸出到檔案中,
Python有一個彩蛋,輸入:
import this
輸出:
The Zen of Python, by Tim Peters
Beautiful is better than ugly.
Explicit is better than implicit.
Simple is better than complex.
Complex is better than complicated.
Flat is better than nested.
Sparse is better than dense.
Readability counts.
Special cases aren't special enough to break the rules.
Although practicality beats purity.
Errors should never pass silently.
Unless explicitly silenced.
In the face of ambiguity, refuse the temptation to guess.
There should be one-- and preferably only one --obvious way to do it.
Although that way may not be obvious at first unless you're Dutch.
Now is better than never.
Although never is often better than *right* now.
If the implementation is hard to explain, it's a bad idea.
If the implementation is easy to explain, it may be a good idea.
Namespaces are one honking great idea -- let's do more of those!
這就是Python之禪,定義了Python的一些規范,
可以分段寫入檔案,避免檔案太大時導致的記憶體不足問題,
如下:
zen_of_py = '''\
The Zen of Python, by Tim Peters
Beautiful is better than ugly.
Explicit is better than implicit.
Simple is better than complex.
Complex is better than complicated.
Flat is better than nested.
Sparse is better than dense.
Readability counts.
Special cases aren't special enough to break the rules.
Although practicality beats purity.
Errors should never pass silently.
Unless explicitly silenced.
In the face of ambiguity, refuse the temptation to guess.
There should be one-- and preferably only one --obvious way to do it.
Although that way may not be obvious at first unless you're Dutch.
Now is better than never.
Although never is often better than *right* now.
If the implementation is hard to explain, it's a bad idea.
If the implementation is easy to explain, it may be a good idea.
Namespaces are one honking great idea -- let's do more of those!
'''
fout = open('zen_of_py.txt', 'w')
size = len(zen_of_py)
offset = 0
chunk = 100
while offset < size:
fout.write(zen_of_py[offset : offset+chunk])
offset += chunk
fout.close()
此時查看目錄,多了檔案為zen_of_py.txt;
這是通過分段的方式寫入檔案的,類似于從網上下載檔案分段下載,
讀檔案也能分段讀取,也可以避免檔案太大時記憶體不足,
如下:
zen_of_py = ''
fin = open('zen_of_py.txt', 'r')
chunk = 100
while True:
fragment = fin.read(chunk)
if not fragment:
break
zen_of_py += fragment
fin.close()
print(zen_of_py)
輸出:
The Zen of Python, by Tim Peters
Beautiful is better than ugly.
Explicit is better than implicit.
Simple is better than complex.
Complex is better than complicated.
Flat is better than nested.
Sparse is better than dense.
Readability counts.
Special cases aren't special enough to break the rules.
Although practicality beats purity.
Errors should never pass silently.
Unless explicitly silenced.
In the face of ambiguity, refuse the temptation to guess.
There should be one-- and preferably only one --obvious way to do it.
Although that way may not be obvious at first unless you're Dutch.
Now is better than never.
Although never is often better than *right* now.
If the implementation is hard to explain, it's a bad idea.
If the implementation is easy to explain, it may be a good idea.
Namespaces are one honking great idea -- let's do more of those!
也可以逐行讀取檔案,
如下:
fin = open('zen_of_py.txt', 'r')
while True:
line = fin.readline()
if not line:
break
print(line, end='')
fin.close()
輸出與前面相同,
3.讀寫二進制檔案
生成二進制資料如下:
bdata = bytes(range(256))
print(len(bdata))
print(bdata[0])
print(bdata[255])
寫入二進制位元組如下:
fout = open('bfile', 'wb')
ret = fout.write(bdata)
fout.close()
print('Write %d bytes' % ret)
輸出:
Write 256 bytes
圖片、音頻、視頻等檔案都屬于二進制檔案,
也可以分段寫入:
fout = open('bfile', 'wb')
size = len(bdata)
offset = 0
chunk = 100
while offset < size:
ret = fout.write(bdata[offset : offset+chunk])
print(ret)
offset += chunk
fout.close()
輸出:
100
100
56
讀取二進制檔案如下:
fin = open('bfile', 'rb')
bdata = fin.read()
fin.close()
len(bdata)
輸出:
256
4.使用with自動關閉檔案
如果打開了一個檔案卻忘了關閉它,當跳出打開檔案的范圍時,Python將關閉它,但是,更安全的方法是使用with關鍵字,
如下:
with open('tmp1', 'w') as fout:
fout.write(text)
此時在當前目錄下會生成檔案tmp1,
5.獲取和改變位置
讀寫時,Python會跟蹤在檔案中的當前位置,tell()回傳檔案開頭的當前偏移量(位元組),seek()跳轉到檔案中的另一個位元組偏移量,
對文本檔案的用法如下:
fin = open('tmp1', 'r')
print('starting pos =', fin.tell())
line = fin.readline()
print('# char read =', len(line))
print('ending pos =', fin.tell())
fin.close()
輸出:
starting pos = 0
# char read = 6
ending pos = 7
這只適用于ASCII編碼的文本檔案,其中每個字符都存盤在一個位元組中,像UTF-8這樣的編碼可以使用每個字符不同數量的位元組,
對二進制檔案的操作如下:
fin = open('bfile', 'rb')
fin.seek(255)
bdata = fin.read()
print(len(bdata))
print(bdata[0])
輸出:
1
255
seek()方法也可以使用第二個引數,即seek(offset, origin):
如果origin為0(默認值),則從開始處偏移位元組;
如果origin為1,則從當前位置偏移;
如果origin為2,則相對于終點偏移位元組(即偏移量必須為負),
origin不是關鍵字引數,
如下:
fin = open('bfile', 'rb')
fin.seek(-1, 2)
bdata = fin.read()
print(len(bdata))
print(bdata[0])
輸出:
1
255
6.結構化文本檔案
對于純文本檔案,唯一的組織級別是行(用換行符分隔),有時可能想要一個更豐富的結構,一種方法是引入額外的分隔符,
常見的形式如下:
- Comma-Separated Values (CSV):
\t、,、| - HTML & XML:
<、> - JSON:
{、}、[、]
寫入csv如下:
import csv
dc_heros = [
['Flash', 'Barry Allen'],
['Green Arrow', 'Oliver Queen'],
['Atom', 'Ray Palmer'],
['Bat Man', 'Bruce Wayne']
]
with open('dc_heros.csv', 'w') as fout:
csvout = csv.writer(fout)
csvout.writerows(dc_heros)
執行后,可以看到目錄中生成了dc_heros.csv檔案,
讀csv檔案如下:
with open('dc_heros.csv', 'r') as fin:
csvin = csv.reader(fin)
dc_heros = [row for row in csvin]
print(dc_heros)
輸出:
[['Flash', 'Barry Allen'], [], ['Green Arrow', 'Oliver Queen'], [], ['Atom', 'Ray Palmer'], [], ['Bat Man', 'Bruce Wayne'], []]
三、面向物件
Object Oriented Programming即面向物件程式設計,
Python中一切皆是物件,從數字到模塊,
然而,Python通過特殊語法的mena隱藏了大部分物件機制,例如可以直接輸入num=7來創建一個值為7的integer型別的物件,并為名稱num指定一個物件參考,
唯一需要查看物件內部的時間是想要創建自己的物件或修改現有物件的行為時,
物件包含:
- 資料(變數,稱為屬性)
- 代碼(函式,稱為方法)
它代表了一個特殊的例子:
把物體看作名詞,把它們的方法看作動詞,
1.用class定義類
如果一個物件像一個盒子,那么一個類就像制造盒子的模具,
定義一個空物件如下:
class Person():
pass
someone = Person()
type(someone)
輸出:
__main__.Person
可以看到,定義了一個叫Person的類,并通過這個類實體化了一個實體,即someone物件,
生成一個整型物件,如下:
a = int(2)
type(a)
輸出:
int
可以在生成物件即初始化時就給物件一些特征,此時可以給類定義__init__()方法即初始化方法,
如下:
class Person():
def __init__(self, name, gender): # The first parameter has to be self
self.name = name
self.gender = gender
ed = Person('Edward', 'Male')
可以看到,__init__()方法中傳遞了self引數,這是在類中定義實體方法必須要帶的引數,代表的是個體物件本身,
創建物件的大概程序如下:
(1)查找Person類的定義;
(2)在記憶體中創建新物件;
(3)呼叫__init__()方法,將新創建的物件作為self傳遞,其他物件作為name和gender傳遞;
(4)在物件中存盤name和gender的值;
(5)回傳新物件;
(6)將創建的物件賦值給ed,
此時可以獲取到創建出來的物件的屬性,如下:
print('Name:', ed.name)
print('Gender:', ed.gender)
輸出:
Name: Edward
Gender: Male
還可以修改物件的屬性,如下:
ed.name = 'Corley'
ed.name
輸出:
'Corley'
還可以在類中定義方法,
如下:
class Person():
def __init__(self, name, gender): # The first parameter has to be self
self.name = name
self.gender = gender
def say(self):
print("Hi I'm " + self.name + ", it's nice to meet you!")
ed = Person('Corley', 'Male')
ed.say()
輸出:
Hi I'm Corley, it's nice to meet you!
2.繼承
還可以從現有類中創建一個新類,但需要添加或更改,這就是繼承,
定義一個繼承自Person類的類:
class MDPerson(Person):
pass
ed = MDPerson("Corley", 'Male')
ed.say()
輸出:
Hi I'm Corley, it's nice to meet you!
可以看到,MDPerson類繼承自Person類,雖然其內部沒有實作其他代碼,但是繼承了父類中的全部特性,因此可以進行初始化和呼叫函式;
其中,MDPerson類稱為子類,Person類稱為父類,
子類還可以擴展新特性,
如下:
class MDPerson(Person):
def diagnose(self):
print('You need some treatment.')
ed = MDPerson("Corley", 'Male')
ed.diagnose()
輸出:
You need some treatment.
但是父類中不能呼叫子類中新實作的特性,
如下:
someone = Person('Someone', 'NA')
someone.diagnose()
會報錯:
----------------------------------------------------------------
AttributeError Traceback (most recent call last)
<ipython-input-11-e646e6a12c4e> in <module>
1 someone = Person('Someone', 'NA')
----> 2 someone.diagnose()
AttributeError: 'Person' object has no attribute 'diagnose'
子類中可以重新定義父類中已經定義過的方法,稱之為重寫,
如下:
class MDPerson(Person):
def __init__(self, name, gender, dept='Cardiac Surgery'):
self.name = 'Doctor ' + name
self.gender = gender
self.dept = dept
def say(self):
print("Hi I'm %s from %s department, how can I help you" % (self.name, self.dept))
ed = MDPerson("Corley", 'Male')
ed.say()
輸出:
Hi I'm Doctor Corley from Cardiac Surgery department, how can I help you
顯然,此時與父類中的方法不同,
可以通過super()繼承來自父類的特性,從而同時實作付類和子類的特性,
如下:
class MDPerson(Person):
def __init__(self, name, gender, dept='Cardiac Surgery'):
super().__init__(name, gender)
self.name = 'Doctor ' + self.name
self.dept = dept
def say(self):
super().say()
print("Hi I'm %s from %s department, how can I help you" % (self.name, self.dept))
ed = MDPerson("Corley", 'Male')
ed.say()
輸出:
Hi I'm Doctor Corley, it's nice to meet you!
Hi I'm Doctor Corley from Cardiac Surgery department, how can I help you
如果Person的定義將來發生更改,那么使用super()將確保MDPerson從Person繼承的屬性和方法將反映更改,
3.子父類呼叫
Python使用self引數來查找正確物件的屬性和方法,
如下:
ed = Person('Corley', 'Male')
ed.say()
Person.say(ed)
輸出:
Hi I'm Corley, it's nice to meet you!
Hi I'm Corley, it's nice to meet you!
可以看到,兩種呼叫方式的效果相同,這是因為類中實作的實體方法本來就有一個引數self,代表物件自己,如果傳一個引數,只要這個引數是物件,也是能夠正常執行的,
背后執行的邏輯如下:
(1)查找物件ed的類(Person);
(2)將物件ed作為self引數傳遞給Person類的say()方法,
再如:
ed = MDPerson('Corley', 'Male')
print(ed.name)
Person.say(ed)
輸出:
Doctor Corley
Hi I'm Doctor Corley, it's nice to meet you!
可以看到,先使用MDPerson類進行初始化,初始化后ed物件的name屬性為Doctor Corley,其父類Person再呼叫say()方法,并將ed作為物件傳遞進去,因此列印出的不是Hi I'm Corley, it's nice to meet you!,也不是Hi I'm Doctor Corley from Cardiac Surgery department, how can I help you,而是Hi I'm Doctor Corley, it's nice to meet you!,
4.鴨子型別
Python對多型性有一個松散的實作,這意味著它對不同的物件應用相同的操作,而不管它們是什么類,
同樣,這也是EAFP設計模式的一部分,Python還會假設一個物件有這樣的方法,并嘗試呼叫它,如果沒有找到方法,它將拋出例外,
如下:
class Quote():
def __init__(self, person, words):
self.person = person
self.words = words
def who(self):
return self.person
def says(self):
return self.words + '.'
class QuestionQuote(Quote):
def says(self):
return self.words + '?'
class ExclamationQuote(Quote):
def says(self):
return self.words + '!'
def who_says(obj):
print(obj.who(), 'says:', obj.says())
q1 = Quote('Edward', 'Normal quote')
q2 = QuestionQuote('Corley', 'Question quote')
q3 = ExclamationQuote('Jack', 'Exclamation quote')
quotes = [q1, q2, q3]
for q in quotes:
who_says(q)
輸出:
Edward says: Normal quote.
Corley says: Question quote?
Jack says: Exclamation quote!
再定義一個與前面的3個類無關系的類,如下:
class Ed():
def who(self):
return 'Ed'
def says(self):
return "Hi I'm Edward :)"
ed = Ed()
who_says(ed)
輸出:
Ed says: Hi I'm Edward :)
與前面的類混合使用:
quotes = [q1, q2, q3, ed]
for q in quotes:
who_says(q)
Ed.says(q3)
輸出:
Edward says: Normal quote.
Corley says: Question quote?
Jack says: Exclamation quote!
Ed says: Hi I'm Edward :)
"Hi I'm Edward :)"
可以看到,即便Ed類和ExclamationQuote類毫無關系,但是ExclamationQuote物件還是可以作為引數傳遞到Education類的say()方法中,
如果它像鴨子一樣走路,像鴨子一樣嘎嘎叫,那它就是鴨子,這也就是多型,使用起來比其它語言更加靈活,沒有類繼承等方面的嚴格限制,
5.特殊方法
當輸入像a = 3 + 8這樣的代碼時,可能想知道整數物件是如何知道如何實作+運算的,并且是如何使用=得到結果的,這些運算子使用了Python的特殊方法(又名魔法方法),
這些特殊方法的名稱都以雙下劃線__開始和結束,就像__init__()方法一樣,
例如,在為重寫__str__()方法時,列印物件如下:
print(q1)
輸出:
<__main__.Quote object at 0x0000020B6FA17400>
顯然,格式很不友好,
此時可以重寫__str__()方法,來自定義列印物件的形式,如下;
class Quote():
def __init__(self, person, words):
self.person = person
self.words = words
def __str__(self):
return 'Quote(%s, %s)' %(self.person, self.words)
def who(self):
return self.person
def says(self):
return self.words + '.'
q1 = Quote('Edward', 'Normal quote')
print(q1)
輸出:
Quote(Edward, Normal quote)
顯然,此時列印物件時,是列印的自定義字串,
而如果不通過列印、而是直接像如下方式輸出:
q1
會輸出:
<__main__.Quote at 0x20b6f5c2970>
這可以通過__repr__()重寫方法,如下:
class Quote():
def __init__(self, person, words):
self.person = person
self.words = words
def __str__(self):
return 'Quote(%s, %s)' %(self.person, self.words)
def __repr__(self):
return 'Quote(%s, %s)' %(self.person, self.words)
def who(self):
return self.person
def says(self):
return self.words + '.'
q1 = Quote('Edward', 'Normal quote')
q1
輸出:
Quote(Edward, Normal quote)
還有很多其他的魔法方法,可以根據需要選擇使用,
總結
Python之所以可以獲得廣泛的應用,一個很重要的原因就是廣泛的庫支持,不僅可以自定義模塊和包,還可以使用Python標準庫和第三方庫,大大增加了Python的功能,Python中的檔案IO操作也很方便,可以對文本檔案、二進制檔案、格式化檔案進行讀寫,并進行其他檔案操作,面向物件是Python的一大特性,但是相比于其他語言限制更少、更加靈活,具有繼承、多型等特性,可以靈活使用,
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/143363.html
標籤:其他
下一篇:Python識別驗證碼

963624318 在群檔案夾商業資料分析從入門到入職中下載即可,