473,548 Members | 2,683 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

One module per class, bad idea?

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)?
Dec 12 '06 #1
32 5771
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. =)

Dec 12 '06 #2
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>

Dec 12 '06 #3

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

Dec 12 '06 #4

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.

Dec 12 '06 #5


On Dec 12, 8:29 am, "Matias Jansson"
<matias.jans... @carmenconsulti ng.comwrote:
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.

Dec 12 '06 #6
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.

Dec 12 '06 #7


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.

Dec 12 '06 #8
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
cv*********@mvi sta.com
Build and Release
MontaVista Software

Dec 12 '06 #9
On 12 dic, 16:17, "Isaac Rodriguez" <isaac.rodrig.. .@comcast.net>
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

Dec 12 '06 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
15412
by: BH | last post by:
Hi what would be the C# equivalent of a VB.NET file that looks like this: Module Utilities Public Sub UtilitySubOne ( ByVal arg As String) // blah blah End Sub
0
1050
by: chris.atlee | last post by:
Hi there, I haven't seen this topic pop up in a while, so I thought I'd raise it again... What is the status of the path module/class PEP? Did somebody start writing one, or did it die? I would really like to see something like Jason Orendorff's path class make its way into the python standard library.
9
2337
by: Rudy | last post by:
Hello All! I'm a little confused on Public Class or Modules. Say I have a this on form "A" Public Sub Subtract() Dim Invoice As Decimal Dim Wage As Decimal Static PO As Decimal Invoice = CDec(txbInv.Text) Wage = CDec(txbTotWage.Text)
7
1954
by: hg | last post by:
Hi, I have the following problem. I find in a directory hierarchy some files following a certain sets of rules: ..../.../../plugin/name1/name1.py ..... ..../.../../plugin/namen/namen.py
6
3456
by: Silfheed | last post by:
Heyas So we have the following situation: we have a testee.py that we want to automatically test out and verifiy that it is worthy of being deployed. We want our tester.py to test the code for testee.py without changing the code for testee.py. testee.py has a module in it that we want to mock in some tests and in others use the real...
7
3048
by: Brad Pears | last post by:
I am developing a simple error message class and would like the be able to generate as part of the message the curent class/module/form and function or sub that the error was generated in without having to hardcode this info to be passed to the error message class. I can get the stack trace using system.environment.stacktrace - but this is too...
0
808
by: thirunavukarasukm | last post by:
Hai.. Is thers possible to use module class in web application.. in web application i have one module class.. the module class one variable declared to assing particular value then i am go to mywebpage to print that modulevariable
0
7512
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7707
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
1
7466
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
6036
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5362
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5082
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3495
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
1
1926
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1051
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.