[Zope-dev] Intercepting __getattr__ in a Python 2.3 old-style mixin class?
Andreas Jung
lists at zopyx.com
Mon Nov 7 14:01:25 CET 2016
Python 2.7 is different because all classes are automatically new-style classes.
In Python 2.7 you would have to overwrite __getattribute__() but I am on Python 2.3 here.
And I have to stay on Python 2.3 for now…welcome to hell :-)
Andreas
On 7 Nov 2016, at 13:23, Michael Howitz wrote:
> Am 07.11.2016 um 13:01 schrieb Andreas Jung <lists at zopyx.com>:
>> I have a large Python 2.3 based installation with 200k LOC. As part of a migration project I need to intercept all attribute lookups of all old-style class.
>>
>> Old legacy code:
>>
>> class Foo(Bar):
>> ...
>>
>>
>> My idea is to inject a common mixin class like
>>
>> class Foo(Bar, Mixin):
>> ...
>>
>> class Mixin:
>>
>> def __getattr__(self, k)
>> print repr(self), k
>> return Foo.__getattr__(self, k)
>
> Hi Andreas,
>
> I doubt that your approach works. Attributes defined on `Foo` are _not_ resolved using __getattr__.
> See the following example using Python 2.7:
>
> In [1]: 1 class A:
> 2 x = 'foo'
> 3 def __getattr__(self, key):
> 4 return 'bar'
>
> In [2]: a = A()
>
> In [3]: a.x
> Out[3]: 'foo'
>
> In [4]: a.b = 'b'
>
> In [5]: a.b
> Out[5]: 'b'
>
> Maybe I am missing something here.
>
> --
> Mit freundlichen Grüßen
> Michael Howitz
>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 618 bytes
Desc: OpenPGP digital signature
URL: <http://mail.zope.org/pipermail/zope-dev/attachments/20161107/251a07df/attachment.sig>
More information about the Zope-Dev
mailing list