Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #109500
| Path | csiph.com!news.mixmin.net!news.unit0.net!fu-berlin.de!uni-berlin.de!not-for-mail |
|---|---|
| From | Gary Herron <gherron@digipen.edu> |
| 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 | <mailman.4.1465111092.2306.python-list@python.org> (permalink) |
| References | <f8f1cc30-3274-4274-9e28-815b32b4e27c@googlegroups.com> <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 | <gherron@digipen.edu> |
| 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 | <f8f1cc30-3274-4274-9e28-815b32b4e27c@googlegroups.com> |
| 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 <python-list.python.org> |
| List-Unsubscribe | <https://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe> |
| List-Archive | <http://mail.python.org/pipermail/python-list/> |
| List-Post | <mailto:python-list@python.org> |
| List-Help | <mailto:python-list-request@python.org?subject=help> |
| List-Subscribe | <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe> |
| X-Mailman-Original-Message-ID | <5753D230.7060902@digipen.edu> |
| X-Mailman-Original-References | <f8f1cc30-3274-4274-9e28-815b32b4e27c@googlegroups.com> |
| Xref | csiph.com comp.lang.python:109500 |
Show key headers only | View raw
On 06/04/2016 11:55 PM, Mark Summerfield wrote:
> Sometimes I want to spread a class over multiple files.
There’s and easy way to do this in Python using what's called a Mixin
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
defined in MainCLass.
Gary Herron
--
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 entire SQL database. I want a model instance to provide a single point of access 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 programming and have lots of methods to reflect all the actions (e.g., menu options
> 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 use and understand and which doesn't use any tricky or hard to maintain code.
>
> 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-effect
> 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__ = [] # self is a Model
> register_method = 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 similar?
Back to comp.lang.python | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
Spreading a class over multiple files Mark Summerfield <list@qtrac.plus.com> - 2016-06-04 23:55 -0700
Re: Spreading a class over multiple files Gary Herron <gherron@digipen.edu> - 2016-06-05 00:18 -0700
Re: Spreading a class over multiple files Mark Summerfield <list@qtrac.plus.com> - 2016-06-05 01:14 -0700
Re: Spreading a class over multiple files Steven D'Aprano <steve@pearwood.info> - 2016-06-05 20:57 +1000
Re: Spreading a class over multiple files Peter Otten <__peter__@web.de> - 2016-06-05 09:40 +0200
Re: Spreading a class over multiple files Terry Reedy <tjreedy@udel.edu> - 2016-06-05 13:59 -0400
csiph-web