Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #108619

Re: Design: Idiom for classes and methods that are customizable by the user?

From Michael Selik <michael.selik@gmail.com>
Newsgroups comp.lang.python
Subject Re: Design: Idiom for classes and methods that are customizable by the user?
Date 2016-05-13 20:33 +0000
Message-ID <mailman.647.1463171609.32212.python-list@python.org> (permalink)
References <5734ECDF.7070006@gmx.de> <mailman.617.1463087060.32212.python-list@python.org> <dplassFelugU1@mid.individual.net> <CAGgTfkPeqybAeqXLq8FHyRSb+d8KLemvxLrk8i3PfbWdwV4rBA@mail.gmail.com>

Show all headers | View raw


On Fri, May 13, 2016 at 2:41 AM Gregory Ewing <greg.ewing@canterbury.ac.nz>
wrote:

> Dirk Bächle wrote:
> > I'm currently following the "Factory" pattern (more or less) as I know
> > it from C++ and similar languages.
>
> This statement sets off alarm bells for me. If you're using some
> design pattern in Python just because you learned to do it that
> way in C++/Java/whatever, you're probably making it more
> complicated than it needs to be.
>

I share Greg's trepidation when I hear a phrase like that, but the general
idea of a registry of classes or functions and then picking the right one
based on string input is fine.

How would the API work for custom Taskmasters? It's not so great to require
that the user must explicitly ``add`` their derived class after defining
it. Perhaps that add function could be a decorator?

def register(key):
    def decorator(cls):
        if not issubclass(cls, Taskmaster):
            raise TypeError('You fool!')
        registry[key] = cls
    return decorator

@register('noclean')
class NoCleanTaskMaster(TaskMaster):
    "Looks clean enough, why worry?"

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

Design: Idiom for classes and methods that are customizable by the user? Dirk Bächle <tshortik@gmx.de> - 2016-05-12 22:51 +0200
  Re: Design: Idiom for classes and methods that are customizable by the user? Marko Rauhamaa <marko@pacujo.net> - 2016-05-13 01:01 +0300
    Re: Design: Idiom for classes and methods that are customizable by the user? Dirk Bächle <tshortik@gmx.de> - 2016-05-13 00:42 +0200
      Re: Design: Idiom for classes and methods that are customizable by the user? Marko Rauhamaa <marko@pacujo.net> - 2016-05-13 08:53 +0300
  Re: Design: Idiom for classes and methods that are customizable by the   user? Gregory Ewing <greg.ewing@canterbury.ac.nz> - 2016-05-13 18:35 +1200
    Re: Design: Idiom for classes and methods that are customizable by the user? Michael Selik <michael.selik@gmail.com> - 2016-05-13 20:33 +0000
    Re: Design: Idiom for classes and methods that are customizable by the user? Dirk Bächle <tshortik@gmx.de> - 2016-05-17 16:51 +0200
    Re: Design: Idiom for classes and methods that are customizable by the user? Michael Selik <michael.selik@gmail.com> - 2016-05-17 18:13 +0000

csiph-web