对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程序设计入门(4)模块和包
Jun 16 Python
详解Python中with语句的用法
Apr 15 Python
浅谈python新手中常见的疑惑及解答
Jun 14 Python
python 创建一个空dataframe 然后添加行数据的实例
Jun 07 Python
基于python log取对数详解
Jun 08 Python
numpy使用fromstring创建矩阵的实例
Jun 15 Python
如何安装多版本python python2和python3共存以及pip共存
Sep 18 Python
Python 输出时去掉列表元组外面的方括号与圆括号的方法
Dec 24 Python
Python3视频转字符动画的实例代码
Aug 29 Python
python实现简易学生信息管理系统
Apr 05 Python
python 中Arduino串口传输数据到电脑并保存至excel表格
Oct 14 Python
Numpy中对向量、矩阵的使用详解
Oct 29 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 Google的translate API代码
2008/12/10 PHP
PHPEXCEL 使用小记
2013/01/06 PHP
深入PHP数据缓存的使用说明
2013/05/10 PHP
php伪静态验证码不显示的解决方案
2019/09/26 PHP
IE autocomplete internet explorer's autocomplete
2007/06/30 Javascript
Javascript String.replace的妙用
2009/09/08 Javascript
ASP中进行HTML数据及JS数据编码函数
2009/11/11 Javascript
使用jquery动态加载javascript以减少服务器压力
2012/10/29 Javascript
jQuery Mobile页面跳转后未加载外部JS原因分析及解决
2013/03/18 Javascript
JavaScript提高性能知识点汇总
2016/01/15 Javascript
JavaScript中关键字 in 的使用方法详解
2016/10/17 Javascript
微信小程序购物商城系统开发系列-工具篇的介绍
2016/11/21 Javascript
AngularJS日程表案例详解
2017/08/15 Javascript
Vue.js路由实现选项卡简单实例
2019/07/24 Javascript
Nodejs 识别图片类型的方法
2019/08/15 NodeJs
JavaScript实现图片上传并预览并提交ajax
2019/09/30 Javascript
小程序自定义弹框效果
2020/11/16 Javascript
使用vue3重构拼图游戏的实现示例
2021/01/25 Vue.js
[04:16]DOTA2全国高校联赛16强抽签
2018/05/02 DOTA
浅谈Python中的zip()与*zip()函数详解
2018/02/24 Python
Python实现批量读取图片并存入mongodb数据库的方法示例
2018/04/02 Python
Python3正则匹配re.split,re.finditer及re.findall函数用法详解
2018/06/11 Python
python for循环输入一个矩阵的实例
2018/11/14 Python
python:动态路由的Flask程序代码
2019/11/22 Python
python3.8与pyinstaller冲突问题的快速解决方法
2020/01/16 Python
详解Python3.8+PyQt5+pyqt5-tools+Pycharm配置详细教程
2020/11/02 Python
英国奢侈品网站:MatchesFashion
2016/12/16 全球购物
玩具反斗城西班牙网上商城:ToysRUs西班牙
2017/01/19 全球购物
金牌葡萄酒俱乐部:Gold Medal Wine Club
2017/11/02 全球购物
大众服装店创业计划书范文
2014/01/01 职场文书
超市仓管员岗位职责
2014/04/07 职场文书
2014年教师节寄语
2014/08/11 职场文书
MIME类型中application/xml与text/xml的区别介绍
2022/01/18 HTML / CSS
全新239军机修复记
2022/04/05 无线电
Python进程池与进程锁之语法学习
2022/04/11 Python
基于Python实现nc批量转tif格式
2022/08/14 Python