Re: Using __new__

2024-02-18 Thread Mats Wichmann via Python-list
On 2/17/24 19:24, dn via Python-list wrote: On 18/02/24 13:21, Jonathan Gossage wrote: - perhaps someone knows a better/proper way to do this? Suggested research: custom classes, ABCs, and meta-classes... Cure the old "what do you want to accomplish" question. If it's to channel access to

Re: Using __new__

2024-02-17 Thread dn via Python-list
On 18/02/24 13:21, Jonathan Gossage wrote: The problem is that if you are dealing with a library class, you may have times when the superclass is 'object' while at other times, with a different inheritance hierarchy, the superclass may need arguments. My thought is that the object class __new__

Re: Using __new__

2024-02-17 Thread dn via Python-list
On 18/02/24 12:48, Jonathan Gossage wrote: The problem that I am facing is that when the superclass is not 'object', the __init__ method may well need arguments. I do not know how to determine if the superclass is 'object'. For what it is worth, any attempt to use this with different arguments 

Re: Using __new__

2024-02-17 Thread dn via Python-list
On 18/02/24 11:35, Jonathan Gossage via Python-list wrote: I am attempting to use the __new__ method in the following code: class SingletonExample(object): _instance = None def __new__(cls, **kwargs): if cls._instance is None: cls._instance = super().__new__(cls,

Re: Using __new__

2024-02-17 Thread MRAB via Python-list
On 2024-02-17 22:35, Jonathan Gossage via Python-list wrote: I am attempting to use the __new__ method in the following code: class SingletonExample(object): _instance = None def __new__(cls, **kwargs): if cls._instance is None: cls._instance = super().__new__(cl

Using __new__

2024-02-17 Thread Jonathan Gossage via Python-list
I am attempting to use the __new__ method in the following code: class SingletonExample(object): _instance = None def __new__(cls, **kwargs): if cls._instance is None: cls._instance = super().__new__(cls, **kwargs) return cls._instance def __init__(self, *

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

multi-Singleton-like using __new__

2008-02-08 Thread Freek Dijkstra
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 object, otherwise, I want to create a new object and add thi