|
| 1 | +.. _protocol-member-order: |
| 2 | + |
| 3 | +Protocol Member Order |
| 4 | +--------------------- |
| 5 | + |
| 6 | +The order of members inside a protocol class body. Settled by the owner as a |
| 7 | +ruling, so a new dissector and a refactor land in the same layout. |
| 8 | + |
| 9 | +Each group sits under its section comment, spelled as the repo spells it: |
| 10 | +``# Properties.``, ``# Methods.``, ``# Data models.`` and ``# Utilities.`` |
| 11 | + |
| 12 | +The Order |
| 13 | +~~~~~~~~~ |
| 14 | + |
| 15 | +#. **Properties.** Every ``@property`` member. |
| 16 | +#. **Methods.** Public methods: mostly ``id()``, ``read()``, ``make()`` and |
| 17 | + ``register_xxx()``. |
| 18 | +#. **Data models.** Every dunder name: ``__index__``, ``__length_hint__``, |
| 19 | + ``__init__``, ``__new__``, ``__post_init__`` and any other. |
| 20 | +#. **Utilities.** Everything else, mostly private methods. A ``_read_xxx()`` comes |
| 21 | + before its ``_make_xxx()``, and any such pair keeps that order. |
| 22 | + |
| 23 | +Example |
| 24 | +~~~~~~~ |
| 25 | + |
| 26 | +.. code-block:: python |
| 27 | +
|
| 28 | + class Foo(Protocol[Data_Foo, Schema_Foo]): |
| 29 | +
|
| 30 | + # Methods. |
| 31 | + def read(self, length, *, ...): |
| 32 | + ... |
| 33 | +
|
| 34 | + def make(self, ...): |
| 35 | + ... |
| 36 | +
|
| 37 | + # Utilities. |
| 38 | + def _read_foo_field(self, ...): |
| 39 | + ... |
| 40 | +
|
| 41 | + def _make_foo_field(self, ...): |
| 42 | + ... |
| 43 | +
|
| 44 | +The Reference |
| 45 | +~~~~~~~~~~~~~ |
| 46 | + |
| 47 | +:class:`~pcapkit.protocols.internet.ipv4.IPv4`, |
| 48 | +:class:`~pcapkit.protocols.transport.tcp.TCP`, |
| 49 | +:class:`~pcapkit.protocols.transport.udp.UDP`, |
| 50 | +:class:`~pcapkit.protocols.link.arp.ARP` and |
| 51 | +:class:`~pcapkit.protocols.link.ethernet.Ethernet` follow this order. When a |
| 52 | +new class is unclear, match them. |
0 commit comments