473,320 Members | 1,821 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

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 10849
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(Person)):
.... 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_extend
# 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.__name__, name))
# module is added automatically
module = dict.pop('__module__', None)
if module is not None:
modules = getattr(extended, '__modules__', None)
if modules is None:
modules = extended.__modules__ = [extended.__module__]
modules.append(module)
# replace the docstring only if it is not None
doc = dict.pop('__doc__', None)
if doc is not None:
setattr(extended, '__doc__', doc)
# now patch the original class with all the new attributes
for attrname, value in dict.items():
setattr(extended, 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
Diez B. Roggisch wrote:
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.
While refactoring to avoid huge classes is usually excellent advice, it
actually is possible (and sometimes even useful) to merge classes
without using inheritance.

See, for example,
http://www.webwareforpython.org/Misc...les/MixIn.html

Regards,
Pat

Aug 8 '06 #11

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 do this:

file_1.py:

class A:
def __init__(self):
self.a = 1

file_2.py:
from file_1 import A

def seta(self,value):
self.a = value

setattr(A,"seta",seta)

but to use the "complete" class you should then import file_2 in other
source files.

bye
rm

Aug 8 '06 #12

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

Similar topics

4
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...
0
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?...
4
by: Jay | last post by:
Is it possible to split a class up in multiple files? if so how?
5
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...
7
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...
1
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
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();...
43
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...
0
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...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...

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.