Is there any Python equivalent to partial classes?

前端 未结 9 1602
無奈伤痛
無奈伤痛 2020-11-27 15:31

Using \"new\" style classes (I\'m in python 3.2) is there a way to split a class over multiple files? I\'ve got a large class (which really should be a single class from an

9条回答
  •  谎友^
    谎友^ (楼主)
    2020-11-27 16:21

    First off, I don't see how splitting the class into multiple files makes editing any easier. A decent IDE should be able to find any method easily whether in one file or multiple; if you're not using a decent IDE, splitting the class means the maintainer has to guess which file a given method is in, which sounds harder rather than easier.

    More fundamentally, this class - so large that you want a special language feature just to support its weight - sounds fundamentally broken. How many lines of code are we talking about? Almost certainly, it would be a better idea to do one of:

    • Refactor duplicated code into fewer, more general primitives
    • Define a base class and extend it with subclasses as Karoly Horvath suggests in comments (this is the closest thing to the 'partial classes' that you're asking for that I would endorse)
    • Define a few separate classes to encapsulate different parts of this class's functionality, and compose this class of instances of those smaller ones.

提交回复
热议问题