对python 中class与变量的使用方法详解
作者:lawenliu 发布时间:2023-12-27 19:22:10
标签:python,class,变量
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与变量的使用方法详解就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持脚本之家。
来源:https://blog.csdn.net/lwc5411117/article/details/83546759


猜你喜欢
- 首先我们要在邮箱的设置中开通那个POP3然后我们要导入这些包import poplibfrom datetime import dateti
- 在django中创建表,会自动创建一些django自带的表,先了解用户认证,认证登录 先要引用 ,from django.contrib i
- 上一篇:微软建议的ASP性能优化28条守则(7)技巧 22:尽可能使用 Server.Transfer 代替 Response.Redire
- 一、介绍数据库的约束是对表中数据进行的一种限制,为了保证数据的正确性、有效性、完整性。无论是在添加数据还是在删除数据的时候,都能提供帮助。所
- 本文实例讲述了Python递归遍历列表及输出的实现方法。分享给大家供大家参考。具体实现方法如下:def dp(s): if is
- 引言最近在做个表情包的小程序,涉及到表情包搜索功能。我们上传表情包的时候,只有一张图,怎么搜索?这个时候我们想到就是将表情包的文字提取出来,
- 一、什么是Oracle字符集 Oracle字符集是一个字节数据的解释的符号集合,有大小之分,有相互的包容关系。ORACLE 支持国家语言的体
- 本游戏程序实现的功能为本地二人对弈中国象棋,实现语言为javascript+VML,在windows 2000 pro+IE 6sp1的环境
- 实际线上的场景比较复杂,当时涉及了truncate, delete 两个操作,经确认丢数据差不多7万多行,等停下来时,差不多又有共计1万多行
- 请问鼠标移过去就出现二级菜单代码怎么写啊 <head><style type="tex
- 我们将使用2019年全国新能源汽车的销量数据作为演示数据,数据保存在一个csv文件中,读者可以在GitHub仓库下载到 https://gi
- 1.嵌入 IFrame(/assets/img/anchor.svg)]()](https://gradio.app/sharing-you
- 环境:Oracle 11.2.0.4 RAC(2 nodes)说明:假设新增闪存挂载点是/flash(使用了第三方的集群文件系统),如果是使
- 在前面的章节中,我们讨论了Series的计算方法与Pandas的自动对齐功能。不光是Series,DataFrame也是支持运算的,而且还是
- 简单说明:思路:从数据岛menuXML中读取数据,从树的根节点开始分析树,利用 hasChildNodes() [方法:是否含有子节点 ]
- python列表元素去重后如何保持原来的顺序不变原列表:list1 = [1,2,1,4,9,3,5,2,6,7,3,1,6,8,4,0]去
- 什么是接口测试接口测试主要用于检测外部系统与内部系统之间,以及系统内部各 个子系统之间的交互点。其测试的重点是,检查数据的交换、传递和控 制
- 建立一个查询,执行下面的语句生成函数fn_GetPy --生成拼音首码 CREATE function fn_GetPy(@str nvar
- 匿名管道管道是一个单向通道,有点类似共享内存缓存.管道有两端,包括输入端和输出端.对于一个进程的而言,它只能看到管道一端,即要么是输入端要么
- 前言:在软件测试中,为项目编写接口自动化用例已成为测试人员常驻的测试工作。本文以python为例,基于笔者曾使用过的三种用例数据读取方法:x