_slots_簡(jiǎn)介2-使用

  1. __slots__作用
  • 類成員變量描述符
  • __slots__是一個(gè)元祖
  • 類的實(shí)例只能擁有__slots__中定義的變量
  • 定義了__slots__之后就不再有__dict__屬性
  • 在Python3中,類變量不能與__slots__預(yù)定義的變量名重名
  1. 示例1
class Foo(object):
    __slots__ = ('x')
    var = 9
    def __init__(self):
        self.a = 10

f = Foo()

運(yùn)行時(shí)錯(cuò)誤AttributeError: 'Foo' object has no attribute 'a'
原因:不能定義名稱為a的類實(shí)例變量

  1. 示例2
class Foo(object):
    __slots__ = ('x')
    var = 9
    def __init__(self):
        self.x = 10

f = Foo()
print(f.__dict__)

運(yùn)行錯(cuò)誤,擁有__slots__的類不再有__dict__屬性。

  1. 示例3
class Foo(object):
    __slots__ = ('x', 'y')
    var = 9
    def __init__(self):
        self.x = 10

f = Foo()
# # 動(dòng)態(tài)添加實(shí)例變量
f.y = 2
print('x= {}, y = {}'.format(f.x, f.y))

正確,動(dòng)態(tài)添加的實(shí)例變量必須在__slots__已經(jīng)定義。

  1. 示例4
class Foo(object):
    __slots__ = ('x')
    var = 9
    def __init__(self):
        self.x = 10

f = Foo()
# # 動(dòng)態(tài)添加實(shí)例變量
f.y= 2
print('x= {}, y = {}'.format(f.x, f.y))

運(yùn)行錯(cuò)誤:AttributeError: 'Foo' object has no attribute 'y'。
原因: 動(dòng)態(tài)添加的屬性必須在__slots__中已經(jīng)定義

  1. 示例5
class Foo(object):
    __slots__ = ('x')
    x = 9
    def __init__(self):
        pass

f = Foo()

print('Foo.x: {}'.format(Foo.x))
print('f.x: {}'.format(f.x))
f.x = 99
print('f.x: {}'.format(f.x))

運(yùn)行錯(cuò)誤:alueError: 'x' in __slots__ conflicts with class variable
特別需要注意的是在Python2中,當(dāng)類變量與__slots__預(yù)定義的變量名沖突時(shí),沖突的變量變?yōu)橹蛔x。

  1. 示例6
    普通類與__slots__類的dir命令區(qū)別。
class Foo(object):
    __slots__ = ('x', 'y')
    def __init__(self):
        pass

f = Foo()

class Goo(object):
    def __init__(self):
        self.x = 1
        self.y = 2
g = Goo()

print(dir(Foo))
print(dir(Goo))
print(dir(f))
print(dir(g))

結(jié)果:

['__class__', '__delattr__', '__dir__', '__doc__', '__eq__', 
'__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', 
'__init__', '__init_subclass__', '__le__', '__lt__', '__module__', 
'__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', 
'__setattr__', '__sizeof__', **'__slots__'**, '__str__', 
'__subclasshook__', 'x', 'y']

['__class__', '__delattr__', '__dict__', '__dir__', '__doc__', '__eq__', 
'__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', 
'__init__', '__init_subclass__', '__le__', '__lt__', '__module__', 
'__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', 
'__setattr__', '__sizeof__', '__str__', '__subclasshook__', 
'__weakref__']

['__class__', '__delattr__', '__dir__', '__doc__', '__eq__', 
'__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', 
'__init__', '__init_subclass__', '__le__', '__lt__', '__module__', 
'__ne__', '__new__', '__reduce__', '__reduce_ex__', 
'__repr__','__setattr__', '__sizeof__', '__slots__', '__str__', 
'__subclasshook__', 'x', 'y']

['__class__', '__delattr__', '__dict__', '__dir__', '__doc__', '__eq__', 
'__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', 
'__init__', '__init_subclass__', '__le__', '__lt__', '__module__', 
'__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', 
'__setattr__', '__sizeof__', '__str__', '__subclasshook__', 
'__weakref__', 'x', 'y']

一個(gè)包含的是__slots__xy,普通類肯定包含__dict__。

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 一、Python簡(jiǎn)介和環(huán)境搭建以及pip的安裝 4課時(shí)實(shí)驗(yàn)課主要內(nèi)容 【Python簡(jiǎn)介】: Python 是一個(gè)...
    _小老虎_閱讀 6,334評(píng)論 0 10
  • 1.1==,is的使用 ·is是比較兩個(gè)引用是否指向了同一個(gè)對(duì)象(引用比較)。 ·==是比較兩個(gè)對(duì)象是否相等。 1...
    TENG書閱讀 790評(píng)論 0 0
  • 寫在前面的話 代碼中的# > 表示的是輸出結(jié)果 輸入 使用input()函數(shù) 用法 注意input函數(shù)輸出的均是字...
    FlyingLittlePG閱讀 3,214評(píng)論 0 9
  • Python語(yǔ)言特性 1 Python的函數(shù)參數(shù)傳遞 看兩個(gè)如下例子,分析運(yùn)行結(jié)果: 代碼一: a = 1 def...
    伊森H閱讀 3,177評(píng)論 0 15
  • 已經(jīng)是第三次看了,到現(xiàn)在為止,還想要看第四遍呢。究竟怎樣的電影呢?會(huì)有此般魔力,竟是這樣的深深吸引著我。 ...
    吳卓逸閱讀 538評(píng)論 2 3

友情鏈接更多精彩內(nèi)容