对python 中class与变量的使用方法详解
作者:lawenliu 时间:2023-12-27 19:22:10
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
标签:python,class,变量
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
使用pandas把某一列的字符值转换为数字的实例
2021-08-16 08:11:55
SQL Server数据库查询优化3种技巧
2008-10-17 10:10:00
layer 刷新某个页面的实现方法
2024-04-10 10:46:39
scrapy与selenium结合爬取数据(爬取动态网站)的示例代码
2023-07-14 00:17:08
![](https://img.aspxhome.com/file/2023/7/79697_0s.png)
python中h5py开源库的使用样例详解
2022-03-30 00:37:03
Python查找不限层级Json数据中某个key或者value的路径方式
2023-07-08 02:39:43
怎样管理好css样式
2008-10-12 12:12:00
python中的tcp示例详解
2021-06-21 21:09:46
![](https://img.aspxhome.com/file/2023/4/98894_0s.png)
Python内置数据类型中的集合详解
2022-07-13 04:06:38
![](https://img.aspxhome.com/file/2023/3/95413_0s.png)
SqlServer表死锁的解决方法分享
2011-09-30 11:16:16
详解Vue进阶构造属性
2024-05-09 15:22:02
Pycharm安装并配置jupyter notebook的实现
2022-11-14 14:24:28
![](https://img.aspxhome.com/file/2023/5/115425_0s.jpg)
mysql-8.0.15-winx64 解压版安装教程及退出的三种方式
2024-01-23 21:56:20
![](https://img.aspxhome.com/file/2023/4/67584_0s.png)
asp网上购物车实例代码
2007-10-03 13:43:00
Vue.js结合SortableJS实现树形数据拖拽
2024-04-30 10:38:06
![](https://img.aspxhome.com/file/2023/1/130131_0s.png)
Python必知必会之os模块实例详解
2023-06-09 22:31:07
![](https://img.aspxhome.com/file/2023/6/99046_0s.jpg)
Python字符串字母大小写转换的各种情况详析
2023-11-23 07:35:41
手把手带你了解python多进程,多线程
2021-07-16 02:08:42
![](https://img.aspxhome.com/file/2023/6/95446_0s.png)
Ethnique公司logo设计过程和思路
2009-09-19 17:04:00
![](https://img.aspxhome.com/file/UploadPic/20099/19/01-79s.jpg)
在TensorFlow中屏蔽warning的方式
2023-05-08 10:54:19