Re: Organisation of python classes and their methods

Discussion in 'Python' started by Robert Kern, Nov 2, 2012.

  1. Robert Kern

    Robert Kern Guest

    On 11/2/12 8:20 AM, Martin Hewitson wrote:
    >
    > On 2, Nov, 2012, at 09:00 AM, Peter Otten <> wrote:
    >
    >> Martin Hewitson wrote:
    >>
    >>> Dear list,
    >>>
    >>> I'm relatively new to Python and have googled and googled but haven't
    >>> found a reasonable answer to this question, so I thought I'd ask it here.
    >>>
    >>> I'm beginning a large Python project which contains many packages, modules
    >>> and classes. The organisation of those is clear to me.
    >>>
    >>> Now, the classes can contain many methods (100s of data analysis methods)
    >>> which operate on instances of the class they belong to. These methods can
    >>> be long and complex. So if I put these methods all in the module file
    >>> inside the class, the file will get insanely long. Reading on google, the
    >>> answer is usually "refactor", but that really doesn't make sense here.
    >>> It's just that the methods are many, and each method can be a long piece
    >>> of code. So, is there a way to put these methods in their own files and
    >>> have them 'included' in the class somehow? I read a little about mixins
    >>> but all the solutions looked very hacky. Is there an official python way
    >>> to do this? I don't like having source files with 100's of lines of code
    >>> in, let alone 1000's.

    >>
    >> You googled, found the right answer ("refactor"), didn't like it and are now
    >> looking to cure the symptoms of the original problem?
    >> Seriously, a good editor can deal with a long source file, but a class with
    >> hundreds of methods will bring trouble to any old brain.

    >
    > Well, here we disagree. Suppose I have a class which encapsulates time-series data. Below is a list of the absolute minimum methods one would have to process that data. That's close to 100 already before even having any specialised methods for dealing with the data. Each of these methods will have maybe 20 lines of documentation. That's 2000 lines already. And what if someone wants to extend that class to add their own processing methods? It would a maintenance nightmare for them to edit the actual class file, which they would then have to repeat each time a new version of the 'official' class file is released.


    Do not make them methods of the time-series class. Make functions that take a
    time-series object. Then you can organize the functions in separate modules to
    your heart's content and import them all into a single convenience namespace.

    --
    Robert Kern

    "I have come to believe that the whole world is an enigma, a harmless enigma
    that is made terrible by our own mad attempt to interpret it as though it had
    an underlying truth."
    -- Umberto Eco
    Robert Kern, Nov 2, 2012
    #1
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Paul Rubin
    Replies:
    10
    Views:
    254
    Steven D'Aprano
    Nov 3, 2012
  2. Peter Otten
    Replies:
    0
    Views:
    169
    Peter Otten
    Nov 2, 2012
  3. Peter Otten
    Replies:
    0
    Views:
    223
    Peter Otten
    Nov 2, 2012
  4. Frank Millman
    Replies:
    0
    Views:
    241
    Frank Millman
    Nov 2, 2012
  5. Ulrich Eckhardt
    Replies:
    0
    Views:
    225
    Ulrich Eckhardt
    Nov 2, 2012
Loading...

Share This Page