对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多线程threading.Lock锁用法实例
Nov 01 Python
python如何求解两数的最大公约数
Sep 27 Python
python使用suds调用webservice接口的方法
Jan 03 Python
numpy基础教程之np.linalg
Feb 12 Python
python求最大值最小值方法总结
Jun 25 Python
python3实现斐波那契数列(4种方法)
Jul 15 Python
Python 类方法和实例方法(@classmethod),静态方法(@staticmethod)原理与用法分析
Sep 20 Python
tensorflow如何继续训练之前保存的模型实例
Jan 21 Python
解决 jupyter notebook 回车换两行问题
Apr 15 Python
pandas DataFrame运算的实现
Jun 14 Python
python怎么删除缓存文件
Jul 19 Python
python如何实现图片压缩
Sep 11 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 array_intersect比array_diff快(附详细的使用说明)
2011/07/03 PHP
PHP+swoole实现简单多人在线聊天群发
2016/01/19 PHP
php写入mysql中文乱码的实例解决方法
2019/09/17 PHP
JS 密码强度验证(兼容IE,火狐,谷歌)
2010/03/15 Javascript
JScript分割字符串示例代码
2013/09/04 Javascript
jquery Ajax 实现加载数据前动画效果的示例代码
2014/02/07 Javascript
jquery实现用户信息修改验证输入方法汇总
2015/07/18 Javascript
Grunt入门教程(自动任务运行器)
2015/08/06 Javascript
Javascript自执行匿名函数(function() { })()的原理浅析
2016/05/15 Javascript
ionic实现可滑动的tab选项卡切换效果
2020/04/15 Javascript
基于chosen插件实现人员选择树搜索自动筛选功能
2016/09/24 Javascript
Javascript 实现计算器时间功能详解及实例(二)
2017/01/08 Javascript
使用vue制作FullPage页面滚动效果
2017/08/21 Javascript
Cpage.js给组件绑定事件的实现代码
2017/08/31 Javascript
arcgis for js栅格图层叠加(Raster Layer)问题
2017/11/22 Javascript
NodeJs项目中关闭ESLint的方法
2018/08/09 NodeJs
vuex的module模块用法示例
2018/11/12 Javascript
javascrpt密码强度校验函数详解
2020/03/18 Javascript
JS运算符优先级与表达式示例详解
2020/09/04 Javascript
浅谈Vue使用Cascader级联选择器数据回显中的坑
2020/10/31 Javascript
[01:12]快闪回顾DOTA2亚洲邀请赛(DAC) 静候2018新征程开启
2018/03/11 DOTA
Python Django Cookie 简单用法解析
2019/08/13 Python
解决django中form表单设置action后无法回到原页面的问题
2020/03/13 Python
班组长工作职责
2013/12/25 职场文书
诉讼财产保全担保书
2014/05/20 职场文书
宣传工作经验材料
2014/06/02 职场文书
敬老院标语
2014/06/27 职场文书
舞蹈教育学专业求职信
2014/06/29 职场文书
学生意外伤害赔偿协议书
2014/09/17 职场文书
党员教师个人对照检查材料(群众路线)
2014/09/26 职场文书
2015年健康教育工作总结
2015/04/10 职场文书
硕士学位申请报告
2015/05/15 职场文书
2016高考寄语集锦
2015/12/04 职场文书
导游词创作书写原则以及开场白技巧怎么学?
2019/09/25 职场文书
基于Python的EasyGUI学习实践
2021/05/07 Python
详解Go语言中配置文件使用与日志配置
2022/06/01 Golang