没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
对对python 中中class与变量的使用方法详解与变量的使用方法详解
今天小编就为大家分享一篇对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
资源评论
- 范德安娜2022-12-20资源太好了,解决了我当下遇到的难题,抱紧大佬的大腿~
weixin_38522106
- 粉丝: 2
- 资源: 901
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功