PaleNeutron
PaleNeutron

Reputation: 3215

Best practice for python module with thousands of classes in one file

I have a generated python module file with thousands of data classes, size 6MB.

These classes are generated from database structure, so I can not reduce them or not provide them with my module.

This module cause two main problem:

  1. Very slow importing
  2. Damage performance of IDE

the number of classes I currently used in this module is less than 30, but I have to provide all of them in my module since other users may use some of them.

My current code looks like:

foo.py:

from .models import ClassA, ClassB


class Foo():

    def __init__(slef):
        self.a = ClassA()
        self.b = ClassB()

__init__.py :

from .foo import Foo
from . import models

What's the right way to organize the classes in the module?

Upvotes: 0

Views: 437

Answers (1)

Richard Plester
Richard Plester

Reputation: 54

You can have the classes available in one module without having to import them all into another module where you intend to use them.

Say "6megModule.py" contains classes class1..class9999

from 6megModule import class1, class2, class3

If you can give more details about how you have designed your classes others may be able to help with structuring them, is there a good reason for having so many classes? Will your users be able to wade through such a massive amount to find and use the classes they actually need?

Upvotes: 1

Related Questions