对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 相关文章推荐
仅用500行Python代码实现一个英文解析器的教程
Apr 02 Python
python基础教程之Filter使用方法
Jan 17 Python
Pycharm学习教程(3) 代码运行调试
May 03 Python
pycharm重置设置,恢复默认设置的方法
Oct 22 Python
在Python中分别打印列表中的每一个元素方法
Nov 07 Python
python抖音表白程序源代码
Apr 07 Python
用Python实现将一张图片分成9宫格的示例
Jul 05 Python
对Django外键关系的描述
Jul 26 Python
python异步编程 使用yield from过程解析
Sep 25 Python
nginx+uwsgi+django环境搭建的方法步骤
Nov 25 Python
Python: tkinter窗口屏幕居中,设置窗口最大,最小尺寸实例
Mar 04 Python
解决阿里云邮件发送不能使用25端口问题
Aug 07 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
thinkphp5框架API token身份验证功能示例
2019/05/21 PHP
php7连接MySQL实现简易查询程序的方法
2020/10/13 PHP
静态的动态续篇之来点XML
2006/08/15 Javascript
用jscript实现新建和保存一个word文档
2007/06/15 Javascript
让JavaScript 轻松支持函数重载 (Part 1 - 设计)
2009/08/04 Javascript
jQuery 常见操作实现方式和常用函数方法总结
2011/05/06 Javascript
javascript中的startWith和endWith的几种实现方法
2013/05/07 Javascript
$.get获取一个文件的内容示例代码
2013/09/11 Javascript
jQuery实现流动虚线框的方法
2015/01/29 Javascript
jQuery对指定元素中指定字符串进行替换的方法
2015/03/17 Javascript
Javascript节点关系实例分析
2015/05/15 Javascript
JS闭包、作用域链、垃圾回收、内存泄露相关知识小结
2016/05/16 Javascript
js中json处理总结之JSON.parse
2016/10/14 Javascript
JavaScript 监控微信浏览器且自带返回按钮时间
2016/11/27 Javascript
jQuery中弹出iframe内嵌页面元素到父页面并全屏化的实例代码
2016/12/27 Javascript
微信小程序 小程序制作及动画(animation样式)详解
2017/01/06 Javascript
bootstrap3-dialog-master模态框使用详解
2017/08/22 Javascript
vue2.0与bootstrap3实现列表分页效果
2017/11/28 Javascript
[32:26]EG vs IG 2018国际邀请赛小组赛BO2 第一场 8.16
2018/08/17 DOTA
python并发2之使用asyncio处理并发
2017/12/21 Python
利用Python在一个文件的头部插入数据的实例
2018/05/02 Python
Python获取系统所有进程PID及进程名称的方法示例
2018/05/24 Python
Tensorflow使用tfrecord输入数据格式
2018/06/19 Python
pandas通过字典生成dataframe的方法步骤
2019/07/23 Python
Python实现代码统计工具
2019/09/19 Python
为什么说python更适合树莓派编程
2020/07/20 Python
[原创]赚疯了!转手立赚800+?大佬的python「抢茅台脚本」使用教程
2021/01/12 Python
python des,aes,rsa加解密的实现
2021/01/16 Python
一套VC试题
2015/01/23 面试题
授权委托书(公民个人适用)
2014/09/19 职场文书
学校三八妇女节活动总结
2015/02/06 职场文书
2016年万圣节活动总结
2016/04/05 职场文书
go类型转换及与C的类型转换方式
2021/05/05 Golang
Python基于百度API识别并提取图片中文字
2021/06/27 Python
MySQL 数据类型详情
2021/11/11 MySQL
Pandas 数据编码的十种方法
2022/04/20 Python