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

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

2020-09-30 13:46:28 區塊鏈

文章目錄

  • 前言
  • 一、程式、模塊和包
    • 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/qukuanlian/142219.html

標籤:區塊鏈

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

下一篇:【資料結構Python描述】優先級佇列簡介及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)

熱門瀏覽
  • JAVA使用 web3j 進行token轉賬

    最近新學習了下區塊鏈這方面的知識,所學不多,給大家分享下。 # 1. 關于web3j web3j是一個高度模塊化,反應性,型別安全的Java和Android庫,用于與智能合約配合并與以太坊網路上的客戶端(節點)集成。 # 2. 準備作業 jdk版本1.8 引入maven <dependency> < ......

    uj5u.com 2020-09-10 03:03:06 more
  • 以太坊智能合約開發框架Truffle

    前言 部署智能合約有多種方式,命令列的瀏覽器的渠道都有,但往往跟我們程式員的風格不太相符,因為我們習慣了在IDE里寫了代碼然后打包運行看效果。 雖然現在IDE中已經存在了Solidity插件,可以撰寫智能合約,但是部署智能合約卻要另走他路,沒辦法進行一個快捷的部署與測驗。 如果團隊管理的區塊節點多、 ......

    uj5u.com 2020-09-10 03:03:12 more
  • 谷歌二次驗證碼成為區塊鏈專用安全碼,你怎么看?

    前言 谷歌身份驗證器,前些年大家都比較陌生,但隨著國內互聯網安全的加強,它越來越多地出現在大家的視野中。 比較廣泛接觸的人群是國際3A游戲愛好者,游戲盜號現象嚴重+國外賬號安全應用廣泛,這類游戲一般都會要求用戶系結名為“兩步驗證”、“雙重驗證”等,平臺一般都推薦用谷歌身份驗證器。 后來區塊鏈業務風靡 ......

    uj5u.com 2020-09-10 03:03:17 more
  • 密碼學DAY1

    目錄 ##1.1 密碼學基本概念 密碼在我們的生活中有著重要的作用,那么密碼究竟來自何方,為何會產生呢? 密碼學是網路安全、資訊安全、區塊鏈等產品的基礎,常見的非對稱加密、對稱加密、散列函式等,都屬于密碼學范疇。 密碼學有數千年的歷史,從最開始的替換法到如今的非對稱加密演算法,經歷了古典密碼學,近代密 ......

    uj5u.com 2020-09-10 03:03:50 more
  • 密碼學DAY1_02

    目錄 ##1.1 ASCII編碼 ASCII(American Standard Code for Information Interchange,美國資訊交換標準代碼)是基于拉丁字母的一套電腦編碼系統,主要用于顯示現代英語和其他西歐語言。它是現今最通用的單位元組編碼系統,并等同于國際標準ISO/IE ......

    uj5u.com 2020-09-10 03:04:50 more
  • 密碼學DAY2

    ##1.1 加密模式 加密模式:https://docs.oracle.com/javase/8/docs/api/javax/crypto/Cipher.html ECB ECB : Electronic codebook, 電子密碼本. 需要加密的訊息按照塊密碼的塊大小被分為數個塊,并對每個塊進 ......

    uj5u.com 2020-09-10 03:05:42 more
  • NTP時鐘服務器的特點(京準電子)

    NTP時鐘服務器的特點(京準電子) NTP時鐘服務器的特點(京準電子) 京準電子官V——ahjzsz 首先對時間同步進行了背景介紹,然后討論了不同的時間同步網路技術,最后指出了建立全球或區域時間同步網存在的問題。 一、概 述 在通信領域,“同步”概念是指頻率的同步,即網路各個節點的時鐘頻率和相位同步 ......

    uj5u.com 2020-09-10 03:05:47 more
  • 標準化考場時鐘同步系統推進智能化校園建設

    標準化考場時鐘同步系統推進智能化校園建設 標準化考場時鐘同步系統推進智能化校園建設 安徽京準電子科技官微——ahjzsz 一、背景概述隨著教育事業的快速發展,學校建設如雨后春筍,隨之而來的學校教育、管理、安全方面的問題成了學校管理人員面臨的最大的挑戰,這些問題同時也是學生家長所擔心的。為了讓學生有更 ......

    uj5u.com 2020-09-10 03:05:51 more
  • 位元幣入門

    引言 位元幣基本結構 位元幣基礎知識 1)哈希演算法 2)非對稱加密技術 3)數字簽名 4)MerkleTree 5)哪有位元幣,有的是UTXO 6)位元幣挖礦與共識 7)區塊驗證(共識) 總結 引言 上一篇我們已經知道了什么是區塊鏈,此篇說一下區塊鏈的第一個應用——位元幣。其實先有位元幣,后有的區塊 ......

    uj5u.com 2020-09-10 03:06:15 more
  • 北斗對時服務器(北斗對時設備)電力系統應用

    北斗對時服務器(北斗對時設備)電力系統應用 北斗對時服務器(北斗對時設備)電力系統應用 京準電子科技官微(ahjzsz) 中國北斗衛星導航系統(英文名稱:BeiDou Navigation Satellite System,簡稱BDS),因為是目前世界范圍內唯一可以大面積提供免費定位服務的系統,所以 ......

    uj5u.com 2020-09-10 03:06:20 more
