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

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

2020-09-30 14:12:34 其他

文章目錄

  • 前言
  • 一、程式、模塊和包
    • 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/qita/142284.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)

熱門瀏覽
  • 網閘典型架構簡述

    網閘架構一般分為兩種:三主機的三系統架構網閘和雙主機的2+1架構網閘。 三主機架構分別為內端機、外端機和仲裁機。三機無論從軟體和硬體上均各自獨立。首先從硬體上來看,三機都用各自獨立的主板、記憶體及存盤設備。從軟體上來看,三機有各自獨立的作業系統。這樣能達到完全的三機獨立。對于“2+1”系統,“2”分為 ......

    uj5u.com 2020-09-10 02:00:44 more
  • 如何從xshell上傳檔案到centos linux虛擬機里

    如何從xshell上傳檔案到centos linux虛擬機里及:虛擬機CentOs下執行 yum -y install lrzsz命令,出現錯誤:鏡像無法找到軟體包 前言 一、安裝lrzsz步驟 二、上傳檔案 三、遇到的問題及解決方案 總結 前言 提示:其實很簡單,往虛擬機上安裝一個上傳檔案的工具 ......

    uj5u.com 2020-09-10 02:00:47 more
  • 一、SQLMAP入門

    一、SQLMAP入門 1、判斷是否存在注入 sqlmap.py -u 網址/id=1 id=1不可缺少。當注入點后面的引數大于兩個時。需要加雙引號, sqlmap.py -u "網址/id=1&uid=1" 2、判斷文本中的請求是否存在注入 從文本中加載http請求,SQLMAP可以從一個文本檔案中 ......

    uj5u.com 2020-09-10 02:00:50 more
  • Metasploit 簡單使用教程

    metasploit 簡單使用教程 浩先生, 2020-08-28 16:18:25 分類專欄: kail 網路安全 linux 文章標簽: linux資訊安全 編輯 著作權 metasploit 使用教程 前言 一、Metasploit是什么? 二、準備作業 三、具體步驟 前言 Msfconsole ......

    uj5u.com 2020-09-10 02:00:53 more
  • 游戲逆向之驅動層與用戶層通訊

    驅動層代碼: #pragma once #include <ntifs.h> #define add_code CTL_CODE(FILE_DEVICE_UNKNOWN,0x800,METHOD_BUFFERED,FILE_ANY_ACCESS) /* 更多游戲逆向視頻www.yxfzedu.com ......

    uj5u.com 2020-09-10 02:00:56 more
  • 北斗電力時鐘(北斗授時服務器)讓網路資料更精準

    北斗電力時鐘(北斗授時服務器)讓網路資料更精準 北斗電力時鐘(北斗授時服務器)讓網路資料更精準 京準電子科技官微——ahjzsz 近幾年,資訊技術的得了快速發展,互聯網在逐漸普及,其在人們生活和生產中都得到了廣泛應用,并且取得了不錯的應用效果。計算機網路資訊在電力系統中的應用,一方面使電力系統的運行 ......

    uj5u.com 2020-09-10 02:01:03 more
  • 【CTF】CTFHub 技能樹 彩蛋 writeup

    ?碎碎念 CTFHub:https://www.ctfhub.com/ 筆者入門CTF時時剛開始刷的是bugku的舊平臺,后來才有了CTFHub。 感覺不論是網頁UI設計,還是題目質量,賽事跟蹤,工具軟體都做得很不錯。 而且因為獨到的金幣制度的確讓人有一種想去刷題賺金幣的感覺。 個人還是非常喜歡這個 ......

    uj5u.com 2020-09-10 02:04:05 more
  • 02windows基礎操作

    我學到了一下幾點 Windows系統目錄結構與滲透的作用 常見Windows的服務詳解 Windows埠詳解 常用的Windows注冊表詳解 hacker DOS命令詳解(net user / type /md /rd/ dir /cd /net use copy、批處理 等) 利用dos命令制作 ......

    uj5u.com 2020-09-10 02:04:18 more
  • 03.Linux基礎操作

    我學到了以下幾點 01Linux系統介紹02系統安裝,密碼啊破解03Linux常用命令04LAMP 01LINUX windows: win03 8 12 16 19 配置不繁瑣 Linux:redhat,centos(紅帽社區版),Ubuntu server,suse unix:金融機構,證券,銀 ......

    uj5u.com 2020-09-10 02:04:30 more
  • 05HTML

    01HTML介紹 02頭部標簽講解03基礎標簽講解04表單標簽講解 HTML前段語言 js1.了解代碼2.根據代碼 懂得挖掘漏洞 (POST注入/XSS漏洞上傳)3.黑帽seo 白帽seo 客戶網站被黑帽植入劫持代碼如何處理4.熟悉html表單 <html><head><title>TDK標題,描述 ......

    uj5u.com 2020-09-10 02:04:36 more
