473,738 Members | 7,599 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 10883
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
1871
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
1262
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
2104
by: Jay | last post by:
Is it possible to split a class up in multiple files? if so how?
5
3677
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
2756
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
5055
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
1630
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
9910
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
1440
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
8788
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8210
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
6751
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
6053
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();...
0
4570
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 the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4825
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3279
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
2
2745
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2193
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.