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

歡迎訪問 生活随笔!

生活随笔

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

python

给Python的类和对象动态增加属性和方法

發(fā)布時間:2025/3/20 python 36 豆豆
生活随笔 收集整理的這篇文章主要介紹了 给Python的类和对象动态增加属性和方法 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

通常我們會將編程語言分為靜態(tài)和動態(tài)。靜態(tài)語言的變量是在內(nèi)存中的有類型的且不可變化的,除非強制轉換它的類型;動態(tài)語言的變量是指向內(nèi)存中的標簽或者名稱,其類型在代碼運行過程中會根據(jù)實際的值而定。Python就是典型的動態(tài)語言。

1.動態(tài)添加屬性
當類或者對象的屬性在需要增加的時候,對于不方便修改源碼的情況下,我們可以選擇動態(tài)的對其添加屬性。

(1 動態(tài)給對象添加屬性

對象屬性只在當前對象生效,在其他對象中是無法調用的。

定義一個類:

class Student(object):def __init__(self,name,age):self.name=nameself.age=age

執(zhí)行:(給實例添加數(shù)學成績屬性并且初始化)

>>> from payhlib import Student >>> s=Student('xiaoming',18) >>> s.name 'xiaoming' >>> s.age 18 >>> s.math_score=100 >>> s.math_score 100 >>> ss=Student('laowang',28) >>> ss.name 'laowang' >>> ss.age 28 >>> ss.math_score Traceback (most recent call last):File "<stdin>", line 1, in <module> AttributeError: 'Student' object has no attribute 'math_score' >>>

(2 動態(tài)給類添加屬性

類屬性在其所有的對象中都生效。

執(zhí)行:(默認所有對象的音樂成績?yōu)?0,當然你也可以對其進行修改)

''' 學習中遇到問題沒人解答?小編創(chuàng)建了一個Python學習交流QQ群:531509025 尋找有志同道合的小伙伴,互幫互助,群里還有不錯的視頻學習教程和PDF電子書! ''' >>> from payhlib import Student >>> Student.music_score=60 >>> s1=Student('xiaoming',19) >>> s1.name 'xiaoming' >>> s1.age 19 >>> s1.music_score 60 >>> s2=Student('laowang',29) >>> s2.music_score 60 >>>

2.動態(tài)添加方法

當類或者對象的方法在需要增加的時候,對于不方便修改源碼的情況下,我們可以選擇動態(tài)的對其添加方法。

(1 動態(tài)給對象添加方法

給對象添加的方法只綁定在當前對象上,不對其他對象生效,而且需要傳入self參數(shù)。

執(zhí)行:(通過types.MethodType方法給a對象綁定sayhi方法)

>>> from payhlib import Student >>> a=Student('xiaoming',17) >>> def sayhi(self): ... print('hi...') ... >>> a.sayhi() Traceback (most recent call last):File "<stdin>", line 1, in <module> AttributeError: 'Student' object has no attribute 'sayhi' >>> import types >>> a.sayhi=types.MethodType(sayhi,a) >>> a.sayhi() hi... >>> b=Student('laowang',27) >>> b.sayhi() Traceback (most recent call last):File "<stdin>", line 1, in <module> AttributeError: 'Student' object has no attribute 'sayhi' >>>

(2 動態(tài)給類添加方法(類方法和靜態(tài)方法)

給類添加的方法對它的所有對象都生效,添加類方法需要傳入self參數(shù),添加靜態(tài)方法則不需要。

執(zhí)行:(給類添加靜態(tài)方法)

''' 學習中遇到問題沒人解答?小編創(chuàng)建了一個Python學習交流QQ群:531509025 尋找有志同道合的小伙伴,互幫互助,群里還有不錯的視頻學習教程和PDF電子書! ''' >>> from payhlib import Student >>> stu = Student('xiaoming',16) >>> @staticmethod ... def staticHi(): ... print('staticHi...') ... >>> Student.hi=staticHi >>> stu.hi <function staticHi at 0x000001CB617F13A8> >>> stu.hi() staticHi... >>>

執(zhí)行:(給類添加類方法)

因為類方法只能使用類變量,所以我們增加一個類變量home

class Student(object):home='china'def __init__(self,name,age):self.name=nameself.age=age>>> from payhlib import Student >>> stu = Student('xiaoming',17) >>> @classmethod ... def classHi(self): ... print(self.home) ... >>> Student.chi=classHi >>> stu.chi() china >>>

3.限制給類或對象添加的屬性

假如我們只希望類或者對象有name,age,score三個屬性,我們可以借助__slots__來做,而且無法添加其他屬性。

修改類:

''' 學習中遇到問題沒人解答?小編創(chuàng)建了一個Python學習交流QQ群:531509025 尋找有志同道合的小伙伴,互幫互助,群里還有不錯的視頻學習教程和PDF電子書! ''' class Student(object):home='china'__slots__=('name','age','score') #init中變量必須在__slots__中def __init__(self,name,age):self.name=nameself.age=age

執(zhí)行:

>>> from payhlib import Student >>> st = Student('xiaoming',16) >>> st.name 'xiaoming' >>> st.age 16 >>> st.score Traceback (most recent call last):File "<stdin>", line 1, in <module> AttributeError: score >>> st.score =100 >>> st.score 100 >>> st.phone='123' #無法添加phone屬性 Traceback (most recent call last):File "<stdin>", line 1, in <module> AttributeError: 'Student' object has no attribute 'phone' >>>

多總結幾點:

  • 類屬性是read-only的
  • 靜態(tài)方法無法使用類變量
  • 類方法只能使用類變量,不能使用初始化變量
  • __slots__數(shù)據(jù)類型為元組
  • __slots__只對當前類生效,對其子類不生效
與50位技術專家面對面20年技術見證,附贈技術全景圖

總結

以上是生活随笔為你收集整理的给Python的类和对象动态增加属性和方法的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。