How does inheritance of __slots__ in subclasses actually work?

前端 未结 5 1626
南笙
南笙 2020-12-02 08:29

In the Python data model reference section on slots there is a list of notes on using __slots__. I am thoroughly confused by the 1st and 6th items, because the

5条回答
  •  無奈伤痛
    2020-12-02 09:23

    From the answer you linked:

    The proper use of __slots__ is to save space in objects. Instead of having a dynamic dict...

    "When inheriting from a class without __slots__, the __dict__ attribute of that class will always be accessible", so adding your own __slots__ cannot prevent objects from having a __dict__, and cannot save space.

    The bit about __slots__ not being inherited is a little obtuse. Remember that it's a magic attribute and doesn't behave like other attributes, then re-read that as saying this magic slots behavior isn't inherited. (That's really all there is to it.)

提交回复
热议问题