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

  • Thread starter =?ISO-8859-15?Q?Martin_H=F6fling?=
  • Start date
?

=?ISO-8859-15?Q?Martin_H=F6fling?=

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
 
M

Michiel Sikma

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:
 
D

Diez B. Roggisch

Martin said:
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
 
B

bearophileHUGS

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
 
Z

Ziga Seilnacht

Martin said:
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:
.... def __init__(self, first, last):
.... self.first = first
.... self.last = last
........ def fullname(self):
.... return self.first + ' ' + self.last
....'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
 
B

Bruno Desthuilliers

Martin said:
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.
 
A

Ant

Martin said:
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...
 
G

Guest

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
 
J

John McMonagle

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
 
P

Patrick Maupin

Diez said:
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/MiscUtils/Docs/Source/Files/MixIn.html

Regards,
Pat
 
R

roelmathys

Martin said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top