对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开发vim插件及心得分享
Nov 04 Python
python处理图片之PIL模块简单使用方法
May 11 Python
python 编程之twisted详解及简单实例
Jan 28 Python
最近Python有点火? 给你7个学习它的理由!
Jun 26 Python
python操作列表的函数使用代码详解
Dec 28 Python
python方法生成txt标签文件的实例代码
May 10 Python
Python编程在flask中模拟进行Restful的CRUD操作
Dec 28 Python
PyQt5实现类似别踩白块游戏
Jan 24 Python
Python3.5以上版本lxml导入etree报错的解决方案
Jun 26 Python
PyTorch预训练的实现
Sep 18 Python
wxPython电子表格功能wx.grid实例教程
Nov 19 Python
使用Python合成图片的实现代码(图片添加个性化文本,图片上叠加其他图片)
Apr 30 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
提高define性能的php扩展hidef的安装和使用
2011/06/14 PHP
php中实现用数组妩媚地生成要执行的sql语句
2015/07/10 PHP
php $_SESSION会员登录实例分享
2021/01/19 PHP
PHP如何解决微信文章图片防盗链
2020/12/09 PHP
Javascript Math对象
2009/08/13 Javascript
javascript移动设备Web开发中对touch事件的封装实例
2014/06/05 Javascript
JavaScript获取图片真实大小代码实例
2014/09/24 Javascript
png在IE6 下无法透明的解决方法汇总
2015/05/21 Javascript
js游戏人物上下左右跑步效果代码分享
2015/08/28 Javascript
Easyui Treegrid改变默认图标的方法
2016/04/29 Javascript
JS使用onerror捕获异常示例
2016/08/03 Javascript
vue 怎么创建组件及组件使用方法
2017/07/27 Javascript
vue2.0 better-scroll 实现移动端滑动的示例代码
2018/01/25 Javascript
jQuery代码优化方法总结
2018/01/29 jQuery
ES6基础之 Promise 对象用法实例详解
2019/08/22 Javascript
深入学习Vue nextTick的用法及原理
2019/10/08 Javascript
微信小程序开发中var that =this的用法详解
2020/01/18 Javascript
vue.js click点击事件获取当前元素对象的操作
2020/08/07 Javascript
JavaScript 如何在浏览器中使用摄像头
2020/12/02 Javascript
python中使用OpenCV进行人脸检测的例子
2014/04/18 Python
python内置函数:lambda、map、filter简单介绍
2017/11/16 Python
Python+tkinter模拟“记住我”自动登录实例代码
2018/01/16 Python
python3学习之Splash的安装与实例教程
2018/07/09 Python
Python图像处理之颜色的定义与使用分析
2019/01/03 Python
Python利用requests模块下载图片实例代码
2019/08/12 Python
使用py-spy解决scrapy卡死的问题方法
2020/09/29 Python
python 根据列表批量下载网易云音乐的免费音乐
2020/12/03 Python
Python爬虫爬取ts碎片视频+验证码登录功能
2021/02/22 Python
CSS3实现粒子旋转伸缩加载动画
2016/04/22 HTML / CSS
CSS3中各种颜色属性的使用教程
2016/05/17 HTML / CSS
Bowflex美国官方网站:高级家庭健身器材
2017/12/22 全球购物
信息部岗位职责
2013/11/12 职场文书
企业出纳岗位职责
2014/03/12 职场文书
实习护士自荐信
2014/06/21 职场文书
整改报告怎么写
2014/11/06 职场文书
SpringCloud的JPA连接PostgreSql的教程
2021/06/26 Java/Android