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


Groups > comp.lang.python > #66185

Re: singleton ... again

From Roy Smith <roy@panix.com>
Newsgroups comp.lang.python
Subject Re: singleton ... again
Date 2014-02-13 10:24 -0500
Organization PANIX Public Access Internet and UNIX, NYC
Message-ID <roy-141A69.10240713022014@news.panix.com> (permalink)
References (3 earlier) <bm0v9iFpuquU1@mid.individual.net> <mailman.6750.1392199807.18130.python-list@python.org> <m2a9dvmlos.fsf@cochabamba.vanoostrum.org> <ldibhu$2g5$1@ger.gmane.org> <mailman.6834.1392292646.18130.python-list@python.org>

Show all headers | View raw


In article <mailman.6834.1392292646.18130.python-list@python.org>,
 Chris Angelico <rosuav@gmail.com> wrote:

> On Thu, Feb 13, 2014 at 10:50 PM, Ned Batchelder <ned@nedbatchelder.com> 
> wrote:
> > I still don't see it.  To convince me that a singleton class makes sense,
> > you'd have to explain why by virtue of the class's very nature, it never
> > makes sense for there ever to be more than one of them.
> 
> There's a huge difference, btw, between mutable and immutable
> singletons. With immutables like None, True/False, integers, strings,
> and tuples thereof, returning a preexisting object is just an
> optimization. Do it if you want, don't if you don't, nobody's going to
> hugely care.

People *depend* on None being a singleton (and are encouraged to do so), 
when they use "is" as the test-for-Noneness.

> With mutables, it's hugely different. A singleton
> "database connection" object would, imo, be hugely confusing; you'd
> think you created a separate connection object, but no, what you do on
> this one affects the other.

Except that if you do caching in the database connector, you would 
certainly want the cache to be shared between all the users.  There's no 
right answer here.  Each way has it's advantages and disadvantages.  And 
it would only be confusing if the documentation didn't spell out which 
way it was doing it, leaving people to make (possibly wrong) assumptions.

> Better there to have module-level functions; at least Python 
> programmers should understand that reimporting a module gives you 
> back another reference to the same module.

Except when it doesn't.  Singleton-ness of modules depends on the names 
under which they were imported.  Symlinks, for example, can fool the 
import machinery.

$ ls -l s1 s2
lrwxrwxrwx 1 roy roy 1 Feb 13 10:13 s1 -> s
lrwxrwxrwx 1 roy roy 1 Feb 13 10:13 s2 -> s

$ ls -l s
total 12
-rw-rw-r-- 1 roy roy   0 Feb 13 10:13 __init__.py
-rw-rw-r-- 1 roy roy 101 Feb 13 10:14 __init__.pyc
-rw-rw-r-- 1 roy roy   9 Feb 13 10:12 singleton.py
-rw-rw-r-- 1 roy roy 123 Feb 13 10:14 singleton.pyc

>>> import s1.singleton
>>> import s2.singleton
>>> s1.singleton == s2.singleton
False

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


Thread

singleton ... again Asaf Las <roegltd@gmail.com> - 2014-02-11 20:34 -0800
  Re: singleton ... again Asaf Las <roegltd@gmail.com> - 2014-02-11 21:15 -0800
  Re:singleton ... again Dave Angel <davea@davea.name> - 2014-02-12 00:48 -0500
    Re: singleton ... again Roy Smith <roy@panix.com> - 2014-02-12 00:55 -0500
      Re: singleton ... again Gregory Ewing <greg.ewing@canterbury.ac.nz> - 2014-02-12 23:04 +1300
        Re: singleton ... again Ben Finney <ben+python@benfinney.id.au> - 2014-02-12 21:09 +1100
          Re: singleton ... again Roy Smith <roy@panix.com> - 2014-02-12 08:40 -0500
          Re: singleton ... again Piet van Oostrum <piet@vanoostrum.org> - 2014-02-13 10:00 +0100
            Re: singleton ... again Ned Batchelder <ned@nedbatchelder.com> - 2014-02-13 06:50 -0500
            Re: singleton ... again Chris Angelico <rosuav@gmail.com> - 2014-02-13 22:57 +1100
              Re: singleton ... again Roy Smith <roy@panix.com> - 2014-02-13 10:24 -0500
                Re: singleton ... again Chris Angelico <rosuav@gmail.com> - 2014-02-14 07:03 +1100
                Re: singleton ... again Robert Kern <robert.kern@gmail.com> - 2014-02-13 21:13 +0000
                Re: singleton ... again Chris Angelico <rosuav@gmail.com> - 2014-02-14 08:27 +1100
            Re: singleton ... again Ethan Furman <ethan@stoneleaf.us> - 2014-02-13 08:57 -0800
              Re: singleton ... again Roy Smith <roy@panix.com> - 2014-02-13 12:57 -0500
                Re: singleton ... again Ethan Furman <ethan@stoneleaf.us> - 2014-02-13 10:31 -0800
                Re: singleton ... again Roy Smith <roy@panix.com> - 2014-02-13 14:03 -0500
                Re: singleton ... again Chris Angelico <rosuav@gmail.com> - 2014-02-14 06:50 +1100
                Re: singleton ... again Grant Edwards <invalid@invalid.invalid> - 2014-02-13 22:28 +0000
              Re: singleton ... again Gregory Ewing <greg.ewing@canterbury.ac.nz> - 2014-02-14 18:21 +1300
            Re: singleton ... again Tim Delaney <timothy.c.delaney@gmail.com> - 2014-02-14 06:16 +1100
        Re: singleton ... again Steven D'Aprano <steve@pearwood.info> - 2014-02-13 02:58 +0000
          Re: singleton ... again Ben Finney <ben+python@benfinney.id.au> - 2014-02-13 14:07 +1100
            Re: singleton ... again Steven D'Aprano <steve@pearwood.info> - 2014-02-13 04:24 +0000
              Re: singleton ... again Chris Angelico <rosuav@gmail.com> - 2014-02-13 15:33 +1100
              Re: singleton ... again Gregory Ewing <greg.ewing@canterbury.ac.nz> - 2014-02-14 18:15 +1300
          Re: singleton ... again Gregory Ewing <greg.ewing@canterbury.ac.nz> - 2014-02-14 18:06 +1300
    Re: singleton ... again Asaf Las <roegltd@gmail.com> - 2014-02-12 09:50 -0800
      Re: singleton ... again Asaf Las <roegltd@gmail.com> - 2014-02-12 09:56 -0800
        Re: singleton ... again Asaf Las <roegltd@gmail.com> - 2014-02-12 09:58 -0800
        Re: singleton ... again Gregory Ewing <greg.ewing@canterbury.ac.nz> - 2014-02-13 10:57 +1300
          Re: singleton ... again Asaf Las <roegltd@gmail.com> - 2014-02-12 17:38 -0800
      Re: singleton ... again Mark Lawrence <breamoreboy@yahoo.co.uk> - 2014-02-12 18:57 +0000
        Re: singleton ... again Asaf Las <roegltd@gmail.com> - 2014-02-12 17:02 -0800
      Re: singleton ... again Ned Batchelder <ned@nedbatchelder.com> - 2014-02-12 16:34 -0500
        Re: singleton ... again Asaf Las <roegltd@gmail.com> - 2014-02-12 17:34 -0800
      Re: singleton ... again Tim Delaney <timothy.c.delaney@gmail.com> - 2014-02-13 10:08 +1100
      Re: singleton ... again Roy Smith <roy@panix.com> - 2014-02-12 20:57 -0500
  Re: singleton ... again Michael Torrie <torriem@gmail.com> - 2014-02-12 15:05 -0700

csiph-web