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怎么用_对python 中class与变量的使用方法详解相关推荐

  1. python中class变量_对python 中class与变量的使用方法详解

    python中的变量定义是很灵活的,很容易搞混淆,特别是对于class的变量的定义,如何定义使用类里的变量是我们维护代码和保证代码稳定性的关键. #!/usr/bin/python #encoding ...

  2. python定义静态变量_对Pyhon实现静态变量全局变量的方法详解

    python不能像C++一样直接定义一个static变量或者通过extern来导入别的库的变量而实现数据共享,但是python的思想是通过模块化来解决这个问题,就是通过模块来实现全局变量. 首先新建一 ...

  3. python2和python3分别是python的两个版本_Windows下Python2与Python3两个版本共存的方法详解...

    前言 一向用Python 3,最近研究微信公众号开发,各云平台只支持Python 2.7,想用其他版本需要自己搭建环境.而网上又搜不到Python 3开发微信公众号的资料.暂打算先使用Python 2 ...

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

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

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

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

  6. 二叉树前序中序后续线索树_后序线索二叉树怎么画 线索二叉树基本操作详解 - 办公软件 - 服务器之家...

    后序线索二叉树怎么画 线索二叉树基本操作详解 发布时间:2017-05-23 来源:服务器之家 遍历二叉树是以一定规则将二叉树中结点排列成一个线性序列,得到二叉树中结点的先序,中序或后序序列.这实际上 ...

  7. python中如何创建一个空列表_Python创建空列表的字典2种方法详解

    如果要在 Python 中创建键值是空列表的字典,有多种方法,但是各种方法之间是否由区别?需要作实验验证,并且分析产生的原因.本文针对两种方法做了实验和分析. 如果要在 Python 中创建一个键值都 ...

  8. python读取html文件正则替换_Python使用正则表达式过滤或替换HTML标签的方法详解...

    本文实例讲述了Python使用正则表达式过滤或替换HTML标签的方法.分享给大家供大家参考,具体如下: python正则表达式关键内容: python正则表达式转义符: . 匹配除换行符以外的任意字符 ...

  9. windows7安dns服务器_在Windows 7 上安装DNS服务器bind9方法详解

    本文主要介绍在WIN7上安装bind9 DNS服务器的方法,非常详细希望对大家有所帮助 本文主要介绍在WIN7上利用ntbind部署DNS服务器的方法.ntbind是Bind的Windows版本, 1 ...

最新文章

  1. opensuse11.4-fcitx输入法安装
  2. mySql中The user specified as a definer ('root'@'%') does not exist
  3. umask详解、cwd简介
  4. Log4Net的WebApplication使用
  5. spring定时注解方式定时写到xml里面融合
  6. python sqlite3使用详解
  7. 库存出现负数 mysql_前台支付商品成功后,sku表库存,购买数量会重复执行减操作好几次了,导致库存为负数...
  8. iis 下的 selfssl
  9. 业务流程图_你会用Visio制作专业的业务流程图吗?
  10. MVC3 Razor语法
  11. 为什么JavaScript声明变量的时候鼓励加var关键字
  12. c#halcon 联合编程经验总结
  13. java sequence 生成器_分布式高效唯一ID生成器(sequence)
  14. 主板电源接口测试软件,雷电接口加入! Intel原厂血统Z77测试
  15. Python深度学习笔记(三)二分类模型
  16. cmos逻辑门传输延迟时间_什么是TTL电平、CMOS电平?区别是什么?
  17. 微信小程序显示圆形图片
  18. 方法区、永久代、元空间的区别
  19. linux修改默认22端口失败,【原创文章】修改亚马逊AWS EC2 LINUX系统SSH默认22端口失败的原因和解决办法...
  20. crontab——定时周期性执行任务

热门文章

  1. 在T-SQL语句中访问远程数据库(openrowset/opendatasource/openquery)
  2. #51CTO学院四周年# 成长路上的我和你
  3. Concourse:可扩展的开源CI管道工具
  4. 编写一个方法,数出从0到n中数字2出现了几次?
  5. OPEN ERP相关财务的基本概念
  6. 基于SpringBoot的乡村医生诊疗系统的设计与实现
  7. 使用scikit-learn进行预处理
  8. PEAK6 2020校招OA
  9. SQLServer中使用Split功能分割字符串
  10. Noip前的大抱佛脚----字符串