Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

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

10 views
Skip to first unread message

Martin Höfling

unread,
Aug 7, 2006, 9:41:34 AM8/7/06
to
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

Diez B. Roggisch

unread,
Aug 7, 2006, 9:50:44 AM8/7/06
to
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

Michiel Sikma

unread,
Aug 7, 2006, 9:48:48 AM8/7/06
to Martin Höfling, pytho...@python.org
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

> --
> http://mail.python.org/mailman/listinfo/python-list

Chris Johnson

unread,
Aug 7, 2006, 9:55:08 AM8/7/06
to

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

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

bearoph...@lycos.com

unread,
Aug 7, 2006, 10:15:22 AM8/7/06
to
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

Ziga Seilnacht

unread,
Aug 7, 2006, 10:15:46 AM8/7/06
to

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

Bruno Desthuilliers

unread,
Aug 7, 2006, 10:23:43 AM8/7/06
to

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.

Ant

unread,
Aug 7, 2006, 10:40:18 AM8/7/06
to

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...

Martin Höfling

unread,
Aug 7, 2006, 10:57:10 AM8/7/06
to
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

John McMonagle

unread,
Aug 7, 2006, 11:55:43 PM8/7/06
to Martin Höfling, pytho...@python.org

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.

Patrick Maupin

unread,
Aug 8, 2006, 12:48:58 AM8/8/06
to

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

roelm...@gmail.com

unread,
Aug 8, 2006, 4:45:34 PM8/8/06
to

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

0 new messages