對(duì)python 中class與變量的使用方法詳解
python中的變量定義是很靈活的,很容易搞混淆,特別是對(duì)于class的變量的定義,如何定義使用類(lèi)里的變量是我們維護(hù)代碼和保證代碼穩(wěn)定性的關(guān)鍵。
#!/usr/bin/python #encoding:utf-8 global_variable_1 = 'global_variable' class MyClass(): class_var_1 = 'class_val_1' # define class variable here def __init__(self, param): self.object_var_1 = param # define object variable here self.object_var_2 = 'object_val_2' # define object variable here self.object_func3() def object_func1(self, param): local_var_1 = param # define lcoal variable here local_var_2 = 'local_val_2' # define local variable here self.internal_var_1 = 'internal_val_1' # define internal variable here print(local_var_1) # we can use local variable of current here print(local_var_2) # we can use local variable of current here print(MyClass.class_var_1) # we can use class variable here, but you have using class name ass prefix print(self.class_var_1) # we can use class variable as object variable here print(self.object_var_1) # we can use object variable here print(self.object_var_2) # we can use object variable here print(self.internal_var_1) # we can use internal variable here #print(local_var_3) # we can't use local variable in another function print(global_variable_1) # we can use global variable here def object_func2(self, param='func_val_1'): local_var_3 = param # define local variable here print(local_var_3) # we can use lcoal variable here print(self.internal_var_1) # we can use internal variable defined in class_func1, but you have to call class_func1 first print(MyClass.class_var_1) # we can use class variable here, but you have using class name ass prefix print(self.class_var_1) # we can class variable here print(self.object_var_1) # we can use object variable here print(self.object_var_2) # we can use object variable here print(global_variable_1) # we can use global variable here def object_func3(self, param='func_val_1'): self.object_var_3 = param # because this function called in construction function, so this is defined as object variable, not internal variable self.object_var_4 = 'object_val_4' # because this function called in construction function, so this is defined as object variable, not internal variable print(global_variable_1) # we can use global variable here # define class function def class_func4(): print(MyClass.class_var_1) print(global_variable_1) # we can use global variable here if __name__ == '__main__': myObject = MyClass('object_val_1') print(MyClass.class_var_1) # we can use class variable directly here #print(MyClass.object_var_1) # we can't use object variable here print(myObject.object_var_1) # we can use object variable here print(myObject.object_var_2) # we can use object variable here print(myObject.object_var_3) # we can use object variable here print(myObject.object_var_4) # we can use object variable here #print(myObject.internal_var_1) # we can't use internal variable as object variable here MyClass.class_func4() # we can use class function here #MyClass.object_func2(myObject, 'local_var_3') # internal variable can't be used in this function myObject.object_func1('local_var_1') # call first function myObject.object_func2('local_var_3') # call second function print(global_variable_1) # we can use global variable here
簡(jiǎn)單的寫(xiě)了個(gè)測(cè)試小程序,枚舉了各種情況,沒(méi)有辦法全部枚舉,但大部分情況應(yīng)該都已經(jīng)包含了。
1. 類(lèi)變量:能夠通過(guò)類(lèi)名或者object的self來(lái)訪問(wèn)到,在類(lèi)的內(nèi)部和外部均可達(dá),比如class_var_1
2. 對(duì)象變量:可以通過(guò)對(duì)象的self來(lái)使用的變量,通過(guò)constructor一路走向去的的self初次被賦值的變量都會(huì)成為對(duì)象變量,比如object_var_1, object_var_2, object_var_3, object_var_4
3. 內(nèi)部變量:可以在函數(shù)中定義,并加上self前綴,在初次調(diào)用過(guò)定義的函數(shù)后,就可以在后面的對(duì)象的函數(shù)中被使用,比如internal_var_1
4. 局部變量:在函數(shù)內(nèi)部定義,并使用的變量,在使用完之后就會(huì)被回收對(duì)類(lèi)及object不可見(jiàn)
5. 全局變量:定義在類(lèi)或者函數(shù)外部,作用域在變量被定義之后的任意代碼段,比如:global_var_1
以上是基于我自己的測(cè)試得到的結(jié)論,如果有不對(duì)的地方,可以幫忙指正。
這篇對(duì)python 中class與變量的使用方法詳解就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
在前女友婚禮上,用Python破解了現(xiàn)場(chǎng)的WIFI還把名稱(chēng)改成了
今日重點(diǎn):① python暴力拿下WiFi密碼,②python拿下路由器管理頁(yè)面,文中有非常詳細(xì)的代碼示例,干貨滿(mǎn)滿(mǎn),,需要的朋友可以參考下2021-05-05Python Selenium實(shí)現(xiàn)無(wú)可視化界面過(guò)程解析
這篇文章主要介紹了Python Selenium實(shí)現(xiàn)無(wú)可視化界面過(guò)程解析,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-08-08小白學(xué)Python之實(shí)現(xiàn)OCR識(shí)別
將圖片翻譯成文字一般被稱(chēng)為光學(xué)文字識(shí)別(Optical Character Recognition,OCR),這篇文章主要給大家介紹了關(guān)于Python實(shí)現(xiàn)OCR識(shí)別的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),需要的朋友可以參考下2021-08-08Python代碼調(diào)用執(zhí)行shell踩坑解決
這篇文章主要為大家介紹了Python代碼調(diào)用執(zhí)行shell,踩過(guò)的坑解決方法,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-06-06解讀opencv->tensorrt的數(shù)據(jù)排列方式
這篇文章主要介紹了解讀opencv->tensorrt的數(shù)據(jù)排列方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-02-02Python 面向?qū)ο笾庋b、繼承、多態(tài)操作實(shí)例分析
這篇文章主要介紹了Python 面向?qū)ο笾庋b、繼承、多態(tài)操作,結(jié)合實(shí)例形式分析了Python面相對(duì)象程序設(shè)計(jì)中封裝、繼承、多態(tài)相關(guān)操作技巧與使用注意事項(xiàng),需要的朋友可以參考下2019-11-11Python發(fā)展簡(jiǎn)史 Python來(lái)歷
在本文里我們給大家分享了關(guān)于Python的來(lái)歷以及Python相關(guān)的發(fā)展內(nèi)容,有興趣的讀者們閱讀下。2019-05-05Python3使用騰訊云文字識(shí)別(騰訊OCR)提取圖片中的文字內(nèi)容實(shí)例詳解
這篇文章主要介紹了Python3使用騰訊云文字識(shí)別(騰訊OCR)提取圖片中的文字內(nèi)容方法詳解,需要的朋友可以參考下2020-02-02