日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > python >内容正文

python

python的类和实例化对象

發布時間:2025/3/19 python 32 豆豆
生活随笔 收集整理的這篇文章主要介紹了 python的类和实例化对象 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

一切皆對象,類也是對象,類來自于元類type,如果一個類沒有聲明自己的元類,默認它就是元類。

即類是元類的實例,通過type(類)會顯示type,而實例來自于類。

?

類有兩個屬性,數據屬性和函數屬性,下面是一個創建類和實例化對象的例子

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()#執行結果 # 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類的數據屬性,__init__()不應該有返回值,否則會報錯,其他函數則是animal類的函數屬性,可以看到類下面的函數的第一個參數都是self。

當執行cat=animal('cat','male',4),觸發animal類的__init__()函數生成一個cat實例。

__dict__表示屬性字典,以字典形式存放,通過打印animal.__dict__和cat.__dict__可以發現,類有數據屬性和函數屬性,而實例只有函數屬性沒有數據屬性,但是實例可以繼承和調用對象的函數屬性。

實例調用類的函數時,會自動將實例本身作為第一個參數傳給函數,但是類自己調用函數時不會自動將實例本身作為參數傳入,例如要通過類調用play()函數,則animal.play(cat)。

類的屬性字典是共用的,而實例的屬性字典是私有的。

?

類屬性的查看、增加、修改和刪除

#查看類的數據屬性 print(animal.type) #修改類的數據屬性 animal.type='Animal' print(animal.type) #增加類的數據屬性 animal.haveteeth=True print(cat.haveteeth) #刪除類的數據屬性 del animal.haveteeth #增加類的函數屬性,修改類似 def play_bal(self,ball):print('The %s is playing %s'%(self.name,ball)) animal.play_ball=play_bal cat.play_ball('tennis')

?

實例屬性的增加

#增加實例的數據屬性,cat.__dict__['key']='value'也可以增加數據屬性但不建議使用 cat.living='land' print(cat.__dict__) #{'name': 'cat', 'sex': 'male', 'leg': 4, 'living': 'land'} #刪除實例的數據屬性 del cat.living print(cat.__dict__) #{'name': 'cat', 'sex': 'male', 'leg': 4} #修改實例的數據屬性 cat.sex='female' print(cat.sex) #female #增加實例的函數屬性 def test():print('hello') cat.test=test cat.test() #hello

?對于實例增加的函數屬性,實例在調用時不會自動將自身作為參數傳入。

?

?

需要注意的是:

如果在類前面定義了與類的數據屬性同名的全局變量,那么只要不通過類或者實例+.調用這個變量,這個變量就是指類前面定義的全局變量

如果實例增加了與類的數據屬性同名的屬性,相當于給實例增加了這個屬性,對實例的屬性沒有影響。

type='mouse' class animal:type='animal'l=['a','b']def __init__(self,name):self.name = nameprint(type) #此處的type是指全局變量,而不是類內部的數據屬性變量 cat=animal('cat') cat.type='male' #相當于給cat增加了一個數據屬性type,對類的type屬性沒有影響 print(cat.__dict__) print(cat.type) cat.l.append('c')#此處修改的是類的數據屬性l,與通過=賦值(給實例增加屬性)不同

?

__class__:實例來自哪個類

__module__:實例來自哪個模塊

?

轉載于:https://www.cnblogs.com/Forever77/p/10083808.html

總結

以上是生活随笔為你收集整理的python的类和实例化对象的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。