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