对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 相关文章推荐
用pywin32实现windows模拟鼠标及键盘动作
Apr 22 Python
Python中的ConfigParser模块使用详解
May 04 Python
python利用urllib实现爬取京东网站商品图片的爬虫实例
Aug 24 Python
Python编程中NotImplementedError的使用方法
Apr 21 Python
python使用response.read()接收json数据的实例
Dec 19 Python
Python 监测文件是否更新的方法
Jun 10 Python
python3.7环境下安装Anaconda的教程图解
Sep 10 Python
使用python实现kNN分类算法
Oct 16 Python
pygame实现俄罗斯方块游戏(对战篇1)
Oct 29 Python
详解Django CAS 解决方案
Oct 30 Python
Pytest框架之fixture的详细使用教程
Apr 07 Python
python实现PDF中表格转化为Excel的方法
Jun 16 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使用CURL不依赖COOKIEJAR获取COOKIE的方法
2015/06/17 PHP
php实现阳历阴历互转的方法
2015/10/28 PHP
jQuery动态添加删除select项(实现代码)
2013/09/03 Javascript
让浏览器崩溃的12行JS代码(DoS攻击分析及防御)
2016/10/10 Javascript
Bootstrap模态框水平垂直居中与增加拖拽功能
2016/11/09 Javascript
JS基于正则截取替换特定字符之间字符串操作示例
2017/02/03 Javascript
教你用十行node.js代码读取docx的文本
2017/03/08 Javascript
jQuery中.attr()和.data()的区别分析
2017/09/03 jQuery
详解使用Visual Studio Code对Node.js进行断点调试
2017/09/14 Javascript
详解React项目的服务端渲染改造(koa2+webpack3.11)
2018/03/19 Javascript
详解React Native 屏幕适配(炒鸡简单的方法)
2018/06/11 Javascript
JS无限级导航菜单实现方法
2019/01/05 Javascript
在Create React App中使用CSS Modules的方法示例
2019/01/15 Javascript
jquery的$().each和$.each的区别
2019/01/18 jQuery
[01:00:44]DOTA2上海特级锦标赛主赛事日 - 3 败者组第三轮#1COL VS Alliance第三局
2016/03/04 DOTA
[01:02:30]Mineski vs Secret 2019国际邀请赛淘汰赛 败者组 BO3 第三场 8.22
2019/09/05 DOTA
[01:18:45]DOTA2-DPC中国联赛 正赛 DLG vs Dragon BO3 第三场2月1日
2021/03/11 DOTA
python使用urllib模块开发的多线程豆瓣小站mp3下载器
2014/01/16 Python
python实现定制交互式命令行的方法
2014/07/03 Python
DJANGO-ALLAUTH社交用户系统的安装配置
2014/11/18 Python
Python实现TCP/IP协议下的端口转发及重定向示例
2016/06/14 Python
浅谈pyqt5中信号与槽的认识
2019/02/17 Python
python之语音识别speech模块
2020/09/09 Python
Python安装第三方库攻略(pip和Anaconda)
2020/10/15 Python
Alpine安装Python3依赖出现的问题及解决方法
2020/12/25 Python
html5自带表单验证体验优化及提示气泡修改功能
2017/09/12 HTML / CSS
HTML5实时语音通话聊天MP3压缩传输3KB每秒
2019/08/28 HTML / CSS
杭州-DOTNET笔试题集
2013/09/25 面试题
三年大学自我鉴定
2014/01/16 职场文书
我们的节日元宵活动方案
2014/08/23 职场文书
国庆横幅标语
2014/10/08 职场文书
2015羊年春节慰问信
2015/02/14 职场文书
政协工作总结2015
2015/05/20 职场文书
html+css实现分层金字塔的实例
2021/06/02 HTML / CSS
Go语言应该什么情况使用指针
2021/07/25 Golang
基于Apache Hudi在Google云构建数据湖平台的思路详解
2022/04/07 Servers