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

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程语言 > python >内容正文

python

python基础——使用__slots__

發(fā)布時(shí)間:2024/4/17 python 28 豆豆
生活随笔 收集整理的這篇文章主要介紹了 python基础——使用__slots__ 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

?

python基礎(chǔ)——使用__slots__

  正常情況下,當(dāng)我們定義了一個(gè)class,創(chuàng)建了一個(gè)class的實(shí)例后,我們可以給該實(shí)例綁定任何屬性和方法,這就是動(dòng)態(tài)語(yǔ)言的靈活性。先定義class:

class Student(object):pass

  然后,嘗試給實(shí)例綁定一個(gè)屬性:

>>> s = Student() >>> s.name = 'Michael' # 動(dòng)態(tài)給實(shí)例綁定一個(gè)屬性 >>> print(s.name) Michael

  還可以嘗試給實(shí)例綁定一個(gè)方法:

>>> def set_age(self, age): # 定義一個(gè)函數(shù)作為實(shí)例方法 ... self.age = age ... >>> from types import MethodType >>> s.set_age = MethodType(set_age, s) # 給實(shí)例綁定一個(gè)方法 >>> s.set_age(25) # 調(diào)用實(shí)例方法 >>> s.age # 測(cè)試結(jié)果 25

  是,給一個(gè)實(shí)例綁定的方法,對(duì)另一個(gè)實(shí)例是不起作用的

>>> s2 = Student() # 創(chuàng)建新的實(shí)例 >>> s2.set_age(25) # 嘗試調(diào)用方法 Traceback (most recent call last):File "<stdin>", line 1, in <module> AttributeError: 'Student' object has no attribute 'set_age'

  為了給所有實(shí)例都綁定方法,可以給class綁定方法

>>> def set_score(self, score): ... self.score = score ... >>> Student.set_score = set_score

  給class綁定方法后,所有實(shí)例均可調(diào)用:

>>> s.set_score(100) >>> s.score 100 >>> s2.set_score(99) >>> s2.score 99

  通常情況下,上面的set_score方法可以直接定義在class中,但動(dòng)態(tài)綁定允許我們?cè)诔绦蜻\(yùn)行的過程中動(dòng)態(tài)給class加上功能,這在靜態(tài)語(yǔ)言中很難實(shí)現(xiàn)。

?

使用__slots__

  但是,如果我們想要限制實(shí)例的屬性怎么辦?比如,只允許對(duì)Student實(shí)例添加nameage屬性。

  為了達(dá)到限制的目的,Python允許在定義class的時(shí)候,定義一個(gè)特殊的__slots__變量,來限制該class實(shí)例能添加的屬性

class Student(object):__slots__ = ('name', 'age') # 用tuple定義允許綁定的屬性名稱

  然后,我們?cè)囋?#xff1a;

>>> s = Student() # 創(chuàng)建新的實(shí)例 >>> s.name = 'Michael' # 綁定屬性'name' >>> s.age = 25 # 綁定屬性'age' >>> s.score = 99 # 綁定屬性'score' Traceback (most recent call last):File "<stdin>", line 1, in <module> AttributeError: 'Student' object has no attribute 'score'

  由于'score'沒有被放到__slots__中,所以不能綁定score屬性,試圖綁定score將得到AttributeError的錯(cuò)誤。

  使用__slots__要注意,__slots__定義的屬性僅對(duì)當(dāng)前類實(shí)例起作用,對(duì)繼承的子類是不起作用的

>>> class GraduateStudent(Student): ... pass ... >>> g = GraduateStudent() >>> g.score = 9999

  除非在子類中也定義__slots__,這樣,子類實(shí)例允許定義的屬性就是自身的__slots__加上父類的__slots__

?

參考源碼:

#python 使用__slots__ 示例 #2016-8-29 21:25:14 #MengmengCoding # -*- coding: utf-8 -*-#使用__slots__限制實(shí)例的屬性class Student(object):__slots__=('name','age') #用tuple定義允許綁定的屬性名稱#新類,繼承于Student class GraduateStudent(Student):passs=Student() #創(chuàng)建新的實(shí)例 s.name='Shuke' #綁定屬性'name' s.age=25 #綁定屬性'age'# ERROR: AttributeError: 'Student' object has no attribute 'score' try:s.score=99 except AttributeError as e:print('AttributeError:',e)g=GraduateStudent() #創(chuàng)建新的實(shí)例 ''' 使用__slots__要注意,__slots__定義的屬性僅對(duì)當(dāng)前類實(shí)例起作用, 對(duì)繼承的子類是不起作用的 ''' g.score=99 #綁定屬性'score' print('g.score=',g.score)

?

轉(zhuǎn)載于:https://www.cnblogs.com/codingmengmeng/p/5819891.html

總結(jié)

以上是生活随笔為你收集整理的python基础——使用__slots__的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。

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