最新发布
  • 2023年最新微信小程式抓包教程

    01 開門見山 隔一個月發一篇文章,不過分。 首先回顧一下《微信系結手機號資料庫被脫庫事件》,我也是第一時間得知了這個訊息,然后跟蹤了整件事情的經過。下面是這起事件的相關截圖以及近日流出的一萬條資料樣本: 個人認為這件事也沒什么,還不如關注一下之前45億快遞資料查詢渠道疑似在近日復活的訊息。 訊息是 ......

    uj5u.com 2023-04-20 08:48:24 more
  • web3 產品介紹:metamask 錢包 使用最多的瀏覽器插件錢包

    Metamask錢包是一種基于區塊鏈技術的數字貨幣錢包,它允許用戶在安全、便捷的環境下管理自己的加密資產。Metamask錢包是以太坊生態系統中最流行的錢包之一,它具有易于使用、安全性高和功能強大等優點。 本文將詳細介紹Metamask錢包的功能和使用方法。 一、 Metamask錢包的功能 數字資 ......

    uj5u.com 2023-04-20 08:47:46 more
  • vulnhub_Earth

    前言 靶機地址->>>vulnhub_Earth 攻擊機ip:192.168.20.121 靶機ip:192.168.20.122 參考文章 https://www.cnblogs.com/Jing-X/archive/2022/04/03/16097695.html https://www.cnb ......

    uj5u.com 2023-04-20 07:46:20 more
  • 從4k到42k,軟體測驗工程師的漲薪史,給我看哭了

    清明節一過,盲猜大家已經無心上班,在數著日子準備過五一,但一想到銀行卡里的余額……瞬間心情就不美麗了。最近,2023年高校畢業生就業調查顯示,本科畢業月平均起薪為5825元。調查一出,便有很多同學表示自己又被平均了。看著這一資料,不免讓人想到前不久中國青年報的一項調查:近六成大學生認為畢業10年內會 ......

    uj5u.com 2023-04-20 07:44:00 more
  • 最新版本 Stable Diffusion 開源 AI 繪畫工具之中文自動提詞篇

    🎈 標簽生成器 由于輸入正向提示詞 prompt 和反向提示詞 negative prompt 都是使用英文,所以對學習母語的我們非常不友好 使用網址:https://tinygeeker.github.io/p/ai-prompt-generator 這個網址是為了讓大家在使用 AI 繪畫的時候 ......

    uj5u.com 2023-04-20 07:43:36 more
  • 漫談前端自動化測驗演進之路及測驗工具分析

    隨著前端技術的不斷發展和應用程式的日益復雜,前端自動化測驗也在不斷演進。隨著 Web 應用程式變得越來越復雜,自動化測驗的需求也越來越高。如今,自動化測驗已經成為 Web 應用程式開發程序中不可或缺的一部分,它們可以幫助開發人員更快地發現和修復錯誤,提高應用程式的性能和可靠性。 ......

    uj5u.com 2023-04-20 07:43:16 more
  • CANN開發實踐:4個DVPP記憶體問題的典型案例解讀

    摘要:由于DVPP媒體資料處理功能對存放輸入、輸出資料的記憶體有更高的要求(例如,記憶體首地址128位元組對齊),因此需呼叫專用的記憶體申請介面,那么本期就分享幾個關于DVPP記憶體問題的典型案例,并給出原因分析及解決方法。 本文分享自華為云社區《FAQ_DVPP記憶體問題案例》,作者:昇騰CANN。 DVPP ......

    uj5u.com 2023-04-20 07:43:03 more
  • msf學習

    msf學習 以kali自帶的msf為例 一、msf核心模塊與功能 msf模塊都放在/usr/share/metasploit-framework/modules目錄下 1、auxiliary 輔助模塊,輔助滲透(埠掃描、登錄密碼爆破、漏洞驗證等) 2、encoders 編碼器模塊,主要包含各種編碼 ......

    uj5u.com 2023-04-20 07:42:59 more
  • Halcon軟體安裝與界面簡介

    1. 下載Halcon17版本到到本地 2. 雙擊安裝包后 3. 步驟如下 1.2 Halcon軟體安裝 界面分為四大塊 1. Halcon的五個助手 1) 影像采集助手:與相機連接,設定相機引數,采集影像 2) 標定助手:九點標定或是其它的標定,生成標定檔案及內參外參,可以將像素單位轉換為長度單位 ......

    uj5u.com 2023-04-20 07:42:17 more
  • 在MacOS下使用Unity3D開發游戲

    第一次發博客,先發一下我的游戲開發環境吧。 去年2月份買了一臺MacBookPro2021 M1pro(以下簡稱mbp),這一年來一直在用mbp開發游戲。我大致分享一下我的開發工具以及使用體驗。 1、Unity 官網鏈接: https://unity.cn/releases 我一般使用的Apple ......

    uj5u.com 2023-04-20 07:40:19 more