[Python-ideas] The Descriptor Protocol...
Martin Chilvers
martin.chilvers at gmail.com
Thu Mar 3 12:52:38 CET 2011
Hi Raymond,
Thanks for your replies. We currently use the traditional
__getattribute__ hook, and the original question came up as part of a
debate about the various merits of descriptors vs __getattribute__.
Martin
On 02/03/2011 19:35, Raymond Hettinger wrote:
> P.S. Python is very customizable. The descriptor protocol in
> implemented by __getattribute__, so it's not a hard exercise to write
> your own __getattribute__ to implement your own variant of the
> descriptor protocol. Here's a quick and dirty example:
>
> class Str: 'Descriptor using a custom protocol' def __my_get__(self,
> obj, key): print(key * 5) return obj
>
> class A:
>
> @property # old protocol def x(self): return 10
>
> y = Str() # new protocol
>
> def __getattribute__(self, key): 'Implement an alternative descriptor
> protocol' attr = object.__getattribute__(self, key) if hasattr(attr,
> '__my_get__'): return attr.__my_get__(attr, key) return attr
>
> a = A() print(a.x) print(a.y)
>
>
More information about the Python-ideas
mailing list