Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python class design - Splitting up big classes into multiple ones to group functionality

Tags:

python

class

OK I've got 2 really big classes > 1k lines each that I currently have split up into multiple ones. They then get recombined using multiple inheritance. Now I'm wondering, if there is any cleaner/better more pythonic way of doing this. Completely factoring them out would result in endless amounts of self.otherself.do_something calls, which I don't think is the way it should be done.

To make things clear here's what it currently looks like:

from gui_events import GUIEvents # event handlers
from gui_helpers import GUIHelpers # helper methods that don't directly modify the GUI

# GUI.py
class GUI(gtk.Window, GUIEvents, GUIHelpers):
    # general stuff here stuff here

One problem that is result of this is Pylint complaining giving me trillions of "init not called" / "undefined attribute" / "attribute accessed before definition" warnings.

EDIT:
You may want to take a look at the code, to make yourself a picture about what the whole thing actually is.
http://github.com/BonsaiDen/Atarashii/tree/next/atarashii/usr/share/pyshared/atarashii/

Please note, I'm really trying anything to keep this thing as DRY as possible, I'm using pylint to detect code duplication, the only thing it complains about are the imports.

like image 479
Ivo Wetzel Avatar asked Mar 24 '10 03:03

Ivo Wetzel


People also ask

How do you split a class in Python?

Probably the most common approach to spreading a class's code over multiple files is to use subclassing, with the base class in one module, and the (or each) subclass, in its own separate module.

What is a mixin Python?

What is a mixin in Python. A mixin is a class that provides method implementations for reuse by multiple related child classes. However, the inheritance is not implying an is-a relationship. A mixin doesn't define a new type. Therefore, it is not intended for direction instantiation.

Should you have classes in separate files Python?

A module can consist of multiple classes or functions. As Python is not an OO language only, it does not make sense do have a rule that says, one file should only contain one class. One file (module) should contain classes / functions that belong together, i.e. provide similar functionality or depend on each other.

Can you have multiple classes in a Python file?

There is no limit on how many classes one can put in a file or a module.


2 Answers

If you want to use multiple inheritance to combine everything into one big class (it might make sense to do this), then you can refactor each of the parent classes so that every method and property is either private (starts with '__') or has a short 2-3 character prefix unique to that class. For example, all the methods and properties in your GUIEvents class could start with ge_, everything in GUIHelpers could start with gh_. By doing this, you'll get achieve some of the clarity of using separate sub-class instances (self.ge.doSomething() vs self.ge_doSomething()) and you'll avoid conflicting member names, which is the main risk when combining such large classes into one.

like image 97
too much php Avatar answered Oct 12 '22 17:10

too much php


Start by finding classes that model real world concepts that your application needs to work with. Those are natural candidates for classes.

Try to avoid multiple inheritance as much as possible; it's rarely useful and always somewhat confusing. Instead, look to use functional composition ("HAS-A" relationships) to give rich attributes to your objects made of other objects.

Remember to make each method do one small, specific thing; this necessarily entails breaking up methods that do too many things into smaller pieces.

Refactor cases where you find many such methods are duplicating each other's functionality; this is another way to find natural collections of functionality that deserve to be in a distinct class.

like image 34
bignose Avatar answered Oct 12 '22 19:10

bignose