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 中class与变量的使用方法详解

本文地址: http://www.cppcns.com/jiaoben/python/263751.html

python中class变量_对python 中class与变量的使用方法详解相关推荐

  1. 站长在线Python精讲:Python中集合的交集、并集、差集和对称差集运算方法详解

    欢迎你来到站长在线的站长学堂学习Python知识,本文学习的是<Python中集合的交集.并集.差集和对称差集运算方法详解>.主要讲的是集合运算的相关的概念,及运算方法,包括:集合的交集. ...

  2. PS中色相饱合度/可选颜色/色彩平衡/曲线的区别和运用方法详解

    http://www.jb51.net/photoshop/374517.html PS中色相饱合度/可选颜色/色彩平衡/曲线的区别和运用方法详解 整体思路: 1.使用颜色混合模式营造照片基调. 2. ...

  3. python下载微信公众号文章_python如何导出微信公众号文章方法详解

    1.安装wkhtmltopdf 下载地址:https://wkhtmltopdf.org/downloads.html 我测试用的是windows的,下载安装后结果如下 2 编写python 代码导出 ...

  4. python接收邮件内容启动程序_Python实现发送与接收邮件的方法详解

    本文实例讲述了Python实现发送与接收邮件的方法.分享给大家供大家参考,具体如下: 一.发送邮件 这里实现给网易邮箱发送邮件功能: import smtplib import tkinter cla ...

  5. java中flush函数作用_Java语言中flush()函数作用及使用方法详解

    最近在学习io流,发现每次都会出现flush()函数,查了一下其作用,起作用主要如下 //------–flush()的作用--------– 笼统且错误的回答: 缓冲区中的数据保存直到缓冲区满后才写 ...

  6. java调用javascript函数_[Java教程]JavaScript函数的4种调用方法详解

    [Java教程]JavaScript函数的4种调用方法详解 0 2016-08-09 00:00:12 在JavaScript中,函数是一等公民,函数在JavaScript中是一个数据类型,而非像C# ...

  7. 什么模式下不可使用曝光补偿_摄影从零到入门 曝光模式与测光方法详解

    今天我们带来180分钟摄影从零到入门的第三节课,认识曝光(中).这一节课我们将主要教会大家以下几个问题:相机有哪些曝光模式以及各有什么作用,相机如何选择测光模式,如何调整曝光补偿,如何快速掌握曝光是否 ...

  8. 下拉多选框 微信小程序_微信小程序下拉框组件使用方法详解

    本文实例为大家分享了微信小程序下拉框组件的使用方法,供大家参考,具体内容如下 适用场景 1.省市三级联动 2.出生日期选择 3.性别选择 4.一般性的下拉选择等 一.省市三级联动使用 注意mode = ...

  9. python什么模块动态调用链接库_Python调用C/C++动态链接库的方法详解

    本文以实例讲解了Python调用C/C++ DLL动态链接库的方法,具体示例如下: 示例一: 首先,在创建一个DLL工程(本例创建环境为VS 2005),头文件: 1 2 3 4 5 6 7 8 9 ...

  10. linux中python编译器的配置_方舟编译器环境配置及源码编译过程详解

    1)首先将方舟编译器源代码包下载到本地. https://www.openarkcompiler.cn/download/OpenArkCompiler-0.2.tar.gz 2)Ubuntu系统中方 ...

最新文章

  1. r语言echarts画箱线图_echarts学习笔记之箱线图的分析与绘制详解
  2. java不会自动提示_eclispe中打点不会提示的解决方法,以及自动补全
  3. POJ 1837 Balance(01背包变型)
  4. SAP UI5 应用开发教程之六十四 - 基于 OData V4 的 SAP UI5 表格控件如何实现 filter(过滤) 和 sort(排序)功能
  5. postfix 部署ssl后还是25_宝塔面板的邮局管理器Postfix无法启动解决办法
  6. Python中Collections模块的Counter容器类使用教程
  7. Spring入门学习手册 6:Spring MVC基础中的基础
  8. Linux 常用系统命令-20160504
  9. jsoup教程_2 http-client 讲解
  10. Github copilot: AI协助编程尝试
  11. 树莓派3B的摄像头模块
  12. 【转载】装机知识显卡篇,一篇文章让小白透彻的了解显卡
  13. 阿里云产品介绍(一):云服务器ECS
  14. Java、计算税收(Java8)
  15. [图文]symbian与uiq开发教程(转)
  16. 【题解】洛谷P3084 照片(差分约束)
  17. 一分钟教你如何将Word生成目录,就是这么简单
  18. 关于android手机应用创意
  19. 《上海市居住证》审核试行办法
  20. 解析酒链世界系统对企业发展的影响

热门文章

  1. java多线程系列(1)
  2. 命令流水帐之二:配置Server环境LAMP
  3. javascript里的post和get有什么区别
  4. 3D 音频技术产品介绍(1):Iosono the future of spatial audio
  5. RH124 第六单元   管理物理存储
  6. pandas(三) -- DataFrame的基本操作
  7. JavaScript ES2021 最值得期待的 5 个新特性解析
  8. jupyter 接受参数
  9. 一篇文章教你读懂Spring @Conditional注解
  10. linux shell带索引下标遍历数组