473,698 Members | 2,403 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

is it possible to dividing up a class in multiple files?

Hi there,

is it possible to put the methods of a class in different files? I just
want to order them and try to keep the files small.

Regards
Martin
Aug 7 '06 #1
11 10877
Martin Höfling wrote:
is it possible to put the methods of a class in different files? I just
want to order them and try to keep the files small.
No, its not possible. What you can do is to create several classes and one
that inherits from all of them.

Better yet is to not write huge classes and getting rid of strange
conventions or views that make the problem appear as such. To explain that:
I've never felt the need to spread a class over several files - au
contraire, I despise Java for forcing me to only have one top level class
per file.

Diez
Aug 7 '06 #2
Hi Martin,

I don't think that's possible, since a file is executed when it is
imported. If you load a file which contains a "partial" class, you
will get an error because the indentation is incorrect, or the
methods will be loaded in the wrong namespace.

Regards,
Michiel

Op 7-aug-2006, om 15:41 heeft Martin Höfling het volgende geschreven:
Hi there,

is it possible to put the methods of a class in different files? I
just
want to order them and try to keep the files small.

Regards
Martin
--
http://mail.python.org/mailman/listinfo/python-list
Aug 7 '06 #3

Martin Höfling wrote:
Hi there,

is it possible to put the methods of a class in different files? I just
want to order them and try to keep the files small.

Regards
Martin
I ran across pyp the other day. It may be what you're wanting.

http://www.freenet.org.nz/python/pyp/

Aug 7 '06 #4
Martin Höfling:
is it possible to put the methods of a class in different files? I just
want to order them and try to keep the files small.
Well, you can create one or more modules filled with nude methods, and
you can define a class inside another module, and then add the methods
to this last class using a little helper function.

Bye,
bearophile

Aug 7 '06 #5
Martin Höfling wrote:
Hi there,

is it possible to put the methods of a class in different files? I just
want to order them and try to keep the files small.

Regards
Martin
You could use something like this:

"""
Example usage:
>>class Person(object):
.... def __init__(self, first, last):
.... self.first = first
.... self.last = last
....
>>john = Person('John', 'Smith')
jane = Person('Jane', 'Smith')
class Person(extend(P erson)):
.... def fullname(self):
.... return self.first + ' ' + self.last
....
>>john.fullname ()
'John Smith'
>>jane.fullname ()
'Jane Smith'
"""

def extend(cls):
extender = object.__new__( Extender)
extender.class_ to_extend = cls
return extender
class Extender(object ):

def __new__(cls, name, bases, dict):
# check that there is only one base
base, = bases
extended = base.class_to_e xtend
# names have to be equal otherwise name mangling wouldn't work
if name != extended.__name __:
msg = "class names are not identical: expected %r, got %r"
raise ValueError(msg % (extended.__nam e__, name))
# module is added automatically
module = dict.pop('__mod ule__', None)
if module is not None:
modules = getattr(extende d, '__modules__', None)
if modules is None:
modules = extended.__modu les__ = [extended.__modu le__]
modules.append( module)
# replace the docstring only if it is not None
doc = dict.pop('__doc __', None)
if doc is not None:
setattr(extende d, '__doc__', doc)
# now patch the original class with all the new attributes
for attrname, value in dict.items():
setattr(extende d, attrname, value)
return extended
Ziga

Aug 7 '06 #6
Martin Höfling wrote:
Hi there,

is it possible to put the methods of a class in different files? I just
want to order them and try to keep the files small.
Technically, yes - but in a somewhat hackish way.

But you *really* should not have such a need at first. Smells like a
design (or coding) problem to me - FWIW, very few of my source files are
1 KLOC, and they usually contains many classes, functions and other
definitions.

Aug 7 '06 #7
Ant

Martin Höfling wrote:
Hi there,

