温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

python魔法方法 __ slots __怎么使用

发布时间:2023-03-01 09:56:10 来源:亿速云 阅读:118 作者:iii 栏目:开发技术

这篇文章主要讲解了“python魔法方法 __ slots __怎么使用”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习“python魔法方法 __ slots __怎么使用”吧!

__ slots __

__slots__是python class的一个特殊attribute,能够节省内存空间。正常情况下,一个类的属性是以字典的形式来管理, 每个类都会有__ dict__ 方法。但是我们可以通过 设置 __ slots__ 来将类的属性构造成一个静态的数据结构来管理,里面存储的是 value references。

class Bar(object):
    def __init__(self, a):
        self.a = a


class BarSlotted(object):
    __slots__ = "a",

    def __init__(self, a):
        self.a = a


# create class instance
bar = Bar(1)
bar_slotted = BarSlotted(1)

print(set(dir(bar)) - set(dir(bar_slotted)))
# {'__dict__', '__weakref__'}   
'''
使用 __slots__ 后, 类里面会减少 __dict__  __weakref__ 两个方法。
__weakref__  --- 弱引用  详情链接 https://docs.python.org/zh-cn/3/library/weakref.html
'''

优点:

  • 节约内存,不用去定义动态数据接口 __ dict__ 的内存, __ weakref__ 也不用去申请

  • access attributes 更快,静态数据结构,比__ dict__ 更快

缺点:

定义死后,不能去申请新的属性,申请会报属性错误

python魔法方法 __ slots __怎么使用

可以通过 把 __ dict__ 作为 __ slots__ 的一个属性,实现既能通过定义__ slots__ 节约内存,又实现新属性的定义。

class BarSlotted(object):
    __slots__ = "a",'__dict__'

    def __init__(self, a):
        self.a = a
        
bar_slotted = BarSlotted(1)
bar_slotted.b = "111"

当你事先知道class的attributes的时候,建议使用slots来节省memory以及获得更快的attribute access。

注意不应当用来限制__slots__之外的新属性作为使用__slots__的原因,可以使用装饰器以及反射的方式来实现属性控制。

注意事项

子类会继承父类的 __ slots__

class Parent(object):
    __slots__ = "x", "y"


class Child(Parent):
    __slots__ = "z",
    # 重复的 x, y 可以不写,
    # __slots__ = "x", "y", "z"


child = Child()
print(dir(child)) 
# [..., 'x', 'y', 'z']

不支持多继承, 会直接报错

class ParentA(object):
    __slots__ = "x",


class ParentB(object):
    __slots__ = "y",


class Child(ParentA, ParentB):
    pass

'''
Traceback (most recent call last):
  File "C:/Users/15284/PycharmProjects/pythonProject/test.py", line 69, in <module>
    class Child(ParentA, ParentB):
TypeError: multiple bases have instance lay-out conflict
'''

只允许父类中有一方设定了 __ slots__

class AbstractA(object):
  __slots__ = ()

class AbstractB(object):
  __slots__ = "x"

class Child(AbstractA, AbstractB):
  __slots__ = "x", "y"

新版本的 pickle中的 pickle含有slotted class,使用时需要注意。

感谢各位的阅读,以上就是“python魔法方法 __ slots __怎么使用”的内容了,经过本文的学习后,相信大家对python魔法方法 __ slots __怎么使用这一问题有了更深刻的体会,具体使用情况还需要大家实践验证。这里是亿速云,小编将为大家推送更多相关知识点的文章,欢迎关注!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI