deferred decorator

Bryan belred1 at yahoo.com
Wed Dec 8 09:35:34 EST 2004


Nick Coghlan wrote:
> Bryan wrote:
> 
>> i'm also curious if it's possible to write this recipe using the new 
>> class style for the Deffered class.    it appears you can nolonger 
>> delegate all attributes including special methods to the contained 
>> object by using the __getattr__ or the new __getattribute__ methods.  
>> does anyone know how to port this recipe to the new class style?
> 
> 
> Override __getattribute__. I don't know why you think it doesn't let you 
> override all attribute accesses, as that's exactly what it is for.
> 
> Cheers,
> Nick.
> 

here's an example. __getattribute__ gets called for x but not for the special 
method __add__.  and the __str__ method was found in the base class object which 
printed the memory location, but didn't call __getattribute__.  so 
__getattribute__ cannot be used to capture special methods and delegate them to 
an encapsulated object.   this is why i'm asking what the technique using new 
classes would be for this recipe.


 >>> class Foo(object):
... 	def __getattribute__(self, name):
... 		raise AttributeError('__getattribute__ is called')
...
 >>> f = Foo()
 >>> f.x()
Traceback (most recent call last):
   File "<interactive input>", line 1, in ?
   File "<interactive input>", line 3, in __getattribute__
AttributeError: __getattribute__ is called
 >>> f + f
Traceback (most recent call last):
   File "<interactive input>", line 1, in ?
TypeError: unsupported operand type(s) for +: 'Foo' and 'Foo'
 >>> str(f)
'<__main__.Foo object at 0x0118AD10>'





More information about the Python-list mailing list