python的类和实例化对象
一切皆對象,類也是對象,類來自于元類type,如果一個類沒有聲明自己的元類,默認(rèn)它就是元類。
即類是元類的實例,通過type(類)會顯示type,而實例來自于類。
?
類有兩個屬性,數(shù)據(jù)屬性和函數(shù)屬性,下面是一個創(chuàng)建類和實例化對象的例子
class animal:'This is class for animal' #類的說明type='animal'def __init__(self,name,sex,leg):self.name = nameself.sex = sexself.leg = legdef eat(self,food):print('%s likes to eat %s'%(self.name,food))def play(self):print('%s is playing'%self.name)print(animal.__name__)#打印類的名稱 print(animal.__doc__) #打印類的說明,__doc__屬性不能繼承給子類 print(animal.__dict__) #打印類的屬性字典 cat=animal('cat','male',4) print(cat.__dict__) #打印類的屬性字典 print(cat.type) cat.eat('mouse') cat.play()#執(zhí)行結(jié)果 # animal # This is class for animal # {'__module__': '__main__', '__doc__': 'This is class for animal', 'haveTeeth': True, '__init__': <function animal.__init__ at 0x00000000021AA598>, 'eat': <function animal.eat at 0x00000000021AA620>, '__dict__': <attribute '__dict__' of 'animal' objects>, '__weakref__': <attribute '__weakref__' of 'animal' objects>} # {'name': 'cat', 'sex': 'male', 'leg': 4} # cat likes to eat mouse # animal在class animal:范圍下面的都是對animal類的定義,其中def __init__()是定義animal類的數(shù)據(jù)屬性,__init__()不應(yīng)該有返回值,否則會報錯,其他函數(shù)則是animal類的函數(shù)屬性,可以看到類下面的函數(shù)的第一個參數(shù)都是self。
當(dāng)執(zhí)行cat=animal('cat','male',4),觸發(fā)animal類的__init__()函數(shù)生成一個cat實例。
__dict__表示屬性字典,以字典形式存放,通過打印animal.__dict__和cat.__dict__可以發(fā)現(xiàn),類有數(shù)據(jù)屬性和函數(shù)屬性,而實例只有函數(shù)屬性沒有數(shù)據(jù)屬性,但是實例可以繼承和調(diào)用對象的函數(shù)屬性。
實例調(diào)用類的函數(shù)時,會自動將實例本身作為第一個參數(shù)傳給函數(shù),但是類自己調(diào)用函數(shù)時不會自動將實例本身作為參數(shù)傳入,例如要通過類調(diào)用play()函數(shù),則animal.play(cat)。
類的屬性字典是共用的,而實例的屬性字典是私有的。
?
類屬性的查看、增加、修改和刪除
#查看類的數(shù)據(jù)屬性 print(animal.type) #修改類的數(shù)據(jù)屬性 animal.type='Animal' print(animal.type) #增加類的數(shù)據(jù)屬性 animal.haveteeth=True print(cat.haveteeth) #刪除類的數(shù)據(jù)屬性 del animal.haveteeth #增加類的函數(shù)屬性,修改類似 def play_bal(self,ball):print('The %s is playing %s'%(self.name,ball)) animal.play_ball=play_bal cat.play_ball('tennis')?
實例屬性的增加
#增加實例的數(shù)據(jù)屬性,cat.__dict__['key']='value'也可以增加數(shù)據(jù)屬性但不建議使用 cat.living='land' print(cat.__dict__) #{'name': 'cat', 'sex': 'male', 'leg': 4, 'living': 'land'} #刪除實例的數(shù)據(jù)屬性 del cat.living print(cat.__dict__) #{'name': 'cat', 'sex': 'male', 'leg': 4} #修改實例的數(shù)據(jù)屬性 cat.sex='female' print(cat.sex) #female #增加實例的函數(shù)屬性 def test():print('hello') cat.test=test cat.test() #hello?對于實例增加的函數(shù)屬性,實例在調(diào)用時不會自動將自身作為參數(shù)傳入。
?
?
需要注意的是:
如果在類前面定義了與類的數(shù)據(jù)屬性同名的全局變量,那么只要不通過類或者實例+.調(diào)用這個變量,這個變量就是指類前面定義的全局變量
如果實例增加了與類的數(shù)據(jù)屬性同名的屬性,相當(dāng)于給實例增加了這個屬性,對實例的屬性沒有影響。
type='mouse' class animal:type='animal'l=['a','b']def __init__(self,name):self.name = nameprint(type) #此處的type是指全局變量,而不是類內(nèi)部的數(shù)據(jù)屬性變量 cat=animal('cat') cat.type='male' #相當(dāng)于給cat增加了一個數(shù)據(jù)屬性type,對類的type屬性沒有影響 print(cat.__dict__) print(cat.type) cat.l.append('c')#此處修改的是類的數(shù)據(jù)屬性l,與通過=賦值(給實例增加屬性)不同?
__class__:實例來自哪個類
__module__:實例來自哪個模塊
?
轉(zhuǎn)載于:https://www.cnblogs.com/Forever77/p/10083808.html
總結(jié)
以上是生活随笔為你收集整理的python的类和实例化对象的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 重磅!阿里巴巴工程师获得 contain
- 下一篇: linux查看java进程cpu占用过高