Re: multi-Singleton-like using __new__

2008-02-09 Thread Arnaud Delobelle
On Feb 9, 1:38 am, Freek Dijkstra <[EMAIL PROTECTED]> wrote: > J Peyret wrote: [...] > I'll use __metaclass__ solution. Here is my (non-threaded) version: Great! There's nothing voodoo about metaclasses. > class RDFObject(object): >     _cache = {}   # class variable is shared among all RDFObject

Re: multi-Singleton-like using __new__

2008-02-09 Thread Hrvoje Niksic
Matt Nordhoff <[EMAIL PROTECTED]> writes: > Steven D'Aprano wrote: >> Except that using has_key() means making an attribute lookup, which takes >> time. > > I was going to say that, but doesn't 'in' require an attribute lookup of > some sort too, of __contains__ or whatever? It doesn't. Frequen

Re: multi-Singleton-like using __new__

2008-02-08 Thread J Peyret
On Feb 8, 5:38 pm, Freek Dijkstra <[EMAIL PROTECTED]> wrote: If you want to subclass, my initial example did not cover that. This will, or at least, I don't have any problems with similar code: ... def __new__(cls,uri,*args,**kwds): ... try: ... return cls.cache[(cls,uri

Re: multi-Singleton-like using __new__

2008-02-08 Thread Steven D'Aprano
On Sat, 09 Feb 2008 01:20:00 +, Matt Nordhoff wrote: > Steven D'Aprano wrote: >> Except that using has_key() means making an attribute lookup, which >> takes time. > > I was going to say that, but doesn't 'in' require an attribute lookup of > some sort too, of __contains__ or whatever? I d

Re: multi-Singleton-like using __new__

2008-02-08 Thread Freek Dijkstra
J Peyret wrote: > >>> class RDFObject(object): > > ... cache ={} > ... isInitialized = False > ... def __new__(cls,uri,*args,**kwds): > ... try: > ... return cls.cache[uri] > ... except KeyError: > ... print "cache miss" > ... res = c

Re: multi-Singleton-like using __new__

2008-02-08 Thread Matt Nordhoff
Steven D'Aprano wrote: > Except that using has_key() means making an attribute lookup, which takes > time. I was going to say that, but doesn't 'in' require an attribute lookup of some sort too, of __contains__ or whatever? has_key is probably now just a wrapper around that, so it would be one mo

Re: multi-Singleton-like using __new__

2008-02-08 Thread Gabriel Genellina
En Fri, 08 Feb 2008 22:04:26 -0200, Matt Nordhoff <[EMAIL PROTECTED]> escribió: > J Peyret wrote: >> - Same with using try/except KeyError instead of in cls.cache. >> Has_key might be better if you insist on look-before-you-leap, because >> 'in cls.cache' probably expends to uri in cls.cache.key

Re: multi-Singleton-like using __new__

2008-02-08 Thread Steven D'Aprano
On Sat, 09 Feb 2008 00:04:26 +, Matt Nordhoff wrote: > At worst, in and has_key are "about the same". Except that using has_key() means making an attribute lookup, which takes time. I'm kinda curious why you say they're "about the same" when your own timing results contradict that. Here th

Re: multi-Singleton-like using __new__

2008-02-08 Thread Matt Nordhoff
J Peyret wrote: > - Same with using try/except KeyError instead of in cls.cache. > Has_key might be better if you insist on look-before-you-leap, because > 'in cls.cache' probably expends to uri in cls.cache.keys(), which can > be rather bad for perfs if the cache is very big. i.e. dict lookups >

Re: multi-Singleton-like using __new__

2008-02-08 Thread J Peyret
I think the metaclass stuff is a bit too black magic for a pretty simple requirement. Txs in any case for showing me the __init__ issue, I wasn't aware of it. Here's a workaround - not exactly elegant in terms of OO, with the isInitialized flag, but it works. >>> class RDFObject(object): ...

Re: multi-Singleton-like using __new__

2008-02-08 Thread Arnaud Delobelle
On Feb 8, 4:44 pm, Freek Dijkstra <[EMAIL PROTECTED]> wrote: > Is there a best practice on how to override __new__? > > I have a base class, RDFObject, which is instantiated using a unique > identifier (a URI in this case). If an object with a given identifier > already exists, I want to return the

Re: multi-Singleton-like using __new__

2008-02-08 Thread Guilherme Polo
2008/2/8, Freek Dijkstra <[EMAIL PROTECTED]>: > Is there a best practice on how to override __new__? > > I have a base class, RDFObject, which is instantiated using a unique > identifier (a URI in this case). If an object with a given identifier > already exists, I want to return the existing ob