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


Groups > comp.lang.python > #86762

Re: suggestions for functional style (singleton pattern?)

Path csiph.com!usenet.pasdenom.info!news.redatomik.org!newsfeed.xs4all.nl!newsfeed2a.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail
Return-Path <ian.g.kelly@gmail.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.010
X-Spam-Evidence '*H*': 0.98; '*S*': 0.00; 'api.': 0.05; 'class,': 0.07; 'def': 0.12; 'changes': 0.15; '"from': 0.16; '@property': 0.16; 'binding,': 0.16; 'singleton': 0.16; 'subject:?)': 0.16; 'wrote:': 0.18; 'code.': 0.18; 'module': 0.19; 'implementing': 0.19; '>>>': 0.22; 'import': 0.22; 'mon,': 0.24; 'header:In-Reply- To:1': 0.27; 'michael': 0.29; 'skip:p 30': 0.29; 'am,': 0.29; 'properties': 0.29; 'message-id:@mail.gmail.com': 0.30; "i'm": 0.30; 'reflected': 0.31; 'class': 0.32; 'another': 0.32; 'subject: (': 0.35; 'but': 0.35; 'received:google.com': 0.35; 'really': 0.36; 'needed': 0.38; 'to:addr:python-list': 0.38; 'does': 0.39; 'itself': 0.39; 'sure': 0.39; 'to:addr:python.org': 0.39; 'skip:p 20': 0.39; 'real': 0.63; 'mar': 0.68; '2015': 0.84
DKIM-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:from:date:message-id:subject:to :content-type; bh=hqP/lTYqgrTJ/HToKLFOXpIcxaYYr6cv698EZMtTuLc=; b=rY07MO8b5Z1yfqNj4wu35WKpYUDxuFHgn27yr7UVPuinKEWFRpX1Xp/7HfJ80g4Wdg Dgy2GhiwzUtga2FMulNKwO/bk68Bvb3rWTqA3/4CGffPyWMBdhgnsf9AU4PG976w8U9p T2e7UpqFWMgMoovwoEgZG0VAVI+NEaMDsIHpLOfv6OxUWAijFWImsGvVGN4ti3m7qU2a kB8m6K4fjvMCUX52e10bC5xDuLF+0u3JCrxnpfWZsY23+lU4MkYQa1ZlSumeuf/bMSUe M5D4zuK08Zr6NBSUoMVMaKlcYMIWd4qCUTagghv402c53S101U6BsyMpK86xI2Zzb49b ZDiw==
X-Received by 10.70.5.3 with SMTP id o3mr22539766pdo.17.1425315145353; Mon, 02 Mar 2015 08:52:25 -0800 (PST)
MIME-Version 1.0
In-Reply-To <54F488D0.2010906@gmail.com>
References <WXrIw.14066$uP4.9692@fx20.iad> <13v4falb4odnhtvss4qdatnn16sgiv5pgd@4ax.com> <jAwIw.631640$rq4.16649@fx27.iad> <mailman.2.1425186322.29956.python-list@python.org> <md1dep$s0t$1@speranza.aioe.org> <54F488D0.2010906@gmail.com>
From Ian Kelly <ian.g.kelly@gmail.com>
Date Mon, 2 Mar 2015 09:51:45 -0700
Subject Re: suggestions for functional style (singleton pattern?)
To Python <python-list@python.org>
Content-Type text/plain; charset=UTF-8
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.19
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>
Newsgroups comp.lang.python
Message-ID <mailman.55.1425315154.13471.python-list@python.org> (permalink)
Lines 25
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1425315154 news.xs4all.nl 2935 [2001:888:2000:d::a6]:49218
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:86762

Show key headers only | View raw


On Mon, Mar 2, 2015 at 8:59 AM, Michael Torrie <torriem@gmail.com> wrote:
> Now I don't know of any way of implementing class-style properties on a
> module as you can do in a class, but if that were needed you would write
> a standard class and instantiate a singleton inside the module itself
> perhaps.

This works, although I'm not sure that I would do it in real code.

>>> class PropertyModule(types.ModuleType):
...     @property
...     def parrot(self):
...         return "Norwegian Blue"
...
>>> sys.modules['parrot_sketch'] = PropertyModule('parrot_sketch')
>>> import parrot_sketch
>>> parrot_sketch.parrot
'Norwegian Blue'

Note that if the user does "from parrot_sketch import parrot", then
the property is evaluated at import time and any changes won't be
reflected in the local binding, which might cause some surprises.

Also, this isn't really a singleton any longer since the user might
just create another instance of the class, but you can hide the class
and present just the module as the API.

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


Thread

suggestions for functional style (singleton pattern?) yves@zioup.com - 2015-02-28 16:12 -0700
  Re: suggestions for functional style (singleton pattern?) Michael Torrie <torriem@gmail.com> - 2015-02-28 19:19 -0700
    Re: suggestions for functional style (singleton pattern?) yves@zioup.com - 2015-02-28 21:11 -0700
      Re: suggestions for functional style (singleton pattern?) Michael Torrie <torriem@gmail.com> - 2015-02-28 22:14 -0700
  Re: suggestions for functional style (singleton pattern?) Mario Figueiredo <marfig@gmail.com> - 2015-03-01 04:45 +0100
    Re: suggestions for functional style (singleton pattern?) yves@zioup.com - 2015-02-28 21:29 -0700
      Re: suggestions for functional style (singleton pattern?) Michael Torrie <torriem@gmail.com> - 2015-02-28 22:05 -0700
        Re: suggestions for functional style (singleton pattern?) Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2015-03-02 02:55 +1100
        Re: suggestions for functional style (singleton pattern?) Fabien <fabien.maussion@gmail.com> - 2015-03-02 11:19 +0100
          Re: suggestions for functional style (singleton pattern?) Mario Figueiredo <marfig@gmail.com> - 2015-03-02 11:31 +0100
          Re: suggestions for functional style (singleton pattern?) Michael Torrie <torriem@gmail.com> - 2015-03-02 08:59 -0700
          Re: suggestions for functional style (singleton pattern?) Ian Kelly <ian.g.kelly@gmail.com> - 2015-03-02 09:51 -0700
      Re: suggestions for functional style (singleton pattern?) Gregory Ewing <greg.ewing@canterbury.ac.nz> - 2015-03-01 19:00 +1300
    Re: suggestions for functional style (singleton pattern?) Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2015-03-02 02:04 +1100
      Re: suggestions for functional style (singleton pattern?) Mario Figueiredo <marfig@gmail.com> - 2015-03-01 19:20 +0100
  Re: suggestions for functional style (singleton pattern?) Paul Rubin <no.email@nospam.invalid> - 2015-02-28 22:23 -0800

csiph-web