Limits on number of classes?

Tim Peters tim.one at comcast.net
Sun Jun 13 13:22:28 EDT 2004


[Kamilche]
> My design reqires hundreds of classes, maybe over a thousand. Can Python
> handle that?

Sure.

> Will there be a speed hit?

Compared to what?  The only part of Python that takes time proportional to
the number of classes is the time it takes for cyclic garbage collection to
stare at the classes from time to time, and determine that they're not yet
trash.  That's not unique to class objects, though, that's an expense
accruing to every kind of container (classes, instances, lists, tuples,
...).

If your inheritance chain is a thousand levels deep, then (a) it's going to
take a long time to resolve a method defined in the base class when accessed
from an instance of the most-derived class; and, (b) you're insane <wink>.

> Just wondering if anyone had hit the limits of Python.

The number of class objects is limited by your virtual memory (each class
object consumes memory, of course).  Base Zope contains about 2,500 Python
classes, and Zope3 about 4,500 so far.  The only problem that causes is
confusion at times.  For example, there are 12 distinct classes named "B" in
Zope3 so far.  Most are throwaways in tests.  When you're staring at a test
using class B, it's sometimes far from obvious exactly which of the dozen B
classes it thinks it's using.






More information about the Python-list mailing list