zoukankan      html  css  js  c++  java
  • python __slot__

    使用slot能降低内存使用,同时也不能对实例新增属性。

    For classes that primarily serve as simple data structures, you can often greatly reduce the memory footprint of instances by adding the __slots__ attribute to the class defi‐ nition. For example:

    class Date:
    __slots__ = ['year', 'month', 'day'] def __init__(self, year, month, day):

                self.year = year
                self.month = month
                self.day = day
    

    When you define __slots__, Python uses a much more compact internal representation for instances. Instead of each instance consisting of a dictionary, instances are built around a small fixed-sized array, much like a tuple or list. Attribute names listed in the __slots__ specifier are internally mapped to specific indices within this array. A side effect of using slots is that it is no longer possible to add new attributes to instances— you are restricted to only those attribute names listed in the __slots__ specifier 

  • 相关阅读:
    MySQL 8.0+ 时区问题
    SSM框架整合搭建流程
    最大子段和、最大子矩阵和
    棋盘覆盖(分治)
    石子合并问题
    矩阵连乘
    selenium完成滑块验证
    背包问题(2)
    背包问题(1)
    皇后问题
  • 原文地址:https://www.cnblogs.com/sooflow/p/6202619.html
Copyright © 2011-2022 走看看