About one class/function per module

Gabriel Genellina gagsl-py2 at yahoo.com.ar
Mon Nov 2 01:12:36 EST 2009


En Sun, 01 Nov 2009 22:27:32 -0300, Peng Yu <pengyu.ut at gmail.com> escribió:
> On Sun, Nov 1, 2009 at 7:02 PM, alex23 <wuwei23 at gmail.com> wrote:
>> On Nov 2, 8:11 am, Peng Yu <pengyu... at gmail.com> wrote:

>>> I prefer organized my code one class/function per file (i.e per module
>>> in python). I know the majority of programmers don't use this
>>> approach. Therefore, I'm wondering what its disadvantage is.
>>
>> You mean, what disadvantages it has _other_ than the ones you've been
>> experiencing?
>>
>> Aren't those enough to warrant actually working with Python's import
>> mechanism rather than against it?
>
> At least, I can use the following for now with one class/function per
> module. Unless this one class/function per module style have other
> disadvantages in term software engineering, I still can live with
> typing the class name (e.g. 'A') twice.
>
>  import test.A
>  a=test.A.A()
>
> So I am asking disadvantages besides python import mechanism is not
> friendly to it.

You don't type the class name twice. One 'A' is a module name, the other  
'A' is the class name.
In C++, a source file is just a compilation unit, only relevant for  
certain rules regarding visibility; it doesn't matter really in which  
source file the code is written in.
In Python, a source file becomes a module. A module is an object: it is  
created (usually by importing it), it has attributes, there are module  
hierarchies (packages), contains other objects... A module is an important  
object in Python. You should not confuse the module with the class (or  
classes) that are defined inside it. In your example above, test.A is a  
module, test.A.A is a class; they're not the same thing. You may put one  
class per file, nobody forbids that - but remember that the class and the  
module are different objects.
If you follow PEP8 conventions, module names are all in lowercase, and  
class names use TitleCase. This helps avoid confusion.

-- 
Gabriel Genellina




More information about the Python-list mailing list