大家學Python物件導向的時候,總是會遇到一個讓人難以理解的存在:self。
這個self到底是誰啊,為什麼每個類別實例方法都有一個參數self,它到底有什麼作用呢?
「先下結論:類別實例化後,self即代表實例(物件)本身。」
想要理解self有個最簡單的方法,就是你把self當作「實例(物件)的身分證。」
Python的類別不能直接使用,只有透過建立實例(物件)才能發揮它的功能,每個實例(物件)都是獨一無二的,它可以呼叫類別的方法、屬性。類別就像靈魂附體一樣,讓實例(物件)有了自己(self)的功能。
初學者會發現,類別的方法(建構方法和實例方法)中都會有一個固定參數self,其實這個參數就是代表著實例(物件)本身,就像是身分證,實例可以憑著身分證去呼叫類別方法。
類比人類,人類就是一個Python類,每個個體的人代表著實例(對象),而每個人的身分證代表的Python中self,每個人可以用身分證去上大學、坐高鐵、住在飯店...(方法),而Python中的實例(物件)也可以憑著self去呼叫類別的方法。
上面是用類比的方法解釋了下self的意義,說到底self就是代表著實例本身,「當某個實例(物件)呼叫類別方法時,該物件會把自身的引用作為第一個參數自動傳給該方法,而這第一個參數就是self。」
而且self只是約定俗成的寫法,你可以用任何其他名稱代替self,不會改變程式碼意義,只不過我們一般不會這麼做。
為了更好的說明self的作用,我們來舉個例子,下面有一個Students類別:
class Students: # 构造方法 def __init__(self,name): self.name = name # 实例方法 def study(self,examination_results): self.examination_results = examination_results print("同学{}的考试分数是{}".format(self.name,self.examination_results)) print("该实例对象的地址是{}".format(self))
先來個實例student_a。
studend_a = Students('studend_a') print(studend_a.name)
結果列印出:studend_a。
再來個實例student_b。
studend_b = Students('studend_b') print(studend_b.name)
結果列印出:studend_b。
可以看出,實例(物件)不一樣,列印出來的結果也不一樣,當類別被實例化後,self.name其實等於實例(物件).name。
還是以剛剛的程式碼為例,我們再來呼叫裡面的實例方法,裡面會印出self,就能看得更加明顯了。
實例student_a:
studend_a = Students('studend_a') print(studend_a.study(80))
輸出結果:
同學studend_a的考試分數是80 這個實例物件的位址是。
實例student_b:
studend_b = Students('studend_b') print(studend_b.study(80))
輸出結果:
同學studend_b的考試分數是80 這個實例物件的位址是。
大家能清楚看到兩個實例印出的self是不一樣的,因為self代表著實例(物件)本身。
以實例student_b來說,列印self出現下面物件資訊。
<__main__.students object at>。
如果再印出 student_b,會出現同樣的結果。
print(student_b)
輸出:
<__main__.students object at>。
這時候是不是就清楚了,類別實例化後,self即代表著實例(物件)本身。
以上是聊聊Python裡面的Self,是誰?的詳細內容。更多資訊請關注PHP中文網其他相關文章!

Pythonarrayssupportvariousoperations:1)Slicingextractssubsets,2)Appending/Extendingaddselements,3)Insertingplaceselementsatspecificpositions,4)Removingdeleteselements,5)Sorting/Reversingchangesorder,and6)Listcomprehensionscreatenewlistsbasedonexistin

NumPyarraysareessentialforapplicationsrequiringefficientnumericalcomputationsanddatamanipulation.Theyarecrucialindatascience,machinelearning,physics,engineering,andfinanceduetotheirabilitytohandlelarge-scaledataefficiently.Forexample,infinancialanaly

useanArray.ArarayoveralistinpythonwhendeAlingwithHomoGeneData,performance-Caliticalcode,orinterfacingwithccode.1)同質性data:arraysSaveMemorywithTypedElements.2)績效code-performance-calitialcode-calliginal-clitical-clitical-calligation-Critical-Code:Arraysofferferbetterperbetterperperformanceformanceformancefornallancefornalumericalical.3)

不,notalllistoperationsareSupportedByArrays,andviceversa.1)arraysdonotsupportdynamicoperationslikeappendorinsertwithoutresizing,wheremactsperformance.2)listssdonotguaranteeconecontanttanttanttanttanttanttanttanttanttimecomplecomecomplecomecomecomecomecomecomplecomectacccesslectaccesslecrectaccesslerikearraysodo。

toAccesselementsInapythonlist,useIndIndexing,負索引,切片,口頭化。 1)indexingStartSat0.2)否定indexingAccessesessessessesfomtheend.3)slicingextractsportions.4)iterationerationUsistorationUsisturessoreTionsforloopsoreNumeratorseforeporloopsorenumerate.alwaysCheckListListListListlentePtotoVoidToavoIndexIndexIndexIndexIndexIndExerror。

Arraysinpython,尤其是Vianumpy,ArecrucialInsCientificComputingfortheireftheireffertheireffertheirefferthe.1)Heasuedfornumerericalicerationalation,dataAnalysis和Machinelearning.2)Numpy'Simpy'Simpy'simplementIncressionSressirestrionsfasteroperoperoperationspasterationspasterationspasterationspasterationspasterationsthanpythonlists.3)inthanypythonlists.3)andAreseNableAblequick

你可以通過使用pyenv、venv和Anaconda來管理不同的Python版本。 1)使用pyenv管理多個Python版本:安裝pyenv,設置全局和本地版本。 2)使用venv創建虛擬環境以隔離項目依賴。 3)使用Anaconda管理數據科學項目中的Python版本。 4)保留系統Python用於系統級任務。通過這些工具和策略,你可以有效地管理不同版本的Python,確保項目順利運行。

numpyarrayshaveseveraladagesoverandastardandpythonarrays:1)基於基於duetoc的iMplation,2)2)他們的aremoremoremorymorymoremorymoremorymoremorymoremoremory,尤其是WithlargedAtasets和3)效率化,效率化,矢量化函數函數函數函數構成和穩定性構成和穩定性的操作,製造


熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

Video Face Swap
使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章

熱工具

SublimeText3 Linux新版
SublimeText3 Linux最新版

MinGW - Minimalist GNU for Windows
這個專案正在遷移到osdn.net/projects/mingw的過程中,你可以繼續在那裡關注我們。 MinGW:GNU編譯器集合(GCC)的本機Windows移植版本,可自由分發的導入函式庫和用於建置本機Windows應用程式的頭檔;包括對MSVC執行時間的擴展,以支援C99功能。 MinGW的所有軟體都可以在64位元Windows平台上運作。

Atom編輯器mac版下載
最受歡迎的的開源編輯器

mPDF
mPDF是一個PHP庫,可以從UTF-8編碼的HTML產生PDF檔案。原作者Ian Back編寫mPDF以從他的網站上「即時」輸出PDF文件,並處理不同的語言。與原始腳本如HTML2FPDF相比,它的速度較慢,並且在使用Unicode字體時產生的檔案較大,但支援CSS樣式等,並進行了大量增強。支援幾乎所有語言,包括RTL(阿拉伯語和希伯來語)和CJK(中日韓)。支援嵌套的區塊級元素(如P、DIV),

SublimeText3 Mac版
神級程式碼編輯軟體(SublimeText3)