对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爬虫之BeautifulSoup 使用select方法详解
Oct 23 Python
python编程之requests在网络请求中添加cookies参数方法详解
Oct 25 Python
Python reduce()函数的用法小结
Nov 15 Python
Python实现PS图像调整颜色梯度效果示例
Jan 25 Python
利用selenium爬虫抓取数据的基础教程
Jun 10 Python
python实现截取屏幕保存文件,删除N天前截图的例子
Aug 27 Python
Python 复平面绘图实例
Nov 21 Python
Python中if有多个条件处理方法
Feb 26 Python
python实现将range()函数生成的数字存储在一个列表中
Apr 02 Python
对Matlab中共轭、转置和共轭装置的区别说明
May 11 Python
django admin管理工具自定义时间区间筛选器DateRangeFilter介绍
May 19 Python
Python爬虫入门教程01之爬取豆瓣Top电影
Jan 24 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
自制短波长线天线频率预选器 - 成功消除B2K之流的镜像
2021/03/02 无线电
php DOS攻击实现代码(附如何防范)
2012/05/29 PHP
php魔术方法与魔术变量、内置方法与内置变量的深入分析
2013/06/03 PHP
解析php file_exists无效的解决办法
2013/06/26 PHP
php通过字符串调用函数示例
2014/03/02 PHP
PHP实现根据设备类型自动跳转相应页面的方法
2014/07/24 PHP
php使用指定字符列表生成随机字符串的方法
2015/04/18 PHP
基于jquery的lazy loader插件实现图片的延迟加载[简单使用]
2011/05/07 Javascript
JS 数字转换研究总结
2013/12/26 Javascript
Javascript学习笔记之 对象篇(三) : hasOwnProperty
2014/06/24 Javascript
javascript实现随机读取数组的方法
2015/08/03 Javascript
jQuery实现ctrl+enter(回车)提交表单
2015/10/19 Javascript
玩转JavaScript OOP - 类的实现详解
2016/06/08 Javascript
jQuery版AJAX简易封装代码
2016/09/14 Javascript
vue+vue-router转场动画的实例代码
2018/09/01 Javascript
Bootstrap4 gulp 配置详解
2019/01/06 Javascript
详解单页面路由工程使用微信分享及二次分享解决方案
2019/02/22 Javascript
在layui中对table中的数据进行判断(0、1)转换为提示信息的方法
2019/09/28 Javascript
Node.js API详解之 net模块实例分析
2020/05/18 Javascript
python求crc32值的方法
2014/10/05 Python
Python中zfill()方法的使用教程
2015/05/20 Python
python冒泡排序简单实现方法
2015/07/09 Python
python更新列表的方法
2015/07/28 Python
Python中在脚本中引用其他文件函数的实现方法
2016/06/23 Python
关于win10在tensorflow的安装及在pycharm中运行步骤详解
2020/03/16 Python
基于python图书馆管理系统设计实例详解
2020/08/05 Python
jupyter notebook 写代码自动补全的实现
2020/11/02 Python
毕业生毕业总结的自我评价范文
2013/11/02 职场文书
退伍老兵事迹材料
2014/01/31 职场文书
兴趣小组活动总结
2014/05/05 职场文书
幼儿园运动会口号
2014/06/07 职场文书
医院我们的节日活动实施方案
2014/08/22 职场文书
军事理论课感想
2015/08/11 职场文书
五年级语文教学反思
2016/03/03 职场文书
Nginx+Tomcat实现负载均衡、动静分离的原理解析
2021/03/31 Servers
GTX1660显卡搭配显示器推荐
2022/04/19 数码科技