root@10.1.6.200:~# cat object.py
#!/usr/bin/env python
#coding:utf8
class Dave():
var1 = "class atribute,public atrribute var1" #类属性,公有属性var1
__var2 = "class self atribute __var2" #类的私有属性__var2
def fun(self):
self.var2 = "object public atrribute var2" #对象的公有属性var2
self.__var3 = "object self atrribute __var3" #对象的私有属性__var3
var4 = "Function of the local variable var4" #函数fun的局部变量
def other(self):
print self.__var3
he = Dave() #实例化一个对象he print he.var1 #从实例中获取类的公有属性 print Dave.var1 #直接从类中获取公有属性
root@10.1.6.200:~# ./object.py
class atribute,public atrribute var1 class atribute,public atrribute var1
he = Dave() print Dave.__var2 print he.__var2
root@10.1.6.200:~# ./object.py
Traceback (most recent call last): File "./object.py", line 19, in <module> print Dave.__var2 AttributeError: class Dave has no attribute '__var2'
class Dave():
var1 = "class atribute,public atrribute var1" #类属性,公有属性var1
__var2 = "class self atribute __var2" #类的私有属性__var2
def other(self):
print Dave.__var2
he = Dave()
he.other()
root@10.1.6.200:~# ./object.py
class self atribute __var2
he = Dave() liu = Dave() he.fun() print he.var2 print liu.var2
root@10.1.6.200:~# ./object.py
object public atrribute var2 Traceback (most recent call last): <span></span> #对象liu由于没有调用fun方法所有就没有该属性. File "./object.py", line 20, in <module> print liu.var2 AttributeError: Dave instance has no attribute 'var2'
he = Dave() he.fun() print he.__var3
root@10.1.6.200:~# ./object.py
Traceback (most recent call last): File "./object.py", line 18, in <module> print he.__var3 AttributeError: Dave instance has no attribute '__var3'
he = Dave() he.fun() print he.var4
root@10.1.6.200:~# ./object.py
Traceback (most recent call last): File "./object.py", line 18, in <module> print he.var4 AttributeError: Dave instance has no attribute 'var4'
def fun(self):
self.var2 = "object public atrribute var2" #对象的公有属性var2
self.__var3 = "object self atrribute __var3" #对象的私有属性__var3
var4 = "Function of the local variable var4" #函数fun的局部变量
print var4 #可以在函数内部直接打印,只在该函数内有用
print self.__var3
he = Dave()
he.fun()
root@10.1.6.200:~# ./object.py
Function of the local variable var4 object self atrribute __var3
def fun(self):
self.var2 = "object public atrribute var2" #对象的公有属性var2
self.__var3 = "object self atrribute __var3" #对象的私有属性__var3
var4 = "Function of the local variable var4" #函数fun的局部变量
print var4 #一个函数的局部变量在另外一个函数是访问不到的
print self.__var3
def other(self):
print var4
print self.__var3
he = Dave()
he.fun()
print "#"*100
he.other()
root@10.1.6.200:~# ./object.py
Function of the local variable var4 object self atrribute __var3 #################################################################################################### Traceback (most recent call last): #会认为var4是全局变量打印.定义全局变量可在class 头加入 var4 = "global" File "./object.py", line 22, in <module> he.other() File "./object.py", line 16, in other print var4 NameError: global name 'var4' is not defined
#!/usr/bin/env python
#coding:utf8
var4 = "global" #定义var4为全局变量
class Dave():
var1 = "class atribute,public atrribute var1" #类属性,公有属性var1
__var2 = "class self atribute __var2" #类的私有属性__var2
def fun(self):
self.var2 = "object public atrribute var2" #对象的公有属性var2
self.__var3 = "object self atrribute __var3" #对象的私有属性__var3
var4 = "Function of the local variable var4" #函数fun的局部变量
print var4
print self.__var3
def other(self):
print var4
print self.__var3 #可调用私有属性,前提是先调用fun
he = Dave()
he.fun()
print "#"*100
he.other()
root@10.1.6.200:~# ./object.py
Function of the local variable var4 object self atrribute __var3 #################################################################################################### global object self atrribute __var3
#!/usr/bin/env python
#coding:utf8
class Dave():
name = "python"
def fun1(self): #定义公有方法
print self.name
print "i am public method"
def __fun2(self): #定义私有方法
print self.name
print "i am self method"
root@10.1.6.200:~# ./method.py #直接调用对象公有方法没有问题
python i am public method
#!/usr/bin/env python
#coding:utf8
class Dave():
name = "python"
def fun1(self): #定义公有方法
print self.name
print "i am public method"
self.__fun2()
def __fun2(self): #定义私有方法
print self.name
print "i am self method"
he = Dave()
he.fun1()
root@10.1.6.200:~# ./method.py
python i am public method python i am self method
@classmethod
def classFun(self): #定义类方法
print self.name
print "i am class method"
Dave.classFun()
root@10.1.6.200:~# ./method.py
python i am class method
def classFun(self): #定义类方法
print self.name
print "i am class method"
classnewFun = classmethod(classFun)
Dave.classnewFun() #被转换后的是一个类方法,原来classfun还是一个普通方法
root@10.1.6.200:~# ./method.py
python i am class method
@staticmethod
def staticFun(): #d定义静态方法
print Dave.name #注意不加self,直接打name也不行,会认为调用全局变量,需要使用类型加属性.
print "i am static method"
Dave.staticFun()
oot@10.1.6.200:~# ./method.py
python i am static method
def staticfun(): #定义静态方法
print Dave.name
print "i am static method"
staticnewFun = staticmethod(staticFun)
Dave.staticnewFun()
root@10.1.6.200:~# ./method.py
python i am static method
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有