对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 获取文件列表(或是目录例表)
Mar 25 Python
Python中不同进制互相转换(二进制、八进制、十进制和十六进制)
Apr 05 Python
Python使用matplotlib实现在坐标系中画一个矩形的方法
May 20 Python
python条件变量之生产者与消费者操作实例分析
Mar 22 Python
Python探索之pLSA实现代码
Oct 25 Python
Python动态导入模块的方法实例分析
Jun 28 Python
Flask之请求钩子的实现
Dec 23 Python
计算机二级python学习教程(1) 教大家如何学习python
May 16 Python
详解mac python+selenium+Chrome 简单案例
Nov 08 Python
如何基于Python获取图片的物理尺寸
Nov 25 Python
Python Tkinter Entry和Text的添加与使用详解
Mar 04 Python
PYTHON使用Matplotlib去实现各种条形图的绘制
Mar 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
一个域名查询的程序
2006/10/09 PHP
在普通HTTP上安全地传输密码
2007/07/21 PHP
PHP实现原比例生成缩略图的方法
2016/02/03 PHP
没有document.getElementByName方法
2013/08/19 Javascript
Jquery通过JSON字符串创建JSON对象
2014/08/24 Javascript
Javascript实现苹果悬浮虚拟按钮
2016/04/10 Javascript
Vue.js对象转换实例
2017/06/07 Javascript
Vue动态组件实例解析
2017/08/20 Javascript
jQuery实现table表格checkbox全选的方法分析
2018/07/04 jQuery
微信小程序授权登录及解密unionId出错的方法
2018/09/26 Javascript
jQuery实现提交表单时不提交隐藏div中input的方法
2019/10/08 jQuery
JavaScript 异步时序问题
2020/11/20 Javascript
[01:00:04]DOTA2上海特级锦标赛B组小组赛#1 Alliance VS Spirit第二局
2016/02/26 DOTA
python使用PyFetion来发送短信的例子
2014/04/22 Python
Python打印scrapy蜘蛛抓取树结构的方法
2015/04/08 Python
Python常用算法学习基础教程
2017/04/13 Python
Python类装饰器实现方法详解
2018/12/21 Python
python处理multipart/form-data的请求方法
2018/12/26 Python
关于Python 常用获取元素 Driver 总结
2019/11/24 Python
python模块和包的应用BASE_PATH使用解析
2019/12/14 Python
opencv resize图片为正方形尺寸的实现方法
2019/12/26 Python
详解python tkinter模块安装过程
2020/01/06 Python
python实现音乐播放器 python实现花框音乐盒子
2020/02/25 Python
Python 常用日期处理 -- calendar 与 dateutil 模块的使用
2020/09/02 Python
详解基于python的图像Gabor变换及特征提取
2020/10/26 Python
html5实现九宫格抽奖可固定抽中某项奖品
2020/06/15 HTML / CSS
德国汉莎航空中国官网: Lufthansa中国
2017/03/30 全球购物
Speedo澳大利亚官网:全球领先游泳品牌
2018/02/04 全球购物
Watch Station官方网站:世界一流的手表和智能手表
2020/01/05 全球购物
一套SQL笔试题
2016/08/14 面试题
小学生开学第一课活动方案
2014/03/27 职场文书
关于读书的演讲稿400字
2014/08/27 职场文书
小学优秀教师先进事迹材料
2014/12/16 职场文书
工程部主管岗位职责
2015/02/12 职场文书
正确使用MySQL update语句
2021/05/26 MySQL
JavaScript小技巧带你提升你的代码技能
2021/09/15 Javascript