日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > python >内容正文

python

python中class变量_对python 中class与变量的使用方法详解

發布時間:2024/10/14 python 71 豆豆
生活随笔 收集整理的這篇文章主要介紹了 python中class变量_对python 中class与变量的使用方法详解 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

python中的變量定義是很靈活的,很容易搞混淆,特別是對于class的變量的定義,如何定義使用類里的變量是我們維護代碼和保證代碼穩定性的關鍵。

#!/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

簡單的寫了個測試小程序,枚舉了各種情況,沒有辦法全部枚舉,但大部分情況應該都已經包含了。

1. 類變量:能夠通過類名或者object的self來訪問到,在類的內部和外部均可達,比如class_var_1

2. 對象變量:可以通過對象的self來使用的變量,通過constructor一路走向去的的self初次被賦值的變量都會成為對象變量,比如object_var_1, object_var_2, object_var_3, object_var_4

3. 內部變量:可以在函數中定義,并加上self前綴,在初次調用過定義的函數后,就可以在后面的對象的函數中被使用,比如internal_var_1

4. 局部變量:在函數內部定義,并使用的變量,在使用完之后就會被回收對類及object不可見

5. 全局變量:定義在類或者函數外部,作用域在變量被定義之后的任意代碼段,比如:global_var_1

以上是基于我自己的測試得到的結論,如果有不對的地方,可以幫忙指正。

這篇對python 中class與變量的使用方法詳解就是小編分享給大家的全部內容了,希望能給大家一個參考,也希望大家多多支持我們。

本文標題: 對python 中class與變量的使用方法詳解

本文地址: http://www.cppcns.com/jiaoben/python/263751.html

總結

以上是生活随笔為你收集整理的python中class变量_对python 中class与变量的使用方法详解的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。

主站蜘蛛池模板: 国产国产乱老熟女视频网站97 | 四虎最新域名 | 永久免费54看片 | 亚洲无人区码一码二码三码 | 日本美女一级视频 | 最好看的日本字幕mv视频大全 | 日本精品视频一区 | 日本99视频| 久草国产视频 | 国产精品一区二区在线免费观看 | 91视频在线观看视频 | 欧美大片高清免费观看 | 蜜臀av色欲a片无码精品一区 | 国产区福利 | 美女伊人网 | 久久久久久av无码免费网站 | 国偷自拍第113页 | 久草播放 | 麻豆av电影在线 | 女警白嫩翘臀呻吟迎合 | 女人床技48动态图 | 天天透天天干 | 国产超级av | 欧美老肥熟 | 99视频久久 | 波多野结衣中文字幕一区二区 | 看片网址国产福利av中文字幕 | 日韩激情免费 | 浮力影院草草 | www插插插 | 少妇喷水在线观看 | 啪啪av网站| 7799精品视频天天看 | 欧美熟妇另类久久久久久多毛 | 四虎精品一区 | 蜜桃视频日韩 | 男人日女人网站 | av观看免费 | 国产第一页视频 | 亚洲三级视频在线观看 | 日韩一区二区视频在线 | 九一在线视频 | 国产宾馆实践打屁股91 | 亚洲系列中文字幕 | 中文国产视频 | 久久二 | 日本护士╳╳╳hd少妇 | 国产精品ⅴa有声小说 | 51啪影院 | 国产一级片av | 日本一区二区免费在线 | 图片区亚洲色图 | 97se在线视频 | 一区二区黄色片 | 九九久久99 | 欧美人xxxx | 精品国偷自产国产一区 | 日韩中文视频 | 色综合图片区 | 国产精品久久久不卡 | 国产在线观看免费视频今夜 | 成人免费看高清电影在线观看 | 国产精品女人久久久 | 日本一本在线视频 | 国产人人爱 | 99精品欧美一区二区蜜桃免费 | 99国内揄拍国内精品人妻免费 | 亚日韩av| 我色综合 | 综合久久久久久 | 夜夜操夜夜爽 | 色老头一区二区三区在线观看 | 爱福利视频一区二区 | 久草成人 | 熟妇大屁股一区二区三区视频 | 午夜av福利| 91高清在线免费观看 | 天堂av免费看 | 捆绑黑丝美女 | 草草在线观看 | 欧洲做受高潮欧美裸体艺术 | 欧美10p | 欧美色一区二区三区在线观看 | 香港三级韩国三级日本三级 | 久久99热人妻偷产国产 | 波多野结衣导航 | 伊伊综合网 | 国产一区二区波多野结衣 | 新久草视频 | 国产嫩bbwbbw高潮 | 久久艳片www.17c.com | 乱子伦一区 | 少妇一级淫片免费观看 | 黄色片子网站 | 激情图片在线观看 | 交专区videossex农村 | 男人天堂伊人 | 女仆裸体打屁屁羞羞免费 | 欧美日韩一区二区三区国产精品成人 |