From 955f12a094b1f6870c49f97b5a16d659811e282b Mon Sep 17 00:00:00 2001 From: Aaron Hall Date: Thu, 25 May 2017 14:12:16 -0700 Subject: [PATCH 1/3] correct __slots__ documentation with minimal changes --- Doc/reference/datamodel.rst | 33 +++++++++++++++------------------ 1 file changed, 15 insertions(+), 18 deletions(-) diff --git a/Doc/reference/datamodel.rst b/Doc/reference/datamodel.rst index 600a71aa4125d2..b78ef8fd67cf4d 100644 --- a/Doc/reference/datamodel.rst +++ b/Doc/reference/datamodel.rst @@ -1614,30 +1614,25 @@ instances cannot override the behavior of a property. __slots__ ^^^^^^^^^ -By default, instances of classes have a dictionary for attribute storage. This -wastes space for objects having very few instance variables. The space -consumption can become acute when creating large numbers of instances. - -The default can be overridden by defining *__slots__* in a class definition. -The *__slots__* declaration takes a sequence of instance variables and reserves -just enough space in each instance to hold a value for each variable. Space is -saved because *__dict__* is not created for each instance. +*__slots__* allow us to explicitly declare data members (like +properties) and deny the creation of *__dict__* and *__weakref__* +(unless explicitly declared in *__slots__* or available in a parent.) +The space saved over using *__dict__* can be significant. .. data:: object.__slots__ - This class variable can be assigned a string, iterable, or sequence of - strings with variable names used by instances. *__slots__* reserves space - for the declared variables and prevents the automatic creation of *__dict__* - and *__weakref__* for each instance. + This class variable can be assigned a string, iterable, sequence of + strings, or mapping with variable names used by instances. + *__slots__* reserves space for the declared variables and prevents + the automatic creation of *__dict__* and *__weakref__* for each instance. Notes on using *__slots__* """""""""""""""""""""""""" -* When inheriting from a class without *__slots__*, the *__dict__* attribute of - that class will always be accessible, so a *__slots__* definition in the - subclass is meaningless. +* When inheriting from a class without *__slots__*, the *__dict__* and + *__weakref__* attribute of the instances will always be accessible. * Without a *__dict__* variable, instances cannot be assigned new variables not listed in the *__slots__* definition. Attempts to assign to an unlisted @@ -1656,9 +1651,11 @@ Notes on using *__slots__* *__slots__*; otherwise, the class attribute would overwrite the descriptor assignment. -* The action of a *__slots__* declaration is limited to the class where it is - defined. As a result, subclasses will have a *__dict__* unless they also define - *__slots__* (which must only contain names of any *additional* slots). +* The action of a *__slots__* declaration is not limited to the class + where it is defined. *__slots__* declared in parents are available in + child classes. However, child subclasses will get a *__dict__* and + *__weakref__* unless they also define *__slots__* (which should only + contain names of any *additional* slots). * If a class defines a slot also defined in a base class, the instance variable defined by the base class slot is inaccessible (except by retrieving its From 501c3411b00fd947bab0fed41ccf263d185c4fe5 Mon Sep 17 00:00:00 2001 From: Aaron Hall Date: Thu, 25 May 2017 14:40:37 -0700 Subject: [PATCH 2/3] add multiple inheritance info --- Doc/reference/datamodel.rst | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/Doc/reference/datamodel.rst b/Doc/reference/datamodel.rst index b78ef8fd67cf4d..c70c5384d6d24e 100644 --- a/Doc/reference/datamodel.rst +++ b/Doc/reference/datamodel.rst @@ -1671,6 +1671,10 @@ Notes on using *__slots__* * *__class__* assignment works only if both classes have the same *__slots__*. +* Multiple inheritance with multiple slotted parent classes can be used, + but only one parent is allowed to have attributes created by slots + (the other bases must have empty slot layouts) - violations raise + :exc:`TypeError`. .. _class-customization: From 7b9d3091c14d92461c780bb848fad4eb00dd6854 Mon Sep 17 00:00:00 2001 From: Aaron Hall Date: Thu, 25 May 2017 22:05:28 -0700 Subject: [PATCH 3/3] remove mapping from description --- Doc/reference/datamodel.rst | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/Doc/reference/datamodel.rst b/Doc/reference/datamodel.rst index c70c5384d6d24e..9708688c5ce1a5 100644 --- a/Doc/reference/datamodel.rst +++ b/Doc/reference/datamodel.rst @@ -1622,10 +1622,10 @@ The space saved over using *__dict__* can be significant. .. data:: object.__slots__ - This class variable can be assigned a string, iterable, sequence of - strings, or mapping with variable names used by instances. - *__slots__* reserves space for the declared variables and prevents - the automatic creation of *__dict__* and *__weakref__* for each instance. + This class variable can be assigned a string, iterable, or sequence of + strings with variable names used by instances. *__slots__* reserves space + for the declared variables and prevents the automatic creation of *__dict__* + and *__weakref__* for each instance. Notes on using *__slots__* pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy