_slots_简介2-使用

  1. __slots__作用
  • 类成员变量描述符
  • __slots__是一个元祖
  • 类的实例只能拥有__slots__中定义的变量
  • 定义了__slots__之后就不再有__dict__属性
  • 在Python3中,类变量不能与__slots__预定义的变量名重名
  1. 示例1
class Foo(object):
    __slots__ = ('x')
    var = 9
    def __init__(self):
        self.a = 10

f = Foo()

运行时错误AttributeError: 'Foo' object has no attribute 'a'
原因:不能定义名称为a的类实例变量

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

f = Foo()
print(f.__dict__)

运行错误,拥有__slots__的类不再有__dict__属性。

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

f = Foo()
# # 动态添加实例变量
f.y = 2
print('x= {}, y = {}'.format(f.x, f.y))

正确,动态添加的实例变量必须在__slots__已经定义。

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

f = Foo()
# # 动态添加实例变量
f.y= 2
print('x= {}, y = {}'.format(f.x, f.y))

运行错误:AttributeError: 'Foo' object has no attribute 'y'
原因: 动态添加的属性必须在__slots__中已经定义

  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))

运行错误:alueError: 'x' in __slots__ conflicts with class variable
特别需要注意的是在Python2中,当类变量与__slots__预定义的变量名冲突时,冲突的变量变为只读

  1. 示例6
    普通类与__slots__类的dir命令区别。
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))

结果:

['__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']

一个包含的是__slots__xy,普通类肯定包含__dict__

©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • 一、Python简介和环境搭建以及pip的安装 4课时实验课主要内容 【Python简介】: Python 是一个...
    _小老虎_阅读 6,384评论 0 10
  • 1.1==,is的使用 ·is是比较两个引用是否指向了同一个对象(引用比较)。 ·==是比较两个对象是否相等。 1...
    TENG书阅读 797评论 0 0
  • 写在前面的话 代码中的# > 表示的是输出结果 输入 使用input()函数 用法 注意input函数输出的均是字...
    FlyingLittlePG阅读 3,281评论 0 9
  • Python语言特性 1 Python的函数参数传递 看两个如下例子,分析运行结果: 代码一: a = 1 def...
    伊森H阅读 3,197评论 0 15
  • 已经是第三次看了,到现在为止,还想要看第四遍呢。究竟怎样的电影呢?会有此般魔力,竟是这样的深深吸引着我。 ...
    吴卓逸阅读 573评论 2 3

友情链接更多精彩内容