最新发布
  • web3 產品介紹:metamask 錢包 使用最多的瀏覽器插件錢包

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

    uj5u.com 2023-04-20 08:46:47 more
  • Hyperledger Fabric 使用 CouchDB 和復雜智能合約開發

    在上個實驗中,我們已經實作了簡單智能合約實作及客戶端開發,但該實驗中智能合約只有基礎的增刪改查功能,且其中的資料管理功能與傳統 MySQL 比相差甚遠。本文將在前面實驗的基礎上,將 Hyperledger Fabric 的默認資料庫支持 LevelDB 改為 CouchDB 模式,以實作更復雜的資料... ......

    uj5u.com 2023-04-16 07:28:31 more
  • .NET Core 波場鏈離線簽名、廣播交易(發送 TRX和USDT)筆記

    Get Started NuGet You can run the following command to install the Tron.Wallet.Net in your project. PM> Install-Package Tron.Wallet.Net 配置 public reco ......

    uj5u.com 2023-04-14 08:08:00 more
  • DKP 黑客分析——不正確的代幣對比率計算

    概述: 2023 年 2 月 8 日,針對 DKP 協議的閃電貸攻擊導致該協議的用戶損失了 8 萬美元,因為 execute() 函式取決于 USDT-DKP 對中兩種代幣的余額比率。 智能合約黑客概述: 攻擊者的交易:0x0c850f,0x2d31 攻擊者地址:0xF38 利用合同:0xf34ad ......

    uj5u.com 2023-04-07 07:46:09 more
  • Defi開發簡介

    Defi開發簡介 介紹 Defi是去中心化金融的縮寫, 是一項旨在利用區塊鏈技術和智能合約創建更加開放,可訪問和透明的金融體系的運動. 這與傳統金融形成鮮明對比,傳統金融通常由少數大型銀行和金融機構控制 在Defi的世界里,用戶可以直接從他們的電腦或移動設備上訪問廣泛的金融服務,而不需要像銀行或者信 ......

    uj5u.com 2023-04-05 08:01:34 more
  • solidity簡單的ERC20代幣實作

    // SPDX-License-Identifier: GPL-3.0 pragma solidity >=0.7.0 <0.9.0; import "hardhat/console.sol"; //ERC20 同質化代幣,每個代幣的本質或性質都是相同 //ETH 是原生代幣,它不是ERC20代幣, ......

    uj5u.com 2023-03-21 07:56:29 more
  • solidity 參考型別修飾符memory、calldata與storage 常量修飾符C

    在solidity語言中 參考型別修飾符(參考型別為存盤空間不固定的數值型別) memory、calldata與storage,它們只能修飾參考型別變數,比如字串、陣列、位元組等... memory 適用于方法傳參、返參或在方法體內使用,使用完就會清除掉,釋放記憶體 calldata 僅適用于方法傳參 ......

    uj5u.com 2023-03-08 07:57:54 more
  • solidity注解標簽

    在solidity語言中 注釋符為// 注解符為/* 內容*/ 或者 是 ///內容 注解中含有這幾個標簽給予我們使用 @title 一個應該描述合約/介面的標題 contract, library, interface @author 作者的名字 contract, library, interf ......

    uj5u.com 2023-03-08 07:57:49 more
  • 評價指標:相似度、GAS消耗

    【代碼注釋自動生成方法綜述】 這些評測指標主要來自機器翻譯和文本總結等研究領域,可以評估候選文本(即基于代碼注釋自動方法而生成)和參考文本(即基于手工方式而生成)的相似度. BLEU指標^[^?88^^?^]^:其全稱是bilingual evaluation understudy.該指標是最早用于 ......

    uj5u.com 2023-02-23 07:27:39 more
  • 基于NOSTR協議的“公有制”版本的Twitter,去中心化社交軟體Damus

    最近,一個幽靈,Web3的幽靈,在網路游蕩,它叫Damus,這玩意詮釋了什么叫做病毒式營銷,滑稽的是,一個Web3產品卻在Web2的產品鏈上瘋狂傳銷,各方大佬紛紛為其背書,到底發生了什么?Damus的葫蘆里,賣的是什么藥? 注冊和簡單實用 很少有什么產品在用戶注冊環節會有什么噱頭,但Damus確實出 ......

    uj5u.com 2023-02-05 06:48:39 more