Distributing methods of a class across multiple files
Frank Millman
frank at chagford.com
Wed Jan 25 03:26:04 EST 2012
"lh" <lhughes42 at gmail.com> wrote:
> Is this possible please? I have done some searching but it is hard to
> narrow down Google searches to this question. What I would like to do
> is, for example:
> 1) define a class Foo in file test.py... give it some methods
> 2) define a file test2.py which contains a set of methods that are
> methods of class Foo defined in test.py. I can import Foo obviously
> but it isn't clear to me how to identify the methods in test2.py to be
> methods of class Foo defined in test.py (normally I would just indent
> them "def"'s under the class but the class isn't textually in
> test2.py).
>
> In short I would like to distribute code for one class across multiple
> files so a given file doesn't get ridiculously long.
>
I take the point of the other responders that it is not a normal thing to
do, but I had a few long but rarely used methods which I wanted to move out
of the main file just to keep the main file tidier. I came up with this
solution, and it seems to work.
In test2.py -
def long_method_1():
pass
def long_method2():
pass
In test.py -
import test2
class Foo:
long_method_1 = test2.long_method_1
long_method_2 = test2.long_method_2
Then in Foo I can refer to self.long_method_1().
HTH
Frank Millman
More information about the Python-list
mailing list