日韩性视频-久久久蜜桃-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与变量的使用方法详解的全部內容,希望文章能夠幫你解決所遇到的問題。

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

主站蜘蛛池模板: 国产精品第一区 | 日本在线视频免费观看 | 麻豆视频传媒入口 | 亚洲一区二区91 | 18视频在线观看男男 | 吃瓜网今日吃瓜 热门大瓜 色婷在线 | 久久h| 成人av动漫| 插插宗合网 | 妻色成人网 | 四虎最新站名点击进入 | 女人扒开屁股让男人捅 | 黄色一级视频免费看 | 中出视频在线观看 | 日本精品视频一区二区三区 | 日韩免费成人 | 成人精品在线观看视频 | 国产精品日韩欧美大师 | 国产高清免费在线播放 | 天天舔夜夜操 | 成人网在线视频 | 白嫩日本少妇做爰 | 加勒比一区二区三区 | 亚洲av综合色区无码另类小说 | 伊人久久大香线 | 少妇高潮一区二区三区四区 | 国产乱一区二区三区 | 亚洲国产一区二区三区在线观看 | ,亚洲人成毛片在线播放 | 伊人春色在线 | 久久久久久久久久久久久av | 2023天天操 | 伊人久久亚洲综合 | 婷婷在线免费 | 久久久久亚洲av无码专区桃色 | mm131丰满少妇人体欣赏图 | 每日在线观看av | 日本国产在线观看 | 日韩av在线看免费观看 | 奇米第四色首页 | 九九99精品视频 | 精品一区二区日韩 | 人妻毛片 | 涩涩天堂| 国产精品久久av无码一区二区 | h视频在线免费看 | www.99re7.com| 免费看日产一区二区三区 | 久久精品网址 | 欧美成人精品一区二区综合免费 | 亚洲免费av在线 | 99re视频在线播放 | 在线看国产视频 | www.狠狠干 | 最新中文字幕在线观看视频 | 啦啦啦av| 成人毛片a | 人妻熟女一区二区三区 | 高清日韩av | 性感美女一区二区三区 | 国产精品2019 | xvideos永久免费入口 | 精品不卡视频 | 成人久久久久久久 | 日韩三区在线 | 九色在线观看 | 青青草这里只有精品 | 夜夜干夜夜 | 玖草视频在线观看 | 真人毛片视频 | av解说在线观看 | 国产在线视频资源 | japanese强行粗暴 | 亚洲天堂aaa | 亚洲国产高清国产精品 | 久热热 | chinese精品自拍hd | 国产女主播av | 丰满少妇aaaaaa爰片毛片 | 中文字幕在线观看三区 | 久久精品国产免费看久久精品 | 69日本xxxxxxxx96 | 69国产 | 性爱免费视频 | 国产一区日本 | 婷婷色网站 | 免费爱爱网站 | 亚洲大片免费观看 | 奴性白洁会所调教 | 亚洲精品五月天 | 中国zzji女人高潮免费 | 50度灰在线 | 欧美国产一级片 | 白石茉莉奈番号 | a视频在线看 | 亚洲一区二区三区在线视频 | 日韩精品无码一区二区 | 国产精彩视频一区 | 激情文学久久 |