Why NOT only one class per file?

Dillon Collins dmc5010 at psu.edu
Wed Apr 4 07:16:51 EDT 2007


On Wednesday 04 April 2007, Chris Lasher wrote:
> He doesn't find my arguments convincing, so I thought I'd ask here to
> see why the Python idiom is the way it is: why should we NOT be
> placing classes in their own separate files?

Because it really just provides as an artificial limitation that could only be 
annoying at best?  After all, programmers can put one class per file if the 
want anyway.

I know I would like Python less if I had to make a directory of files each 
with somehing like:

class MyError(Exception):
   pass

Of course, there are other cases as well where logically grouping classes is 
much nicer.  However, at the end of the day the question is almost 
meaningless because of the way Python treats classes.  
Examples:

class A(object):
    class B:
        pass
C=A.B

class A(object):
    pass
A=1

class A(object):
    pass
B=type('B', (object,), {})


The first creates a class with a contained class, but then "exports" the 
contained class as C.  The second creates class A, but then destroys it by 
writing over it with the value 1.  The third also creates a class A, but this 
time creates a top-level class B.  You'll note that the method of 
construction allows for B to be created dynamically.

So the idea of one class per file is almost meaningless.





More information about the Python-list mailing list