Distributing methods of a class across multiple files

Cameron Simpson cs at zip.com.au
Wed Jan 25 01:15:53 EST 2012

On 24Jan2012 19:54, 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.

You may need to define "ridiculously long". What's your objecttion to
a long file? What specific difficulties does it cause? I'm not arguing
here that all programs should be in a single monolithic file, just that
breaking things up just on size is a rather arbitrary basis.

If methods supply a particular type of functionality you can write a
mixin class, like this:

  from fooey import FooeyClass
  from bloohey import BlooheyCLass

  class Foo(object, FooeyClass, BlooheyClass):
    ... core methods of Foo here ...

and then put methods involving Fooeyness in the class definition of
FooeyClass and so forth. But unless you intend to reuse FooeyClass to
augument other classes or FooeyClass really is a well defined standalone
piece of functionality, this is probably more contortation than it is

Cameron Simpson <cs at zip.com.au> DoD#743

Hello, my name is Yog-Sothoth, and I'll be your eldritch horror today.
        - Heather Keith <hkeith+ at andrew.cmu.edu>

More information about the Python-list mailing list