Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Organizing Python classes in modules and/or packages

I like the Java convention of having one public class per file, even if there are sometimes good reasons to put more than one public class into a single file. In my case I have alternative implementations of the same interface. But if I would place them into separate files, I'd have redundant names in the import statements (or misleading module names):

import someConverter.SomeConverter 

whereas someConverter would be the file (and module) name and SomeConverter the class name. This looks pretty inelegant to me. To put all alternative classes into one file would lead to a more meaningful import statement:

import converters.SomeConverter 

But I fear that the files become pretty large, if I put all related classes into a single module file. What is the Python best practise here? Is one class per file unusual?

like image 558
deamon Avatar asked Oct 01 '10 19:10

deamon


People also ask

How do you organize Python modules?

Organize your modules into packages. Each package must contain a special __init__.py file. Your project should generally consist of one top-level package, usually containing sub-packages. That top-level package usually shares the name of your project, and exists as a directory in the root of your project's repository.

Why should you organize your programs in functions modules and packages?

Modules and packages aren't just there to spread your Python code across multiple source files and directories—they allow you to organize your code to reflect the logical structure of what your program is trying to do.

What is the difference between modules and packages in Python?

A module is a file containing Python code. A package, however, is like a directory that holds sub-packages and modules.


2 Answers

Zach's solution breaks on Python 3. Here is a fixed solution.

A lot of it is personal preference. Using python modules, you do have the option to keep each class in a separate file and still allow for import converters.SomeConverter (or from converters import SomeConverter)

Your file structure could look something like this:

* converters      - __init__.py      - baseconverter.py      - someconverter.py      - otherconverter.py 

and then in your __init__.py file:

from converters.baseconverter import BaseConverter from converters.otherconverter import OtherConverter 
like image 34
Spundun Avatar answered Sep 22 '22 12:09

Spundun


A lot of it is personal preference. Using python modules, you do have the option to keep each class in a separate file and still allow for import converters.SomeConverter (or from converters import SomeConverter)

Your file structure could look something like this:

* converters      - __init__.py      - baseconverter.py      - someconverter.py      - otherconverter.py 

and then in your __init__.py file:

from baseconverter import BaseConverter from otherconverter import OtherConverter 
like image 67
Zach Avatar answered Sep 22 '22 12:09

Zach