Re: Organisation of python classes and their methods

Discussion in 'Python' started by Frank Millman, Nov 2, 2012.

  1. On 02/11/2012 08:16, 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.
    >
    > Many thanks,
    >
    > Martin
    >


    I have read the other responses, so I may get some flak for encouraging
    bad habits. Nevertheless, I did have a similar requirement, and I found
    a solution that worked for me.

    My situation was not as extreme as yours. I had a class with a number of
    methods. Some of them were of an 'operational' nature - they represented
    the main functionality of the class, and could be called often. Some of
    them were of a 'setup' nature - they were optionally called when the
    object was instantiated, but would only be called once.

    I found that when I wanted to focus on one set of methods, the other set
    'got in my way', and vice-versa. My solution was to take the 'setup'
    methods and put them in another file. This is how I did it.

    BEFORE
    ======

    main.py -

    class MyClass:
    def setup1(self, ...):
    [...]
    def setup2(self, ...):
    [...]
    def func1(self, ...):
    [...]
    def func2(self, ...):
    [...]


    AFTER
    =====

    setup.py -

    def setup1(self, ...):
    [...]
    def setup2(self, ...):
    [...]

    main.py -

    import setup
    class MyClass:
    setup1 = setup.setup1
    setup2 = setup.setup2
    def func1(self, ...):
    [...]
    def func2(self, ...):
    [...]


    Hope this gives you some ideas.

    Frank Millman
     
    Frank Millman, 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:
    280
    Steven D'Aprano
    Nov 3, 2012
  2. Peter Otten
    Replies:
    0
    Views:
    186
    Peter Otten
    Nov 2, 2012
  3. Robert Kern
    Replies:
    0
    Views:
    222
    Robert Kern
    Nov 2, 2012
  4. Peter Otten
    Replies:
    0
    Views:
    240
    Peter Otten
    Nov 2, 2012
  5. Ulrich Eckhardt
    Replies:
    0
    Views:
    238
    Ulrich Eckhardt
    Nov 2, 2012
Loading...

Share This Page