[Zope-dev] Intercepting __getattr__ in a Python 2.3 old-style mixin class?
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 :-)
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 class A:
> 2 x = 'foo'
> 3 def __getattr__(self, key):
> 4 return 'bar'
> In : a = A()
> In : a.x
> Out: 'foo'
> In : a.b = 'b'
> In : a.b
> Out: 'b'
> Maybe I am missing something here.
> Mit freundlichen Grüßen
> Michael Howitz
-------------- next part --------------
A non-text attachment was scrubbed...
Size: 618 bytes
Desc: OpenPGP digital signature
More information about the Zope-Dev