Class.__class__ magic trick help

2012-08-21 Thread Oscar Benjamin
On 21 August 2012 16:19, Oscar Benjamin wrote: > > On Aug 21, 2012 3:42 PM, "Massimo DiPierro" > wrote: > > > > Thanks again Oscar. I cannot do that. I have tight constraints. I am not > at liberty to modify the code that uses the class. The exposed API cannot > change including a.x, dict(a), is

Re: Class.__class__ magic trick help

2012-08-21 Thread Massimo DiPierro
Thanks again Oscar. I cannot do that. I have tight constraints. I am not at liberty to modify the code that uses the class. The exposed API cannot change including a.x, dict(a), is isinstance(a,dict). My goal it so change the definition of this class to make it faster. Where is in the Python so

Re: Class.__class__ magic trick help

2012-08-21 Thread Oscar Benjamin
On 21 August 2012 14:50, Massimo Di Pierro wrote: > Hello Oscar, > > thanks for your help but your proposal of adding: > > def __setitem__(self,key,value): >self.__dict__[key] = value >dict.__setitem__(self, key, value) > > does not help me. > > What I have today is a class that works like

Re: Class.__class__ magic trick help

2012-08-21 Thread Massimo Di Pierro
Hello Oscar, thanks for your help but your proposal of adding: def __setitem__(self,key,value): self.__dict__[key] = value dict.__setitem__(self, key, value) does not help me. What I have today is a class that works like SlowStorage. I want to replace it with NewStorage because it is 10x

Re: Class.__class__ magic trick help

2012-08-21 Thread Oscar Benjamin
On 21 August 2012 13:52, Massimo Di Pierro wrote: > On Aug 21, 2:40 am, Oscar Benjamin wrote: > > On Mon, 20 Aug 2012 21:17:15 -0700 (PDT), Massimo Di Pierro > > > > > > > > > > > > > > > > > > > > wrote: > > > Consider this code: > > > class SlowStorage(dict): > > > def __getattr__(self,key

Re: Class.__class__ magic trick help

2012-08-21 Thread Massimo Di Pierro
On Aug 21, 2:40 am, Oscar Benjamin wrote: > On Mon, 20 Aug 2012 21:17:15 -0700 (PDT), Massimo Di Pierro > > > > > > > > > > wrote: > > Consider this code: > > class SlowStorage(dict): > >     def __getattr__(self,key): > >           return self[key] > >     def __setattr__(self,key): > >        

Re: Class.__class__ magic trick help

2012-08-21 Thread Oscar Benjamin
On Mon, 20 Aug 2012 21:17:15 -0700 (PDT), Massimo Di Pierro wrote: Consider this code: class SlowStorage(dict): def __getattr__(self,key): return self[key] def __setattr__(self,key): self[key]=value class FastStorage(dict): def __init__(self, __d__=None, *

Re: Class.__class__ magic trick help

2012-08-20 Thread Massimo Di Pierro
Consider this code: class SlowStorage(dict): def __getattr__(self,key): return self[key] def __setattr__(self,key): self[key]=value class FastStorage(dict): def __init__(self, __d__=None, **kwargs): self.update(__d__,**kwargs) def __getitem__(self,key):

Re: Class.__class__ magic trick help

2012-08-20 Thread Massimo Di Pierro
The fact is this works: >>> class B(object): ...__class__ = dict >>> b=B() but this does not >>> class B(object): ...def __init__(self): ...self.__class__ = dict >>> b=B() Traceback (most recent call last): File "", line 1, in File "", line 3, in __init__ TypeError: __class_

Re: Class.__class__ magic trick help

2012-08-20 Thread Ian Kelly
On Mon, Aug 20, 2012 at 12:01 PM, Massimo Di Pierro wrote: > I discovered I can do this: > > class A(object): pass > class B(object): > __class__ = A # magic > > b = B() > isinstance(b,A) # returns True (as if B derived from A) > isinstance(b,B) # also returns True

Re: Class.__class__ magic trick help

2012-08-20 Thread Steven D'Aprano
On Mon, 20 Aug 2012 11:01:36 -0700, Massimo Di Pierro wrote: > I discovered I can do this: > > class A(object): pass > class B(object): > __class__ = A # magic Why do you think that's magic? > b = B() > isinstance(b,A) # returns True (as if B derived from A) b.__

Class.__class__ magic trick help

2012-08-20 Thread Massimo Di Pierro
I discovered I can do this: class A(object): pass class B(object): __class__ = A # magic b = B() isinstance(b,A) # returns True (as if B derived from A) isinstance(b,B) # also returns True I have some reasons I may want to do this (I an object with same methods a