对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 相关文章推荐
使用graphics.py实现2048小游戏
Mar 10 Python
在Python中进行自动化单元测试的教程
Apr 15 Python
Python基于分水岭算法解决走迷宫游戏示例
Sep 26 Python
python中os和sys模块的区别与常用方法总结
Nov 14 Python
解决python xlrd无法读取excel文件的问题
Dec 25 Python
用python实现刷点击率的示例代码
Feb 21 Python
python实现列表的排序方法分享
Jul 01 Python
python3 实现的对象与json相互转换操作示例
Aug 17 Python
python自动化测试无法启动谷歌浏览器问题
Oct 10 Python
Python pytesseract验证码识别库用法解析
Jun 29 Python
解决TensorFlow调用Keras库函数存在的问题
Jul 06 Python
python如何绘制疫情图
Sep 16 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数据类型判断函数有哪些
2013/09/23 PHP
PHP函数getenv简介和使用实例
2014/05/12 PHP
php中call_user_func函数使用注意事项
2014/11/21 PHP
验证token、回复图文\文本、推送消息的实用微信类php代码
2016/06/28 PHP
php基于curl实现随机ip地址抓取内容的方法
2016/10/11 PHP
js 巧妙去除数组中的重复项
2010/01/25 Javascript
jQuery源码中的chunker 正则过滤符分析
2012/07/31 Javascript
jQuery创建平滑的页面滚动(顶部或底部)
2013/02/26 Javascript
基于jquery实现的文字向上跑动类似跑马灯的效果
2014/06/17 Javascript
jQuery可见性过滤器:hidden和:visibility用法实例
2015/06/24 Javascript
jQuery1.9.1源码分析系列(十六)ajax之ajax框架
2015/12/04 Javascript
详解iframe与frame的区别
2016/01/13 Javascript
javascript 面向对象function详解及实例代码
2017/02/28 Javascript
jQuery插件HighCharts绘制2D饼图效果示例【附demo源码下载】
2017/03/21 jQuery
angular2中router路由跳转navigate的使用与刷新页面问题详解
2017/05/07 Javascript
Vue2.0 事件的广播与接收(观察者模式)
2018/03/14 Javascript
Vue自定义指令封装节流函数的方法示例
2018/07/09 Javascript
iView框架问题整理小结
2018/10/16 Javascript
详解jQuery如何实现模糊搜索
2019/05/10 jQuery
详解Vue中CSS样式穿透问题
2019/09/12 Javascript
[58:23]LGD vs TNC 2019国际邀请赛小组赛 BO2 第一场 8.15
2019/08/16 DOTA
pycharm 使用心得(三)Hello world!
2014/06/05 Python
python提取具有某种特定字符串的行数据方法
2018/12/11 Python
python利用小波分析进行特征提取的实例
2019/01/09 Python
python3的print()函数的用法图文讲解
2019/07/16 Python
python读取与处理netcdf数据方式
2020/02/14 Python
基于Python的接口自动化读写excel文件的方法
2021/01/15 Python
应届生财务会计求职信
2013/11/05 职场文书
毕业生求职的求职信
2013/12/05 职场文书
高中美术教学反思
2014/01/19 职场文书
生日寿宴答谢词
2014/01/19 职场文书
2015学校师德师风工作总结
2015/04/22 职场文书
python实现三阶魔方还原的示例代码
2021/04/28 Python
python turtle绘制多边形和跳跃和改变速度特效
2022/03/16 Python
Redis 哨兵机制及配置实现
2022/03/25 Redis
唤醒紫霞仙子,携手再游三界!大话手游X《大话西游》电影合作专属剧情任务
2022/04/03 其他游戏