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


Groups > comp.lang.python > #86645

Re: suggestions for functional style (singleton pattern?)

Date 2015-02-28 22:05 -0700
From Michael Torrie <torriem@gmail.com>
Subject Re: suggestions for functional style (singleton pattern?)
References <WXrIw.14066$uP4.9692@fx20.iad> <13v4falb4odnhtvss4qdatnn16sgiv5pgd@4ax.com> <jAwIw.631640$rq4.16649@fx27.iad>
Newsgroups comp.lang.python
Message-ID <mailman.2.1425186322.29956.python-list@python.org> (permalink)

Show all headers | View raw


On 02/28/2015 09:29 PM, yves@zioup.com wrote:
>> Problem 4:
>> You speak of a singleton. But you haven't implemented one. It is not
>> clear from your code if this class should be a singleton. I'm guessing
>> not. Singletons are in fact rare. Well, let me put it another way:
>> Good reasons to code a singleton are rare.
> 
> Thanks. I hadn't realise "singleton" meant a class built such that it could
> not be instanciated more than once, I thought it corresponded to a pattern
> where only one object is ever created from a given class.

Is not "only one object is ever created from a given class" the same as
"could not be [instantiated] more than once?"

To be clear, singletons are commonly used in Python every time you
import a module.  A module *is* a singleton pattern, particularly one
that maintains state.  I use sometimes use this feature for sharing
config and other data between other modules (global state when it's
required).

"import module" essentially does the one-time instantiation if it's a
new import; top-level code in the module is run once on first import,
where it acts like a constructor.  So it functions as a singleton.  As
near as I can tell there's very little reason to make a class-based
singleton in Python, since a module has the same effect and is cleaner.

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