主頁 > 前端設計 > 商業資料分析從入門到入職(8)Python模塊、檔案IO和面向物件

商業資料分析從入門到入職(8)Python模塊、檔案IO和面向物件

2020-09-30 22:05:36 前端設計

文章目錄

  • 前言
  • 一、程式、模塊和包
    • 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群 Python極客部落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標準庫,而是第三方庫,需要下載安裝之后才能匯入,

安裝第三方庫可以使用condapip命令:
如安裝jieba庫(中文分詞庫)則可以執行conda install jiebapip 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

標籤:其他

上一篇:CTF萌新入坑指南之sqlmap環境搭建

下一篇:Python識別驗證碼

標籤雲
其他(157675) Python(38076) JavaScript(25376) Java(17977) C(15215) 區塊鏈(8255) C#(7972) AI(7469) 爪哇(7425) MySQL(7132) html(6777) 基礎類(6313) sql(6102) 熊猫(6058) PHP(5869) 数组(5741) R(5409) Linux(5327) 反应(5209) 腳本語言(PerlPython)(5129) 非技術區(4971) Android(4554) 数据框(4311) css(4259) 节点.js(4032) C語言(3288) json(3245) 列表(3129) 扑(3119) C++語言(3117) 安卓(2998) 打字稿(2995) VBA(2789) Java相關(2746) 疑難問題(2699) 细绳(2522) 單片機工控(2479) iOS(2429) ASP.NET(2402) MongoDB(2323) 麻木的(2285) 正则表达式(2254) 字典(2211) 循环(2198) 迅速(2185) 擅长(2169) 镖(2155) 功能(1967) .NET技术(1958) Web開發(1951) python-3.x(1918) HtmlCss(1915) 弹簧靴(1913) C++(1909) xml(1889) PostgreSQL(1872) .NETCore(1853) 谷歌表格(1846) Unity3D(1843) for循环(1842)

