对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实现Tab自动补全和历史命令管理的方法
Mar 12 Python
简述Python中的进程、线程、协程
Mar 18 Python
Tensorflow简单验证码识别应用
May 25 Python
matplotlib设置legend图例代码示例
Dec 19 Python
pygame实现俄罗斯方块游戏(基础篇1)
Oct 29 Python
numpy:np.newaxis 实现将行向量转换成列向量
Nov 30 Python
python plotly画柱状图代码实例
Dec 13 Python
Python 实现将numpy中的nan和inf,nan替换成对应的均值
Jun 08 Python
Python Pillow(PIL)库的用法详解
Sep 19 Python
Python在centos7.6上安装python3.9的详细教程(默认python版本为2.7.5)
Oct 15 Python
python request 模块详细介绍
Nov 10 Python
Django REST Framework 分页(Pagination)详解
Nov 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
打造计数器DIY三步曲(上)
2006/10/09 PHP
浅谈PHP5.6 与 PHP7.0 区别
2019/10/09 PHP
PHP 图片处理
2020/09/16 PHP
js 屏蔽鼠标右键脚本附破解方法
2009/12/03 Javascript
浅谈类似于(function(){}).call()的js语句
2015/03/30 Javascript
JavaScript的React Web库的理念剖析及基础上手指南
2016/05/10 Javascript
javascript获取网页各种高宽及位置的方法总结
2016/07/27 Javascript
AngularJS通过$location获取及改变当前页面的URL
2016/09/23 Javascript
js实现砖头在页面拖拉效果
2020/11/20 Javascript
JavaScript在form表单中使用button按钮实现submit提交方法
2017/01/23 Javascript
JavaScript中利用构造器函数模拟类的方法
2017/02/16 Javascript
使用MUI框架模拟手机端的下拉刷新和上拉加载功能
2017/09/04 Javascript
Vue.js devtool插件安装后无法使用的解决办法
2017/11/27 Javascript
mui框架 页面无法滚动的解决方法(推荐)
2018/01/25 Javascript
vue项目中引入noVNC远程桌面的方法
2018/03/05 Javascript
vue项目中跳转到外部链接的实例讲解
2018/09/20 Javascript
JavaScript解析机制与闭包原理实例详解
2019/03/08 Javascript
微信小程序人脸识别功能代码实例
2019/05/07 Javascript
JS函数进阶之继承用法实例分析
2020/01/15 Javascript
JavaScript 空间坐标的使用
2020/08/19 Javascript
原生js 实现表单验证功能
2021/02/08 Javascript
[02:35]DOTA2英雄基础教程 末日使者
2013/12/04 DOTA
[38:27]完美世界DOTA2联赛PWL S2 Forest vs FTD.C 第二场 11.26
2020/11/30 DOTA
python编写简单爬虫资料汇总
2016/03/22 Python
python中类和实例如何绑定属性与方法示例详解
2017/08/18 Python
Python 函数返回值的示例代码
2019/03/11 Python
【python】matplotlib动态显示详解
2019/04/11 Python
浅析Python3中的对象垃圾收集机制
2019/06/06 Python
迪拜航空官方网站:flydubai
2017/04/20 全球购物
Book Depository欧盟:一家领先的国际图书零售商
2019/05/21 全球购物
党的群众路线教育实践活动实施方案
2014/10/31 职场文书
优秀教师先进事迹材料
2014/12/15 职场文书
2015年园林绿化工作总结
2015/05/23 职场文书
药房管理制度范本
2015/08/06 职场文书
8g内存用python读取10文件_面试题-python 如何读取一个大于 10G 的txt文件?
2021/05/28 Python
Win10 heic文件怎么打开 ? Win10 heic文件打开教程
2022/04/06 数码科技