Is there any Python equivalent to partial classes?

前端 未结 9 1608
無奈伤痛
無奈伤痛 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:31

    First I'd like to say that something this complicated it probably not a good idea just to make finding your place in the class easier - it would be best to add comments, highlight sections etc. However, I see two ways you could do this:

    1. Write the class in several files, then read them in as text, concatenate them and exec the resulting string.

    2. Create a separate class in each file, then inherit them all into a master class as mixins. However, if you're subclassing another class already this could lead to MRO problems. You could get around this by creating a metaclass for your master class which manually resolves the MRO, but this could get messy.

    The easiest would be the first option.

提交回复
热议问题