-
__slots__作用
- 類成員變量描述符
-
__slots__是一個(gè)元祖 - 類的實(shí)例只能擁有
__slots__中定義的變量 - 定義了
__slots__之后就不再有__dict__屬性 - 在Python3中,類變量不能與
__slots__預(yù)定義的變量名重名
- 示例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í)例變量
- 示例2
class Foo(object):
__slots__ = ('x')
var = 9
def __init__(self):
self.x = 10
f = Foo()
print(f.__dict__)
運(yùn)行錯(cuò)誤,擁有__slots__的類不再有__dict__屬性。
- 示例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)定義。
- 示例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)定義
- 示例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。
- 示例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__,x和 y,普通類肯定包含__dict__。