熱門瀏覽
  • vue移動端上拉加載

    可能做得過于簡單或者比較low,請各位大佬留情,一起探討技術 ......

    uj5u.com 2020-09-10 04:38:07 more
  • 優美網站首頁,頂部多層導航

    一個個人用的瀏覽器首頁,可以把一下常用的網站放在這里,平常打開會比較方便。 第一步,HTML代碼 <script src=https://www.cnblogs.com/szharf/p/"js/jquery-3.4.1.min.js"></script> <div id="navigate"> <ul> <li class="labels labels_1"> ......

    uj5u.com 2020-09-10 04:38:47 more
  • 頁面為要加<!DOCTYPE html>

    最近因為寫一個js函式,需要用到$(window).height(); 由于手寫demo的時候,過于自信,其實對前端方面的認識也不夠體系,用文本檔案直接敲出來的html代碼,第一行沒有加上<!DOCTYPE html> 導致了$(window).height();的結果直接是整個document的高 ......

    uj5u.com 2020-09-10 04:38:52 more
  • WordPress網站程式手動升級要做好資料備份

    WordPress博客網站程式在進行升級前,必須要做好網站資料的備份,這個問題良家佐言是遇見過的;在剛開始接觸WordPress博客程式的時候,因為升級問題和博客網站的修改的一些嘗試,良家佐言是吃盡了苦頭。因為購買的是西部數碼的空間和域名,每當佐言把自己的WordPress博客網站搞到一塌糊涂的時候 ......

    uj5u.com 2020-09-10 04:39:30 more
  • WordPress程式不能升級為5.4.2版本的原因

    WordPress是一款個人博客系統,受到英文博客愛好者和中文博客愛好者的追捧,并逐步演化成一款內容管理系統軟體;它是使用PHP語言和MySQL資料庫開發的,用戶可以在支持PHP和MySQL資料庫的服務器上使用自己的博客。每一次WordPress程式的更新,就會牽動無數WordPress愛好者的心, ......

    uj5u.com 2020-09-10 04:39:49 more
  • 使用CSS3的偽元素進行首字母下沉和首行改變樣式

    網頁中常見的一種效果,首字改變樣式或者首行改變樣式,效果如下圖。 代碼: <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, ......

    uj5u.com 2020-09-10 04:40:09 more
  • 關于a標簽的講解

    什么是a標簽? <a> 標簽定義超鏈接,用于從一個頁面鏈接到另一個頁面。 <a> 元素最重要的屬性是 href 屬性,它指定鏈接的目標。 a標簽的語法格式:<a href=https://www.cnblogs.com/summerxbc/p/"指定要跳轉的目標界面的鏈接">需要展示給用戶看見的內容</a> a標簽 在所有瀏覽器中,鏈接的默認外觀如下: 未被訪問的鏈接帶 ......

    uj5u.com 2020-09-10 04:40:11 more
  • 前端輪播圖

    在需要輪播的頁面是引入swiper.min.js和swiper.min.css swiper.min.js地址: 鏈接:https://pan.baidu.com/s/15Uh516YHa4CV3X-RyjEIWw 提取碼:4aks swiper.min.css地址 鏈接:https://pan.b ......

    uj5u.com 2020-09-10 04:40:13 more
  • 如何設定html中的背景圖片(全屏顯示,且不拉伸)

    1 <style>2 body{background-image:url(https://uploadbeta.com/api/pictures/random/?key=BingEverydayWallpaperPicture); 3 background-size:cover;background ......

    uj5u.com 2020-09-10 04:40:16 more
  • Java學習——HTML詳解(上)

    HTML詳解 初識HTML Hyper Text Markup Language(超文本標記語言) 1 <!--DOCTYPE:告訴瀏覽器我們要使用什么規范--> 2 <!DOCTYPE html> 3 <html lang="en"> 4 <head> 5 <!--meta 描述性的標簽,描述一些 ......

    uj5u.com 2020-09-10 04:40:33 more
最新发布
  • 我的第一個NPM包:panghu-planebattle-esm(胖虎飛機大戰)使用說明

    好家伙,我的包終于開發完啦 歡迎使用胖虎的飛機大戰包!! 為你的主頁添加色彩 這是一個有趣的網頁小游戲包,使用canvas和js開發 使用ES6模塊化開發 效果圖如下: (覺得圖片太sb的可以自己改) 代碼已開源!! Git: https://gitee.com/tang-and-han-dynas ......

    uj5u.com 2023-04-20 07:59:23 more
  • 生產事故-走近科學之消失的JWT

    入職多年,面對生產環境,盡管都是小心翼翼,慎之又慎,還是難免捅出簍子。輕則滿頭大汗,面紅耳赤。重則系統停擺,損失資金。每一個生產事故的背后,都是寶貴的經驗和教訓,都是專案成員的血淚史。為了更好地防范和遏制今后的各類事故,特開此專題,長期更新和記錄大大小小的各類事故。有些是親身經歷,有些是經人耳傳口授 ......

    uj5u.com 2023-04-18 07:55:04 more
  • 記錄--Canvas實作打飛字游戲

    這里給大家分享我在網上總結出來的一些知識,希望對大家有所幫助 打開游戲界面,看到一個畫面簡潔、卻又富有挑戰性的游戲。螢屏上,有一個白色的矩形框,里面不斷下落著各種單詞,而我需要迅速地輸入這些單詞。如果我輸入的單詞與螢屏上的單詞匹配,那么我就可以獲得得分;如果我輸入的單詞錯誤或者時間過長,那么我就會輸 ......

    uj5u.com 2023-04-04 08:35:30 more
  • 了解 HTTP 看這一篇就夠

    在學習網路之前,了解它的歷史能夠幫助我們明白為何它會發展為如今這個樣子,引發探究網路的興趣。下面的這張圖片就展示了“互聯網”誕生至今的發展歷程。 ......

    uj5u.com 2023-03-16 11:00:15 more
  • 藍牙-低功耗中心設備

    //11.開啟藍牙配接器 openBluetoothAdapter //21.開始搜索藍牙設備 startBluetoothDevicesDiscovery //31.開啟監聽搜索藍牙設備 onBluetoothDeviceFound //30.停止監聽搜索藍牙設備 offBluetoothDevi ......

    uj5u.com 2023-03-15 09:06:45 more
  • canvas畫板(滑鼠和觸摸)

    <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>canves</title> <style> #canvas { cursor:url(../images/pen.png),crosshair; } #canvasdiv{ bo ......

    uj5u.com 2023-02-15 08:56:31 more
  • 手機端H5 實作自定義拍照界面

    手機端 H5 實作自定義拍照界面也可以使用 MediaDevices API 和 <video> 標簽來實作,和在桌面端做法基本一致。 首先,使用 MediaDevices.getUserMedia() 方法獲取攝像頭媒體流,并將其傳遞給 <video> 標簽進行渲染。 接著,使用 HTML 的 < ......

    uj5u.com 2023-01-12 07:58:22 more
  • 記錄--短視頻滑動播放在 H5 下的實作

    這里給大家分享我在網上總結出來的一些知識,希望對大家有所幫助 短視頻已經無數不在了,但是主體還是使用 app 來承載的。本文講述 H5 如何實作 app 的視頻滑動體驗。 無聲勝有聲,一圖頂百辯,且看下圖: 網址鏈接(需在微信或者手Q中瀏覽) 從上圖可以看到,我們主要實作的功能也是本文要講解的有: ......

    uj5u.com 2023-01-04 07:29:05 more
  • 一文讀懂 HTTP/1 HTTP/2 HTTP/3

    從 1989 年萬維網(www)誕生,HTTP(HyperText Transfer Protocol)經歷了眾多版本迭代,WebSocket 也在期間萌芽。1991 年 HTTP0.9 被發明。1996 年出現了 HTTP1.0。2015 年 HTTP2 正式發布。2020 年 HTTP3 或能正... ......

    uj5u.com 2022-12-24 06:56:02 more
  • 【HTML基礎篇002】HTML之form表單超詳解

    ??一、form表單是什么

    ??二、form表單的屬性

    ??三、input中的各種Type屬性值

    ??四、標簽 ......

    uj5u.com 2022-12-18 07:17:06 more