Semantic newlines.
This commit is contained in:
parent
cfc42ef222
commit
7818400ce7
|
@ -380,13 +380,14 @@ Converters are run *before* validators, so you can use validators to check the f
|
||||||
Slots
|
Slots
|
||||||
-----
|
-----
|
||||||
|
|
||||||
By default, instances of classes have a dictionary for attribute storage. This
|
By default, instances of classes have a dictionary for attribute storage.
|
||||||
wastes space for objects having very few instance variables. The space
|
This wastes space for objects having very few instance variables.
|
||||||
consumption can become acute when creating large numbers of instances.
|
The space consumption can become acute
|
||||||
|
when creating large numbers of instances.
|
||||||
|
|
||||||
Normal Python classes can avoid using a separate dictionary for each instance of
|
Normal Python classes can avoid using a separate dictionary
|
||||||
a class by defining ``__slots__``. ``attrs`` classes should just use
|
for each instance of a class by defining ``__slots__``.
|
||||||
``slots=True``.
|
``attrs`` classes should just use ``slots=True``.
|
||||||
|
|
||||||
|
|
||||||
.. doctest::
|
.. doctest::
|
||||||
|
@ -399,18 +400,20 @@ a class by defining ``__slots__``. ``attrs`` classes should just use
|
||||||
|
|
||||||
.. note::
|
.. note::
|
||||||
|
|
||||||
``attrs`` slot classes can inherit from other classes just like non-slot
|
``attrs`` slot classes can inherit from other classes
|
||||||
classes, but some of the benefits of slot classes are lost if you do that.
|
just like non-slot classes,
|
||||||
If you must inherit from other classes, try to inherit only from slot
|
but some of the benefits of slot classes are lost if you do that.
|
||||||
classes.
|
If you must inherit from other classes,
|
||||||
|
try to inherit only from other slot classes.
|
||||||
|
|
||||||
|
|
||||||
Slot classes are a little different than ordinary, dictionary-backed classes:
|
Slot classes are a little different than ordinary, dictionary-backed classes:
|
||||||
|
|
||||||
* assigning to a non-existent attribute of an instance will result in an
|
* assigning to a non-existent attribute of an instance
|
||||||
``AttributeError`` being raised. Depending on your needs, this might actually
|
will result in an ``AttributeError`` being raised.
|
||||||
be a good thing since it will let you catch typos early. This is not the case
|
Depending on your needs, this might actually be a good thing
|
||||||
if your class inherits from any non-slot classes.
|
since it will let you catch typos early.
|
||||||
|
This is not the case if your class inherits from any non-slot classes.
|
||||||
|
|
||||||
.. doctest::
|
.. doctest::
|
||||||
|
|
||||||
|
@ -425,8 +428,9 @@ Slot classes are a little different than ordinary, dictionary-backed classes:
|
||||||
...
|
...
|
||||||
AttributeError: 'Coordinates' object has no attribute 'z'
|
AttributeError: 'Coordinates' object has no attribute 'z'
|
||||||
|
|
||||||
* slot classes cannot share attribute names with their instances, while non-slot
|
* slot classes cannot share attribute names with their instances,
|
||||||
classes can. The following behaves differently if slot classes are used:
|
while non-slot classes can.
|
||||||
|
The following behaves differently if slot classes are used:
|
||||||
|
|
||||||
.. doctest::
|
.. doctest::
|
||||||
|
|
||||||
|
@ -441,10 +445,12 @@ Slot classes are a little different than ordinary, dictionary-backed classes:
|
||||||
>>> C.x
|
>>> C.x
|
||||||
<member 'x' of 'C' objects>
|
<member 'x' of 'C' objects>
|
||||||
|
|
||||||
* Since non-slot classes cannot be turned into slot classes after they have been
|
* since non-slot classes cannot be turned into slot classes
|
||||||
created, ``attr.s(.., slots=True)`` will actually replace the class it is
|
after they have been created,
|
||||||
applied to with a copy. In almost all cases this isn't a problem, but we
|
``attr.s(.., slots=True)`` will actually replace the class
|
||||||
mention it for the sake of completeness.
|
it is applied to with a copy.
|
||||||
|
In almost all cases this isn't a problem,
|
||||||
|
but we mention it for the sake of completeness.
|
||||||
|
|
||||||
|
|
||||||
Other Goodies
|
Other Goodies
|
||||||
|
|
Loading…
Reference in New Issue