is it possible to put the methods of a class in different files? I just
want to order them and try to keep the files small.
The editor leo (http://webpages.charter.net/edreamleo/front.html) gives
you a way of handling large files in this way without actually having
to split the class between files. A bit like a more powerful form of
folding and narrowing text that some other editors have.

But like others have noted, it is probably an indication that the class
could use a bit of refactoring...

Aug 7 '06 #8
Thanks for your suggestions, precompiling is not an option, cause I
can't introduce extra dependencies from a precompiler.
Better yet is to not write huge classes and getting rid of strange
conventions or views that make the problem appear as such. To explain that:
I've never felt the need to spread a class over several files - au
contraire, I despise Java for forcing me to only have one top level class
per file.
You're probably right. I'll think about it if it's possible to move some
stuff out of the class.

Thanks
Martin
Aug 7 '06 #9
On Mon, 2006-08-07 at 15:41 +0200, Martin Höfling wrote:
Hi there,

is it possible to put the methods of a class in different files? I just
want to order them and try to keep the files small.
Here's how I do it:

Firstly, I create a file called imports.py which contains all my import
statements. These are obviously indented at the zero position.

I then create a classname.py file. This just has the class statement
(eg: class Foo:)
..

I then create a separate file for each function in the class. Eg:
init.py, function1.py, etc). These must be indented by one indent
position.

I then create a main.py file. This contains statements to be executed
after all the statements in the class.

I create a shell script which concatenates the files in the correct
order

eg:

cat imports.py \
classname.py \
init.py \
function1.py \
....
....
...
main.py module.py

I use the concatenated file, module.py, as the program to run or import.

I find it easier to maintain code in this way - if I need to make a
change to a particular function, I just edit the file in which the
function is defined and re-run the shell script to make the program. If
I need to add a new function to the class I just need to create a new
file and add its entry to the shell script in the correct place and
re-run the shell script.

Of course you are not limited to a single function per file, but that is
what works best for me.

Regards,

John

--
This message has been scanned for viruses and
dangerous content by MailScanner, and is
believed to be clean.

Aug 8 '06 #10

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

Similar topics

4
1868
by: Kim Chee | last post by:
I'm needing to get X number of files (.txt,.doc,.xls,.tiff,.pdf) into a single Print Job. The idea here is that a user searches for criteria in a system and then builds a batch of documents that need to be printed together (so that no one else on the network can slip in anything until this job completes). Any suggestions would be very very much appreciated. I'm at my wits end on this one!!
0
1258
by: CS | last post by:
Dear all, I would like to know whether it is possible to send multiple files in 1 fax? I noticed from some sample code, I can only specify 1 faxdocument.body in the code. Is there a workaround? Any help is appreciated.
4
2102
by: Jay | last post by:
Is it possible to split a class up in multiple files? if so how?
5
3673
by: Stanav | last post by:
Hello all, Thanks in advance for any replies... Now, my question is: Is it possible to do a multiple files download for a single response event on an aspx page? If there is, please give me some directions/sample code or anything. On my web application, I have a CheckBoxList that shows all the files. The user will check (select) the files to download, then click on the "Download" button. A SaveAs dialog pops up to allow the user to...
7
2754
by: crowl | last post by:
Hi there, I am looking for a component allowing me uploading multiple files by my asp page. I have found several components achieving this by require a <input type="file" name="FileX"> field for each file to upload. I want to allow my visitor to select the files to upload in the "Open" window. In the "Open" window it should be possible to select multiple files. Does such an asp component exist?
1
5050
by: MobileBoy36 | last post by:
Hi all, Is it possible to add multiple files to an archive using Gzipstream? How can it be done then? best regards, Mobileboy
3
1627
by: fatima.issawi | last post by:
Hello, Is it possible to download multiple files? Right now I am using the following code in a loop, but I can only save one file - the first one. Response.Clear(); Response.AddHeader("Content-Disposition", "attachment; filename=" + fileName); Response.AddHeader("Content-Length", "10"); Response.ContentType = "application/octet-stream";
43
9887
by: bonneylake | last post by:
Hey Everyone, Well this is my first time asking a question on here so please forgive me if i post my question in the wrong section. What i am trying to do is upload multiple files like gmail does. I found a script that does this on easycfm.com (Topic 13543). But anyway when i try to upload multiple files it will create multiple records in my database (like it should), but it wont upload multiple files. What ever file i choose to upload...
0
1438
by: metalheadstorm | last post by:
I have a tcp client - server implementation running in the same program, on different background worker threads. There will be instances of this program on multiple computers so they can send and receive files between each other. I can send files sequentially between computers using network stream, but how would I send multiple files at the same time from computer A to B. Sending multiple files over one connection ( socket ) is fine, but...
0
9169
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. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9030
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8899
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 most users, this new feature is actually very convenient. If you want to control the update process,...
0
8871
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7738
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 launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6528
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 instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5861
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 into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
2
2335
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2007
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.