Review feedback incorporated:
* capitalize bullet items * replace bullet asterisks with dashes * indent bullet code fragments * join some lines
This commit is contained in:
parent
7818400ce7
commit
adb08a187a
|
@ -382,11 +382,9 @@ Slots
|
||||||
|
|
||||||
By default, instances of classes have a dictionary for attribute storage.
|
By default, instances of classes have a dictionary for attribute storage.
|
||||||
This wastes space for objects having very few instance variables.
|
This wastes space for objects having very few instance variables.
|
||||||
The space consumption can become acute
|
The space consumption can become acute when creating large numbers of instances.
|
||||||
when creating large numbers of instances.
|
|
||||||
|
|
||||||
Normal Python classes can avoid using a separate dictionary
|
Normal Python classes can avoid using a separate dictionary for each instance of a class by defining ``__slots__``.
|
||||||
for each instance of a class by defining ``__slots__``.
|
|
||||||
``attrs`` classes should just use ``slots=True``.
|
``attrs`` classes should just use ``slots=True``.
|
||||||
|
|
||||||
|
|
||||||
|
@ -400,57 +398,47 @@ for each instance of a class by defining ``__slots__``.
|
||||||
|
|
||||||
.. note::
|
.. note::
|
||||||
|
|
||||||
``attrs`` slot classes can inherit from other classes
|
``attrs`` slot classes can inherit from other classes just like non-slot 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 other slot classes.
|
||||||
but some of the benefits of slot classes are lost if you do that.
|
|
||||||
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
|
- Assigning to a non-existent attribute of an instance will result in an ``AttributeError`` being raised.
|
||||||
will result in an ``AttributeError`` being raised.
|
Depending on your needs, this might be a good thing since it will let you catch typos early.
|
||||||
Depending on your needs, this might actually be a good thing
|
|
||||||
since it will let you catch typos early.
|
|
||||||
This is not the case if your class inherits from any non-slot classes.
|
This is not the case if your class inherits from any non-slot classes.
|
||||||
|
|
||||||
.. doctest::
|
.. doctest::
|
||||||
|
|
||||||
>>> @attr.s(slots=True)
|
>>> @attr.s(slots=True)
|
||||||
... class Coordinates(object):
|
... class Coordinates(object):
|
||||||
... x = attr.ib()
|
... x = attr.ib()
|
||||||
... y = attr.ib()
|
... y = attr.ib()
|
||||||
...
|
...
|
||||||
>>> c = Coordinates(x=1, y=2)
|
>>> c = Coordinates(x=1, y=2)
|
||||||
>>> c.z = 3
|
>>> c.z = 3
|
||||||
Traceback (most recent call last):
|
Traceback (most recent call last):
|
||||||
...
|
...
|
||||||
AttributeError: 'Coordinates' object has no attribute 'z'
|
AttributeError: 'Coordinates' object has no attribute 'z'
|
||||||
|
|
||||||
* slot classes cannot share attribute names with their instances,
|
- Slot classes cannot share attribute names with their instances, while non-slot classes can.
|
||||||
while non-slot classes can.
|
|
||||||
The following behaves differently if slot classes are used:
|
The following behaves differently if slot classes are used:
|
||||||
|
|
||||||
.. doctest::
|
.. doctest::
|
||||||
|
|
||||||
>>> @attr.s
|
>>> @attr.s
|
||||||
... class C(object):
|
... class C(object):
|
||||||
... x = attr.ib()
|
... x = attr.ib()
|
||||||
>>> C.x
|
>>> C.x
|
||||||
Attribute(name='x', default=NOTHING, validator=None, repr=True, cmp=True, hash=True, init=True, convert=None)
|
Attribute(name='x', default=NOTHING, validator=None, repr=True, cmp=True, hash=True, init=True, convert=None)
|
||||||
>>> @attr.s(slots=True)
|
>>> @attr.s(slots=True)
|
||||||
... class C(object):
|
... class C(object):
|
||||||
... x = attr.ib()
|
... x = attr.ib()
|
||||||
>>> C.x
|
>>> C.x
|
||||||
<member 'x' of 'C' objects>
|
<member 'x' of 'C' objects>
|
||||||
|
|
||||||
* since non-slot classes cannot be turned into slot classes
|
- Since non-slot classes cannot be turned into slot classes after they have been created, ``attr.s(.., slots=True)`` will actually replace the class it is applied to with a copy.
|
||||||
after they have been created,
|
In almost all cases this isn't a problem, but we mention it for the sake of completeness.
|
||||||
``attr.s(.., slots=True)`` will actually replace the class
|
|
||||||
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