Path: csiph.com!news.mixmin.net!news.unit0.net!fu-berlin.de!uni-berlin.de!not-for-mail From: Gary Herron Newsgroups: comp.lang.python Subject: Re: Spreading a class over multiple files Date: Sun, 5 Jun 2016 00:18:08 -0700 Lines: 112 Message-ID: References: <5753D230.7060902@digipen.edu> Mime-Version: 1.0 Content-Type: text/plain; charset=windows-1252; format=flowed Content-Transfer-Encoding: quoted-printable X-Trace: news.uni-berlin.de T4t0in4V4hmMA5pS0qWNXg+K06domsTVThjrfi8L3gXA== Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.045 X-Spam-Evidence: '*H*': 0.91; '*S*': 0.00; 'essentially': 0.04; 'imports': 0.09; 'subject:files': 0.09; 'python': 0.10; 'files.': 0.13; 'stack': 0.13; 'def': 0.13; 'file,': 0.15; 'cleanly': 0.16; 'hacks': 0.16; 'received:io': 0.16; 'received:psf.io': 0.16; 'subject:class': 0.16; 'toolbar': 0.16; 'wrote:': 0.16; 'differ': 0.18; 'skip:l 30': 0.18; 'gui': 0.18; 'solution.': 0.18; '(see': 0.20; 'decorator': 0.22; 'explicit': 0.22; 'file:': 0.22; 'programming': 0.22; 'code.': 0.23; 'defined': 0.23; 'tables': 0.23; 'import': 0.24; 'header:In-Reply-To:1': 0.24; 'discussion': 0.24; 'module': 0.25; "i've": 0.25; 'header:User-Agent:1': 0.26; "doesn't": 0.26; 'separate': 0.27; 'question': 0.27; '(e.g.,': 0.27; 'reflect': 0.27; 'structure,': 0.29; 'unchanged': 0.29; 'url:wikipedia': 0.29; 'way?': 0.29; 'summary': 0.29; 'classes': 0.30; 'url:wiki': 0.30; 'primary': 0.31; 'skip:_ 10': 0.32; 'point': 0.33; 'options': 0.33; 'class': 0.33; 'interaction': 0.33; 'structure': 0.34; 'url:eu': 0.34; 'this?': 0.34; 'so,': 0.35; 'could': 0.35; 'instance': 0.35; 'requiring': 0.35; 'sometimes': 0.35; 'but': 0.36; 'there': 0.36; 'url:org': 0.36; 'actions': 0.36; 'to:addr:python-list': 0.36; 'pm,': 0.36; 'subject:: ': 0.37; 'two': 0.37; 'method': 0.37; 'say': 0.37; 'spread': 0.37; 'self': 0.38; 'files': 0.38; 'url:en': 0.39; 'received:192': 0.39; 'to:addr:python.org': 0.40; 'mark': 0.40; 'called': 0.40; 'some': 0.40; 'easy': 0.60; 'avoid': 0.61; 'entire': 0.61; 'provide': 0.61; 'charset:windows-1252': 0.62; 'needs,': 0.63; 'more': 0.63; 'strictly': 0.64; 'website:': 0.65; 'dr.': 0.69; 'overall': 0.72; 'received:204': 0.75; 'institute': 0.77; '(425)': 0.84; '895-4418': 0.84; 'actions,': 0.84; 'digipen': 0.84; 'extras': 0.84; 'herron': 0.84; 'subject:over': 0.84; '\xa0\xa0\xa0\xa0\xa0': 0.84; 'approach.': 0.91; 'tricky': 0.93 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:38.0) Gecko/20100101 Thunderbird/38.8.0 In-Reply-To: X-Content-Filtered-By: Mailman/MimeDel 2.1.22 X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.22 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Mailman-Original-Message-ID: <5753D230.7060902@digipen.edu> X-Mailman-Original-References: Xref: csiph.com comp.lang.python:109500 On 06/04/2016 11:55 PM, Mark Summerfield wrote: > Sometimes I want to spread a class over multiple files. There=92s and easy way to do this in Python using what's called a Mixin=20 class and (multiple) inheritance: (See https://en.wikipedia.org/wiki/Mixin for more information.) In one file, say extras.py class ExtraMethodsMixin: def extra_1(...): ... def extra_2(...): ... In the main class file: from extras import ExtraMethodsMixin class MainClass(ExtraMethodsMixin): def __init__(...): ... # and so on The result will be essentially the same as if all three methods were=20 defined in MainCLass. Gary Herron --=20 Dr. Gary Herron Professor of Computer Science DigiPen Institute of Technology (425) 895-4418 > > My primary use case is when I create a "Model" class to reflect an enti= re SQL database. I want a model instance to provide a single point of acc= ess to > the database, but the database has many tables each requiring its own= methods since they differ in their structure, purpose, validation needs,= etc. > > A secondary use case is when I create "MainWindow" classes in GUI progr= amming and have lots of methods to reflect all the actions (e.g., menu op= tions > and toolbar actions, plus interaction with the main widget(s)). > > To meet these needs I've devised an approach that I think is easy to us= e and understand and which doesn't use any tricky or hard to maintain cod= e. > > My question is -- are there nicer/better ways to achieve this? > > Here's a summary of my approach. A fuller discussion is on my website: > https://www.qtrac.eu/pyclassmulti.html > > # Lib.py > # This provides the two functions (both decorators) used to support my = approach > def add_methods_from(*modules): > def decorator(Class): > for module in modules: > for method in getattr(module, "__methods__"): > setattr(Class, method.__name__, method) > return Class > return decorator > > def register_method(methods): # A decorator used purely for its side-ef= fect > def register_method(method): > methods.append(method) > return method # Unchanged and not strictly necessary > return register_method > > # Model.py > # This provides my model but some methods are in separate files > import Lib > import _ModelConfig > import _ModelOutput > > @Lib.add_methods_from(_ModelConfig, _ModelOutput) > class Model: > ... > def small_method(self): > ... > > # _ModelConfig.py # _ModelOutput has the same structure so not shown > import Lib > > __methods__ =3D [] # self is a Model > register_method =3D Lib.register_method(__methods__) > > @register_method > def config(self): > ... > So, that's the overall pattern of my solution. > > Is there a nicer/better way? Could I cleanly avoid the explicit imports= (e.g., import _ModelConfig), without resorting to stack frame hacks or s= imilar?