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


Posted in Python onJune 26, 2019

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 相关文章推荐
Python中为feedparser设置超时时间避免堵塞
Sep 28 Python
python统计一个文本中重复行数的方法
Nov 19 Python
Python函数式编程指南(一):函数式编程概述
Jun 24 Python
Python基于回溯法子集树模板解决马踏棋盘问题示例
Sep 11 Python
Python实现一个Git日志统计分析的小工具
Dec 14 Python
使用Python快乐学数学Github万星神器Manim简介
Aug 07 Python
春节到了 教你使用python来抢票回家
Jan 06 Python
Python使用type动态创建类操作示例
Feb 29 Python
python opencv进行图像拼接
Mar 27 Python
python实现数字炸弹游戏程序
Jul 17 Python
matplotlib之属性组合包(cycler)的使用
Feb 24 Python
一行代码python实现文件共享服务器
Apr 22 Python
python 机器学习之支持向量机非线性回归SVR模型
Jun 26 #Python
python机器学习库scikit-learn:SVR的基本应用
Jun 26 #Python
Python Numpy 实现交换两行和两列的方法
Jun 26 #Python
python 字典操作提取key,value的方法
Jun 26 #Python
通过PYTHON来实现图像分割详解
Jun 26 #Python
Flask模板引擎之Jinja2语法介绍
Jun 26 #Python
如何使用Python实现自动化水军评论
Jun 26 #Python
You might like
PHP iconv()函数字符编码转换的问题讲解
2019/03/22 PHP
JQuery 表格操作(交替显示、拖动表格行、选择行等)
2009/07/29 Javascript
基于jquery的多功能软键盘插件
2012/07/25 Javascript
JS中批量给元素绑定事件过程中的相关问题使用闭包解决
2013/04/15 Javascript
你未必知道的JavaScript和CSS交互的5种方法
2014/04/02 Javascript
js实现文章文字大小字号功能完整实例
2014/11/01 Javascript
js用拖动滑块来控制图片大小的方法
2015/02/27 Javascript
jQuery.position()方法获取不到值的安全替换方法
2015/03/13 Javascript
jQuery可见性过滤器:hidden和:visibility用法实例
2015/06/24 Javascript
在Node.js中使用Javascript Generators详解
2016/05/05 Javascript
js阻止浏览器默认行为的简单实例
2016/05/15 Javascript
深入理解jquery自定义动画animate()
2016/05/24 Javascript
JavaScript实现简单的拖动效果
2016/07/02 Javascript
省市区三级联动jquery实现代码
2020/04/15 Javascript
Node.js的特点详解
2017/02/03 Javascript
jQuery实现动态删除LI的方法
2017/05/30 jQuery
基于ExtJs在页面上window再调用Window的事件处理方法
2017/07/26 Javascript
Python3学习urllib的使用方法示例
2017/11/29 Python
Python3.6+Django2.0以上 xadmin站点的配置和使用教程图解
2019/06/04 Python
Python3.5以上版本lxml导入etree报错的解决方案
2019/06/26 Python
使用pandas读取文件的实现
2019/07/31 Python
Pytorch抽取网络层的Feature Map(Vgg)实例
2019/08/20 Python
django中的图片验证码功能
2019/09/18 Python
100行Python代码实现每天不同时间段定时给女友发消息
2019/09/27 Python
Pytorch 神经网络—自定义数据集上实现教程
2020/01/07 Python
Python urlopen()和urlretrieve()用法解析
2020/01/07 Python
Python异常原理及异常捕捉实现过程解析
2020/03/25 Python
Django封装交互接口代码
2020/07/12 Python
护理毕业生自荐信范文
2013/12/22 职场文书
乐观大学生的自我评价
2014/01/10 职场文书
幼儿园亲子活动方案
2014/01/29 职场文书
关于读书的活动方案
2014/08/14 职场文书
语文课外活动总结
2014/08/27 职场文书
2016会计专业自荐信范文
2016/01/28 职场文书
Java SSM配置文件案例详解
2021/08/30 Java/Android
海弦WR-800F
2022/04/05 无线电