Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #86762
| References | (1 earlier) <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 | 2015-03-02 09:51 -0700 |
| Subject | Re: suggestions for functional style (singleton pattern?) |
| Newsgroups | comp.lang.python |
| Message-ID | <mailman.55.1425315154.13471.python-list@python.org> (permalink) |
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 | Next — Previous in thread | Next in thread | Find similar | Unroll 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