Friday Finking: Source code organisation

Cameron Simpson cs at cskk.id.au
Sat Dec 28 23:55:31 EST 2019


On 28Dec2019 19:19, Tim Chase <python.list at tim.thechases.com> wrote:
>Inside a class, I tend to roughly follow
>
>  __new__ (if present)
>  __init__
>  other dunder methods
>  subsequent methods alphabetically

I put the factory methods up near __init__, ahead of other methods.  But 
after most dunders. Here I mean things like this:

  class Foo:

    def __init__(....):

    @classmethod
    def from_bytes(cls, bs, ...):
      ... parse out of `bs` and then finish with something like ...
      return cls(parsed-stuff)

which calls the "conventional" __init__ as normal.

And I often group methods of similar purpose together, rather than 
alphabetically. For example, if there's a few methods for transribing 
the object, they might land together.

Cheers,
Cameron Simpson <cs at cskk.id.au>


More information about the Python-list mailing list