对python 中class与变量的使用方法详解
python中的变量定义是很灵活的,很容易搞混淆,特别是对于class的变量的定义,如何定义使用类里的变量是我们维护代码和保证代码稳定性的关键。
#!/usr/bin/python #encoding:utf-8 global_variable_1='global_variable' classMyClass(): class_var_1='class_val_1'#defineclassvariablehere def__init__(self,param): self.object_var_1=param#defineobjectvariablehere self.object_var_2='object_val_2'#defineobjectvariablehere self.object_func3() defobject_func1(self,param): local_var_1=param#definelcoalvariablehere local_var_2='local_val_2'#definelocalvariablehere self.internal_var_1='internal_val_1'#defineinternalvariablehere print(local_var_1)#wecanuselocalvariableofcurrenthere print(local_var_2)#wecanuselocalvariableofcurrenthere print(MyClass.class_var_1)#wecanuseclassvariablehere,butyouhaveusingclassnameassprefix print(self.class_var_1)#wecanuseclassvariableasobjectvariablehere print(self.object_var_1)#wecanuseobjectvariablehere print(self.object_var_2)#wecanuseobjectvariablehere print(self.internal_var_1)#wecanuseinternalvariablehere #print(local_var_3)#wecan'tuselocalvariableinanotherfunction print(global_variable_1)#wecanuseglobalvariablehere defobject_func2(self,param='func_val_1'): local_var_3=param#definelocalvariablehere print(local_var_3)#wecanuselcoalvariablehere print(self.internal_var_1)#wecanuseinternalvariabledefinedinclass_func1,butyouhavetocallclass_func1first print(MyClass.class_var_1)#wecanuseclassvariablehere,butyouhaveusingclassnameassprefix print(self.class_var_1)#wecanclassvariablehere print(self.object_var_1)#wecanuseobjectvariablehere print(self.object_var_2)#wecanuseobjectvariablehere print(global_variable_1)#wecanuseglobalvariablehere defobject_func3(self,param='func_val_1'): self.object_var_3=param#becausethisfunctioncalledinconstructionfunction,sothisisdefinedasobjectvariable,notinternalvariable self.object_var_4='object_val_4'#becausethisfunctioncalledinconstructionfunction,sothisisdefinedasobjectvariable,notinternalvariable print(global_variable_1)#wecanuseglobalvariablehere #defineclassfunction defclass_func4(): print(MyClass.class_var_1) print(global_variable_1)#wecanuseglobalvariablehere if__name__=='__main__': myObject=MyClass('object_val_1') print(MyClass.class_var_1)#wecanuseclassvariabledirectlyhere #print(MyClass.object_var_1)#wecan'tuseobjectvariablehere print(myObject.object_var_1)#wecanuseobjectvariablehere print(myObject.object_var_2)#wecanuseobjectvariablehere print(myObject.object_var_3)#wecanuseobjectvariablehere print(myObject.object_var_4)#wecanuseobjectvariablehere #print(myObject.internal_var_1)#wecan'tuseinternalvariableasobjectvariablehere MyClass.class_func4()#wecanuseclassfunctionhere #MyClass.object_func2(myObject,'local_var_3')#internalvariablecan'tbeusedinthisfunction myObject.object_func1('local_var_1')#callfirstfunction myObject.object_func2('local_var_3')#callsecondfunction print(global_variable_1)#wecanuseglobalvariablehere
简单的写了个测试小程序,枚举了各种情况,没有办法全部枚举,但大部分情况应该都已经包含了。
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与变量的使用方法详解就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持毛票票。