singleton ... again

Chris Angelico rosuav at gmail.com
Thu Feb 13 06:57:22 EST 2014


On Thu, Feb 13, 2014 at 10:50 PM, Ned Batchelder <ned at 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. 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. 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.

ChrisA



More information about the Python-list mailing list