Organisation of python classes and their methods
martinhewitson at mac.com
Fri Nov 2 09:08:07 CET 2012
On 2, Nov, 2012, at 08:38 AM, Paul Rubin <no.email at nospam.invalid> wrote:
> Martin Hewitson <martinhewitson at mac.com> writes:
>> So, is there a way to put these methods in their own files and have
>> them 'included' in the class somehow? ... 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.
> That code sounds kind of smelly... why are there so many methods per
Simply because there are many different ways to process the data. The class encapsulates the data, and the user can process the data in many ways. Of course, one could have classes which encapsulate the algorithms, as well as the data, but it also seems natural to me to have algorithms as methods which are part of the data class, so the user operates on the data using methods of that class.
> Python lets you inject new methods into existing classes (this is
> sometimes called duck punching) but I don't recommend doing this.
Is there not a way just to declare the method in the class and put the actual implementation in another file on the python path so that it's picked up a run time?
> A few hundred lines of code in a file is completely reasonable. Even a
> few thousand is ok. IME it starts getting unwieldy at 3000 or so.
In all other projects (in other languages) I've always tried to keep individual files short to ease maintainability and search-ability (if there is such a word).
Even if one takes reasonable numbers: 20 methods, each method has 20 lines of documentation, then we immediately have 400 lines in the file before writing a line of code. It would seem much more natural to me to have these methods in their own file, grouped nicely in sub-directories. But it seems this is not the python way. Sigh.
Thanks for your thoughts,
More information about the Python-list