对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爬虫之URLError异常处理
Feb 17 Python
Python应用03 使用PyQT制作视频播放器实例
Dec 07 Python
Python实现自定义顺序、排列写入数据到Excel的方法
Apr 23 Python
Python3实现的爬虫爬取数据并存入mysql数据库操作示例
Jun 06 Python
Python解决两个整数相除只得到整数部分的实例
Nov 10 Python
值得收藏的10道python 面试题
Apr 15 Python
Django组件content-type使用方法详解
Jul 19 Python
Python笔记之工厂模式
Nov 20 Python
Python使用Turtle库绘制一棵西兰花
Nov 23 Python
Python异常继承关系和自定义异常实现代码实例
Feb 20 Python
Python使用进程Process模块管理资源
Mar 05 Python
Python下载的11种姿势(小结)
Nov 18 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边学边教》(02.Apache+PHP环境配置――下篇)
2006/12/13 PHP
通过php删除xml文档内容的方法
2015/01/23 PHP
php实现parent调用父类的构造方法与被覆写的方法
2015/02/11 PHP
基于laravel制作APP接口(API)
2016/03/15 PHP
php求数组全排列,元素所有组合的方法
2016/05/05 PHP
PHP Socket网络操作类定义与用法示例
2017/08/30 PHP
php实现微信企业转账功能
2018/10/02 PHP
PhpStorm 如何优雅的调试Hyperf的方法步骤
2019/11/24 PHP
jQuery表格行换色的三种实现方法
2011/06/27 Javascript
Javascript拓展String方法小结
2013/07/08 Javascript
jQuery Mobile的loading对话框显示/隐藏方法分享
2013/11/26 Javascript
js中settimeout方法加参数
2014/02/28 Javascript
javascript中AJAX用法实例分析
2015/01/30 Javascript
canvas轨迹回放功能实现
2017/12/20 Javascript
在vue项目中使用element-ui的Upload上传组件的示例
2018/02/08 Javascript
Vue中CSS动画原理的实现
2019/02/13 Javascript
JavaScript函数定义方法实例详解
2019/03/05 Javascript
微信小程序template模版的使用方法
2019/04/13 Javascript
three.js着色器材质的内置变量示例详解
2020/08/16 Javascript
JavaScript 中的六种循环方法
2021/01/06 Javascript
python中使用sys模板和logging模块获取行号和函数名的方法
2014/04/15 Python
selenium 安装与chromedriver安装的方法步骤
2019/06/12 Python
基于python实现学生信息管理系统
2019/11/22 Python
Python Sphinx使用实例及问题解决
2020/01/17 Python
解决jupyter notebook显示不全出现框框或者乱码问题
2020/04/09 Python
Python参数传递机制传值和传引用原理详解
2020/05/22 Python
CSS实现的一闪而过的图片闪光效果
2014/04/23 HTML / CSS
非常震撼的纯CSS3人物行走动画
2016/02/24 HTML / CSS
描述RIP和OSPF区别以及特点
2015/01/17 面试题
竞聘上岗演讲稿范文
2014/01/10 职场文书
医院辞职信范文
2014/01/17 职场文书
优秀学生干部推荐材料
2014/02/03 职场文书
市场调查策划方案
2014/06/10 职场文书
继续教育心得体会(共6篇)
2016/01/19 职场文书
2016年圣诞节活动总结范文
2016/04/01 职场文书
java固定大小队列的几种实现方式详解
2021/07/15 Java/Android