Python面向?qū)ο笾惡蛯嵗梅ǚ治?/h1>
更新時間:2019年06月08日 09:31:22 作者:feesland
這篇文章主要介紹了Python類和實例用法,較為詳細(xì)的分析了Python面向?qū)ο蟪绦蛟O(shè)計中類、實例、構(gòu)造函數(shù)、析構(gòu)函數(shù)、私有變量等相關(guān)概念與使用技巧,需要的朋友可以參考下
本文實例講述了Python面向?qū)ο笾惡蛯嵗梅?。分享給大家供大家參考,具體如下:
類
雖然 Python 是解釋性語言,但是它是面向?qū)ο蟮?,能夠進(jìn)行對象編程。至于何為面向?qū)ο?,在此就不詳說了。面向?qū)ο蟪绦蛟O(shè)計本身就很值得深入學(xué)習(xí),如要了解,請參閱網(wǎng)上其他的資料。
面向?qū)ο笞钪匾母拍罹褪穷悾–lass)和實例(Instance),牢記 類 是抽象的模板,比如Student類,而實例是根據(jù)類創(chuàng)建出來的一個個具體的“對象”,每個對象都擁有相同的方法,但各自的數(shù)據(jù)可能不同。
以Student類為例,在Python中,定義類是通過 class 關(guān)鍵字:
注意:Python 2.X 需要在類名后面加 (object) 這邊 pass 語句表示空語句段。
class 后面緊接著是類名,即Student,類名通常是大寫開頭的單詞,緊接著是(object),表示該類是從哪個類繼承下來的,繼承的概念我們后面再講,通常,如果沒有合適的繼承類,就使用object類,這是所有類最終都會繼承的類。
class Student(object):
pass
Python 3.X 則只需要類名,不需要加 (object)
class Student:
pass
實例
創(chuàng)建實例是通過類名+()實現(xiàn)的(若 __init__ 無或僅有self);如定義好了Student類,就可以根據(jù)Student類創(chuàng)建出Student的實例,如下:
class Student(object):
pass
May = Student() # 新建May實例
print(May)
運行結(jié)果:
<__main__.Student object at 0x0000000001DCC400>
可以看到,變量May指向的就是一個Student的object,后面的0x006C4770是內(nèi)存地址,每個object的地址都不一樣,而Student本身則是一個類。
可以自由地給一個實例變量綁定屬性,比如,給實例 May 綁定一個 name 屬性,這個 name 屬性是實例 May 特有的,其他新建的實例是沒有 name 屬性的
class Student(object):
pass
May = Student() # 新建May實例
print(May)
May.name = "May" # 給實例 May 綁定 name 屬性為 "May"
print(May.name)
Peter = Student() # 新建Peter實例
# print(Peter.name) # 報錯,因為Peter沒有Name屬性
那么,如果我們需要類必須綁定屬性,那如何定義呢? 請參見下文。
__init__ 構(gòu)造函數(shù)
由于類可以起到模板的作用,因此,可以在創(chuàng)建實例的時候,把一些我們認(rèn)為必須綁定的屬性強制填寫進(jìn)去。 (注意 __init__ 雙下劃線)
如對于Student類,我們定義 name 和 score 屬性(所有Sudent 都須有的屬性):
__init__方法的第一個參數(shù)永遠(yuǎn)是self,表示創(chuàng)建的實例本身,因此,在__init__方法內(nèi)部,就可以把各種屬性綁定到self,因為self就指向創(chuàng)建的實例本身。
有了__init__方法,在創(chuàng)建實例的時候,就不能傳入空的參數(shù)了,必須傳入與__init__方法匹配的參數(shù),但self不需要傳,Python解釋器自己會把實例變量傳進(jìn)去:
class Student(object):
def __init__(self, name, score):
self.name = name
self.score = score
May = Student("May",90) # 須要提供兩個屬性
Peter = Student("Peter",85)
print(May.name, May.score)
print(Peter.name, Peter.score)
__del__ 析構(gòu)函數(shù)
Just like the __init__ method, there is another special method __del__ which is called when an object is going to die i.e. it is no longer being used and is being returned to the computer system for reusing that piece of memory.
The __del__ method is run when the object is no longer in use and there is no guarantee when that method will be run. If you want to explicitly see it in action, we have to use the del statement which is what we have done here.
相對于 構(gòu)造函數(shù) Python 也有類似 C++ 中的析構(gòu)函數(shù) __del__ , Python的垃圾回收過程與常用語言的不一樣,如果一定需要,最好需要使用del語句來激活。
私有變量
Note for C++/Java/C# Programmers
All class members (including the data members) are public and all the methods are virtual in Python.
One exception: If you use data members with names using the double underscore prefix such as __privatevar, Python uses name-mangling to effectively make it a private variable.
Thus, the convention followed is that any variable that is to be used only within the class or object should begin with an underscore and all other names are public and can be used by other classes/objects. Remember that this is only a convention and is not enforced by Python (except for the double underscore prefix).
Python 中定義私有變量,命名規(guī)則為前綴加兩個下劃線 “__” ,注意不可前后都包含__XXX__(該命名表示為類屬性或內(nèi)建變量);還有一種命名為單下劃線 _XXX ,表示約定俗成不可訪問的變量。
class Person(object):
def __init__(self,name,sex):
self.name = name
self.__sex = sex # sex 定義為私有變量
def print_title(self):
if self.sex == "male":
print("man")
elif self.sex == "female":
print("woman")
May = Person("May","female")
print(May.name)
print(May.__sex) # 會報錯
更多關(guān)于Python相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Python面向?qū)ο蟪绦蛟O(shè)計入門與進(jìn)階教程》、《Python數(shù)據(jù)結(jié)構(gòu)與算法教程》、《Python函數(shù)使用技巧總結(jié)》、《Python字符串操作技巧匯總》、《Python編碼操作技巧總結(jié)》及《Python入門與進(jìn)階經(jīng)典教程》
希望本文所述對大家Python程序設(shè)計有所幫助。
相關(guān)文章
-
推薦技術(shù)人員一款Python開源庫(造數(shù)據(jù)神器)
今天小編給大家推薦一款Python開源庫,技術(shù)人必備的造數(shù)據(jù)神器!非常不錯,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友參考下吧 2020-07-07
-
Python使用execjs執(zhí)行包含中文參數(shù)的JavaScript
爬蟲的開發(fā)過程中,往往需要對JS進(jìn)行模擬,簡單或者通用的還可以在Python中模擬或者找到對應(yīng)的第三方庫,但是復(fù)雜的就可能不好實現(xiàn)了,下面這篇文章主要給大家介紹了關(guān)于Python使用execjs執(zhí)行包含中文參數(shù)的JavaScript的相關(guān)資料,需要的朋友可以參考下 2022-03-03
-
django的auth認(rèn)證,authenticate和裝飾器功能詳解
這篇文章主要介紹了django的auth認(rèn)證,authenticate和裝飾器功能詳解,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下 2019-07-07
-
Pyqt清空某一個QTreeewidgetItem下的所有分支方法
今天小編就為大家分享一篇Pyqt清空某一個QTreeewidgetItem下的所有分支方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧 2019-06-06
-
Python調(diào)用JavaScript代碼的幾種方法小結(jié)
日常Web端爬蟲過程中,經(jīng)常會遇到參數(shù)被加密的場景,因此,我們需要分析網(wǎng)頁源代碼通過調(diào)式,一層層剝離出關(guān)鍵的JS代碼,使用Python去執(zhí)行這段代碼,本文將聊聊利用 Python 調(diào)用 JS 的4種方式,需要的朋友可以參考下 2024-12-12
最新評論
本文實例講述了Python面向?qū)ο笾惡蛯嵗梅?。分享給大家供大家參考,具體如下:
類
雖然 Python 是解釋性語言,但是它是面向?qū)ο蟮?,能夠進(jìn)行對象編程。至于何為面向?qū)ο?,在此就不詳說了。面向?qū)ο蟪绦蛟O(shè)計本身就很值得深入學(xué)習(xí),如要了解,請參閱網(wǎng)上其他的資料。
面向?qū)ο笞钪匾母拍罹褪穷悾–lass)和實例(Instance),牢記 類 是抽象的模板,比如Student類,而實例是根據(jù)類創(chuàng)建出來的一個個具體的“對象”,每個對象都擁有相同的方法,但各自的數(shù)據(jù)可能不同。
以Student類為例,在Python中,定義類是通過 class 關(guān)鍵字:
注意:Python 2.X 需要在類名后面加 (object) 這邊 pass 語句表示空語句段。
class 后面緊接著是類名,即Student,類名通常是大寫開頭的單詞,緊接著是(object),表示該類是從哪個類繼承下來的,繼承的概念我們后面再講,通常,如果沒有合適的繼承類,就使用object類,這是所有類最終都會繼承的類。
class Student(object): pass
Python 3.X 則只需要類名,不需要加 (object)
class Student: pass
實例
創(chuàng)建實例是通過類名+()實現(xiàn)的(若 __init__ 無或僅有self);如定義好了Student類,就可以根據(jù)Student類創(chuàng)建出Student的實例,如下:
class Student(object): pass May = Student() # 新建May實例 print(May)
運行結(jié)果:
<__main__.Student object at 0x0000000001DCC400>
可以看到,變量May指向的就是一個Student的object,后面的0x006C4770是內(nèi)存地址,每個object的地址都不一樣,而Student本身則是一個類。
可以自由地給一個實例變量綁定屬性,比如,給實例 May 綁定一個 name 屬性,這個 name 屬性是實例 May 特有的,其他新建的實例是沒有 name 屬性的
class Student(object): pass May = Student() # 新建May實例 print(May) May.name = "May" # 給實例 May 綁定 name 屬性為 "May" print(May.name) Peter = Student() # 新建Peter實例 # print(Peter.name) # 報錯,因為Peter沒有Name屬性
那么,如果我們需要類必須綁定屬性,那如何定義呢? 請參見下文。
__init__ 構(gòu)造函數(shù)
由于類可以起到模板的作用,因此,可以在創(chuàng)建實例的時候,把一些我們認(rèn)為必須綁定的屬性強制填寫進(jìn)去。 (注意 __init__ 雙下劃線)
如對于Student類,我們定義 name 和 score 屬性(所有Sudent 都須有的屬性):
__init__方法的第一個參數(shù)永遠(yuǎn)是self,表示創(chuàng)建的實例本身,因此,在__init__方法內(nèi)部,就可以把各種屬性綁定到self,因為self就指向創(chuàng)建的實例本身。
有了__init__方法,在創(chuàng)建實例的時候,就不能傳入空的參數(shù)了,必須傳入與__init__方法匹配的參數(shù),但self不需要傳,Python解釋器自己會把實例變量傳進(jìn)去:
class Student(object):
def __init__(self, name, score):
self.name = name
self.score = score
May = Student("May",90) # 須要提供兩個屬性
Peter = Student("Peter",85)
print(May.name, May.score)
print(Peter.name, Peter.score)
__del__ 析構(gòu)函數(shù)
Just like the __init__ method, there is another special method __del__ which is called when an object is going to die i.e. it is no longer being used and is being returned to the computer system for reusing that piece of memory.
The __del__ method is run when the object is no longer in use and there is no guarantee when that method will be run. If you want to explicitly see it in action, we have to use the del statement which is what we have done here.
相對于 構(gòu)造函數(shù) Python 也有類似 C++ 中的析構(gòu)函數(shù) __del__ , Python的垃圾回收過程與常用語言的不一樣,如果一定需要,最好需要使用del語句來激活。
私有變量
Note for C++/Java/C# Programmers
All class members (including the data members) are public and all the methods are virtual in Python.
One exception: If you use data members with names using the double underscore prefix such as __privatevar, Python uses name-mangling to effectively make it a private variable.
Thus, the convention followed is that any variable that is to be used only within the class or object should begin with an underscore and all other names are public and can be used by other classes/objects. Remember that this is only a convention and is not enforced by Python (except for the double underscore prefix).
Python 中定義私有變量,命名規(guī)則為前綴加兩個下劃線 “__” ,注意不可前后都包含__XXX__(該命名表示為類屬性或內(nèi)建變量);還有一種命名為單下劃線 _XXX ,表示約定俗成不可訪問的變量。
class Person(object):
def __init__(self,name,sex):
self.name = name
self.__sex = sex # sex 定義為私有變量
def print_title(self):
if self.sex == "male":
print("man")
elif self.sex == "female":
print("woman")
May = Person("May","female")
print(May.name)
print(May.__sex) # 會報錯
更多關(guān)于Python相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Python面向?qū)ο蟪绦蛟O(shè)計入門與進(jìn)階教程》、《Python數(shù)據(jù)結(jié)構(gòu)與算法教程》、《Python函數(shù)使用技巧總結(jié)》、《Python字符串操作技巧匯總》、《Python編碼操作技巧總結(jié)》及《Python入門與進(jìn)階經(jīng)典教程》
希望本文所述對大家Python程序設(shè)計有所幫助。
相關(guān)文章
推薦技術(shù)人員一款Python開源庫(造數(shù)據(jù)神器)
今天小編給大家推薦一款Python開源庫,技術(shù)人必備的造數(shù)據(jù)神器!非常不錯,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友參考下吧2020-07-07
Python使用execjs執(zhí)行包含中文參數(shù)的JavaScript
爬蟲的開發(fā)過程中,往往需要對JS進(jìn)行模擬,簡單或者通用的還可以在Python中模擬或者找到對應(yīng)的第三方庫,但是復(fù)雜的就可能不好實現(xiàn)了,下面這篇文章主要給大家介紹了關(guān)于Python使用execjs執(zhí)行包含中文參數(shù)的JavaScript的相關(guān)資料,需要的朋友可以參考下2022-03-03
django的auth認(rèn)證,authenticate和裝飾器功能詳解
這篇文章主要介紹了django的auth認(rèn)證,authenticate和裝飾器功能詳解,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2019-07-07
Pyqt清空某一個QTreeewidgetItem下的所有分支方法
今天小編就為大家分享一篇Pyqt清空某一個QTreeewidgetItem下的所有分支方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-06-06
Python調(diào)用JavaScript代碼的幾種方法小結(jié)
日常Web端爬蟲過程中,經(jīng)常會遇到參數(shù)被加密的場景,因此,我們需要分析網(wǎng)頁源代碼通過調(diào)式,一層層剝離出關(guān)鍵的JS代碼,使用Python去執(zhí)行這段代碼,本文將聊聊利用 Python 調(diào)用 JS 的4種方式,需要的朋友可以參考下2024-12-12

