对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之Python文档
Oct 10 Python
详解MySQL数据类型int(M)中M的含义
Nov 20 Python
python用Pygal如何生成漂亮的SVG图像详解
Feb 10 Python
Python+matplotlib+numpy实现在不同平面的二维条形图
Jan 02 Python
Python使用matplotlib绘制正弦和余弦曲线的方法示例
Jan 06 Python
Python实现找出数组中第2大数字的方法示例
Mar 26 Python
Python获取二维矩阵每列最大值的方法
Apr 03 Python
解决phantomjs截图失败,phantom.exit位置的问题
May 17 Python
python 实现分页显示从es中获取的数据方法
Dec 26 Python
python读取txt文件并取其某一列数据的示例
Feb 19 Python
详解python调用cmd命令三种方法
Jul 08 Python
Python中的流程控制详解
Feb 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 php_openssl.dll的作用
2013/07/01 PHP
PHP中两个float(浮点数)比较实例分析
2015/09/27 PHP
浅谈PHP值mysql操作类
2016/06/29 PHP
php PDO异常处理详解
2016/11/20 PHP
js 替换
2008/02/19 Javascript
基于Jquery的简单&简陋Tabs插件代码
2010/02/09 Javascript
基于jquery的修改当前TAB显示标题的代码
2010/12/11 Javascript
jquery select多选框的左右移动 具体实现代码
2013/07/03 Javascript
使用jQuery实现Web页面换肤功能的要点解析
2016/05/12 Javascript
JavaScript Promise 用法
2016/06/14 Javascript
Node.js实现文件上传
2016/07/05 Javascript
JavaScript中值类型和引用类型的区别
2017/02/23 Javascript
Vue 2.0在IE11中打开项目页面空白的问题解决
2017/07/16 Javascript
微信小程序使用modal组件弹出对话框功能示例
2017/11/29 Javascript
CryptoJS中AES实现前后端通用加解密技术
2018/12/18 Javascript
微信小程序实现文字无限轮播效果
2018/12/28 Javascript
JS左右无缝轮播功能完整实例
2019/05/16 Javascript
Node.js API详解之 zlib模块用法分析
2020/05/19 Javascript
[01:00:30]TFT vs VGJ.T Supermajor 败者组 BO3 第一场 6.5
2018/06/06 DOTA
对于Python的Django框架部署的一些建议
2015/04/09 Python
Python的Flask框架及Nginx实现静态文件访问限制功能
2016/06/27 Python
Pycharm 文件更改目录后,执行路径未更新的解决方法
2019/07/19 Python
centos+nginx+uwsgi+Django实现IP+port访问服务器
2019/11/15 Python
德国低价购买灯具和家具网站:Style-home.de
2016/11/25 全球购物
美国一家全面的在线零售鞋类公司:SHOEBACCA
2017/01/06 全球购物
摄影助理岗位职责
2014/02/07 职场文书
小学信息技术教学反思
2014/02/10 职场文书
小学校园广播稿集锦
2014/10/04 职场文书
教师个人事迹材料
2014/12/17 职场文书
教师个人学习总结
2015/02/11 职场文书
新闻通讯稿范文
2015/07/22 职场文书
2016年猴年新春致辞
2015/08/01 职场文书
队列队形口号
2015/12/25 职场文书
vue3使用vue-router的完整步骤记录
2021/06/20 Vue.js
十大好看的穿越动漫排名:《瑞克和莫蒂》第一,国漫《有药》在榜
2022/03/18 日漫
Win11显卡控制面板打开显卡设置方法
2022/04/20 数码科技