Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > One module per class, bad idea?

Reply
Thread Tools

One module per class, bad idea?

 
 
Matias Jansson
Guest
Posts: n/a
 
      12-12-2006
I come from a background of Java and C# where it is common practise to have
one class per file in the file/project structure. As I have understood it,
it is more common practice to have many classes in a Python module/file.
What is the motivation behind it, would it be a bad idea to have a guideline
in your project that promotes a one class per file structure (assuming most
of the programmers a background similar to mine)?


 
Reply With Quote
 
 
 
 
billie
Guest
Posts: n/a
 
      12-12-2006
> would it be a bad idea to have a guideline
> in your project that promotes a one class per file structure (assuming most
> of the programmers a background similar to mine)?


Yes, it would be a bad idea. =)

 
Reply With Quote
 
 
 
 
Fredrik Lundh
Guest
Posts: n/a
 
      12-12-2006
Matias Jansson wrote:

> I come from a background of Java and C# where it is common practise to have
> one class per file in the file/project structure. As I have understood it,
> it is more common practice to have many classes in a Python module/file.


even more important is that in Python, you don't use classes for every-
thing; if you need factories, singletons, multiple ways to create
objects, polymorphic helpers, etc, you use plain functions, not classes
or static methods.

once you've gotten over the "it's all classes", use modules to organize
things in a way that makes sense to the code that uses your components.
make the import statements look good.

</F>

 
Reply With Quote
 
Fuzzyman
Guest
Posts: n/a
 
      12-12-2006

Matias Jansson wrote:
> I come from a background of Java and C# where it is common practise to have
> one class per file in the file/project structure. As I have understood it,
> it is more common practice to have many classes in a Python module/file.
> What is the motivation behind it, would it be a bad idea to have a guideline
> in your project that promotes a one class per file structure (assuming most
> of the programmers a background similar to mine)?


It's not a bad general guideline.

We try and use one class per file unless they are really trivial or
tightly coupled to another.

It allows you to be very specific in the naming of files and isolate
functionality.

We also have files with 'helper functions', which often have several
functions.

Fuzzyman
http://www.voidspace.org.uk/index2.shtml

 
Reply With Quote
 
Andy Dingley
Guest
Posts: n/a
 
      12-12-2006

Matias Jansson wrote:

> I come from a background of Java and C# where it is common practise to have
> one class per file in the file/project structure.


Don't confuse packages and files. Java commonly splits a package
across many files, Python binds a module to a single file. If you see
"Java package" as more comparable to "Python module" then the
difference in how many classes are in a file becomes unimportant.

Java also puts many classes in the same source file, if they're tightly
coupled (e.g. Swing UI). It spits them out into separate .class files
though.

 
Reply With Quote
 
Paddy
Guest
Posts: n/a
 
      12-12-2006


On Dec 12, 8:29 am, "Matias Jansson"
<(E-Mail Removed)> wrote:
> I come from a background of Java and C# where it is common practise to have
> one class per file in the file/project structure. As I have understood it,
> it is more common practice to have many classes in a Python module/file.
> What is the motivation behind it, would it be a bad idea to have a guideline
> in your project that promotes a one class per file structure (assuming most
> of the programmers a background similar to mine)?

Hi,
This is one of the cases where Java and C# common practice diverge from
Pythons.
You might try looking at the source to some of the standard modules to
see how things are done in Python.

- Paddy.

 
Reply With Quote
 
Isaac Rodriguez
Guest
Posts: n/a
 
      12-12-2006

> Yes, it would be a bad idea. =)


Saying it is a bad idea and not explaining why will not help anyone. I
would like you to elaborate on why it is a bad idea to have one file
per class.

Thanks,

- Isaac.

 
Reply With Quote
 
Isaac Rodriguez
Guest
Posts: n/a
 
      12-12-2006



> make the import statements look good.



You can still make your import statements look good and have one class
per file, that's one of the __init__.py wonderful features.

Also, C++ support stand alone functions and the rule is to organize
classes and their interface (functions that operate in objects of the
class are considered part of the interface) in their own module.

I will like to understand why this will not be a good idea for python,
other than to make beautiful import statements that is.

Thanks,

- Isaac.

 
Reply With Quote
 
Carl J. Van Arsdall
Guest
Posts: n/a
 
      12-12-2006
Isaac Rodriguez wrote:
>> Yes, it would be a bad idea. =)
>>

>
> Saying it is a bad idea and not explaining why will not help anyone. I
> would like you to elaborate on why it is a bad idea to have one file
> per class.
>

A module per class makes a lot of sense in some cases, or rather, make
your module your class (look at the singleton pattern). I actually like
to structure all of my code like this, it helps me keep things organized
and separated. I guess i'm not sure why it would ever be a really bad
idea, maybe if you had really small classes?

-c


--

Carl J. Van Arsdall
http://www.velocityreviews.com/forums/(E-Mail Removed)
Build and Release
MontaVista Software

 
Reply With Quote
 
Gabriel Genellina
Guest
Posts: n/a
 
      12-12-2006
On 12 dic, 16:17, "Isaac Rodriguez" <(E-Mail Removed)>
wrote:
> > Yes, it would be a bad idea. =)Saying it is a bad idea and not explaining why will not help anyone. I

> would like you to elaborate on why it is a bad idea to have one file
> per class.


The HyperText package (http://dustman.net/andy/python/HyperText) has a
lot of classes (about 90 in HTML40.py); one class per HTML tag. Most of
them are oneliners with "pass" alone. All are public. It would be
nonsense to split them on one file per class just because you have a
rule that says so.
On the other hand, it would be nonsense too to mix a bunch of unrelated
classes all inside a single module, just because you can do that.
Common sense is hard to measure, but required as any other programming
skills. (Anyone knows the story of Epaminondas and his auntie?)

--
Gabriel Genellina

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Quick one - Is SESSION per browser instance or per IP Address? =?Utf-8?B?S01aX3N0YXRl?= ASP .Net 7 04-10-2006 03:38 AM
ActiveX apologetic Larry Seltzer... "Sun paid for malicious ActiveX code, and Firefox is bad, bad bad baad. please use ActiveX, it's secure and nice!" (ok, the last part is irony on my part) fernando.cassia@gmail.com Java 0 04-16-2005 10:05 PM
24 Season 3 Bad Bad Bad (Spoiler) nospam@nospam.com DVD Video 12 02-23-2005 03:28 AM
Static class - one per JVM or one per app? junk1@davidbevan.co.uk Java 5 12-20-2004 08:26 PM
Static class - one per JVM or one per app? junk1@davidbevan.co.uk Java 0 12-20-2004 08:15 PM



Advertisments