SIngleton from __defaults__

88888 Dihedral dihedral88888 at gmail.com
Wed Jan 22 12:23:02 EST 2014


On Thursday, January 23, 2014 12:37:36 AM UTC+8, Asaf Las wrote:
> On Wednesday, January 22, 2014 6:18:57 PM UTC+2, Chris Angelico wrote:
> 
> > On Thu, Jan 23, 2014 at 3:07 AM, Asaf Las <r.... at gmail.com> wrote:
> 
> > 
> 
> > Why not simply:
> 
> > def get_singleton(x = SomeClass()):
> 
> >     return x
> 
> > Or even:
> 
> > singleton = SomeClass()
> 
> > ? Neither of the above provides anything above the last one, except
> 
> > for late creation.
> 
> > 
> 
> > ChrisA
> 
> 
> 
> Actually need was to have some interface to running independent threads
> 
> to give same and once created object always.
> 
> 
> 
> For first  - SomeClass's object will be created whenever there will be 
> 
> call to get_singleton().
> 
> For second, again it is free to create it whenever someone (thread) 
> 
> wish. 
> 
> 
> 
> Hmmm, use case was to create persistent counter in multithreaded app
> 
> accessing single file where incrementing integer is stored.
> 
> When my imagination expanded it onto multiprocessing mess i ended up 
> 
> using sqlite access to DB in exclusive transaction mode. 
> 
> But this was not pythonic :-) 
> 
> 
> 
> Asaf

In a high level  language such as Python, functions and class initilizers
are the first class objects.

Don't you get the proof?
Everyting OOP is equivalent to everything functional but  it is much trivial to debug  by the designer  
than the old grandma lisp.



More information about the Python-list mailing list