Spreading a class over multiple files

Steven D'Aprano steve at pearwood.info
Sun Jun 5 06:57:45 EDT 2016


On Sun, 5 Jun 2016 06:14 pm, Mark Summerfield wrote:

> You're quite right! For some reason I have a blind-spot about mixins, but
> they are the perfect solution. Thanks:-)

They really aren't :-)

Just yesterday on an unrelated thread I linked to a long discussion about
multiple inheritance, mixins and traits. See links here:

https://mail.python.org/pipermail/python-list/2016-June/709808.html

To my mind, if you have to split a class over multiple files, it probably
does too much. The "God Class" that Peter referred to is an anti-pattern:

https://en.wikipedia.org/wiki/God_object

but if you still need to split your class over multiple files, this is how I
would do it:


# file a.py
class Zeus_King_Of_The_Gods:  # *wink*

    from b import throw_lightening
    from c import turn_into_a_shower_of_gold

    def turn_into_animal(self, animal='bull'):
        ...


# file b.py
def throw_lightening(self):
    ...


# file c.py
def turn_into_a_shower_of_gold(self):
    ...



Notice that in files b and c you define the functions with an explicit self,
even though they are at the top level.



-- 
Steven




More information about the Python-list mailing list