日韩久久久精品,亚洲精品久久久久久久久久久,亚洲欧美一区二区三区国产精品 ,一区二区福利

對python 中class與變量的使用方法詳解

系統 1835 0

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與變量的使用方法詳解就是小編分享給大家的全部內容了,希望能給大家一個參考,也希望大家多多支持腳本之家。


更多文章、技術交流、商務合作、聯系博主

微信掃碼或搜索:z360901061

微信掃一掃加我為好友

QQ號聯系: 360901061

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。

【本文對您有幫助就好】

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描上面二維碼支持博主2元、5元、10元、自定義金額等您想捐的金額吧,站長會非常 感謝您的哦!!!

發表我的評論
最新評論 總共0條評論
主站蜘蛛池模板: 化德县| 环江| 东丽区| 基隆市| 厦门市| 遵义县| 固原市| 清苑县| 汉阴县| 鹤庆县| 龙门县| 饶平县| 岱山县| 吕梁市| 凤冈县| 涡阳县| 柞水县| 焉耆| 石河子市| 焦作市| 巴青县| 民丰县| 灵丘县| 陆川县| 商丘市| 梧州市| 紫金县| 双流县| 兴文县| 深水埗区| 法库县| 洛宁县| 咸宁市| 安阳市| 明光市| 读书| 阿拉尔市| 余干县| 朝阳区| 建阳市| 浪卡子县|