Re: Intercepting __getattr__ in a Python 2.3 old-style mixin class?

"Andreas Jung" <[email protected]> Mon, 07 Nov 2016 14:01:25 +0100
Newsgroups gmane.comp.web.zope.devel
Message-ID <[email protected]>
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 <[email protected]>:
>> 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
>

_______________________________________________
Zope-Dev maillist  -  [email protected]
https://mail.zope.org/mailman/listinfo/zope-dev
**  No cross posts or HTML encoding!  **
(Related lists -
 https://mail.zope.org/mailman/listinfo/zope-announce
 https://mail.zope.org/mailman/listinfo/zope )
signature.asc (application/pgp-signature, 618 B)
-----BEGIN PGP SIGNATURE-----

iQGUBAEBCgAGBQJYIHsmAAoJEADcfz7u4AZjO20LwMzUxcpwsU01B+2rt49Hhp+m
dIld5aGfT8++ydMTOgixgDBbODBtf6Tw0JmWF5gOqTFv0R+6muTAHcOTACfqwjn3
Zdz3M9ABvhq1mOrSsGIrXvarWV7JicfUh9mnHSXuCbGNo2Noy7XiPu4pdUSDbv8W
1xWrj4eACeyrm48wPGz4Te6XjxAfyYBL1s9r98pZ379NI7eJlYyiibTk+XRA7hRU
jLN2Ksf0xoIE6lIvha7iOUPQeHrbGCa5jNkBddsG76SVyNMae5YokEbsHyY811UV
7lky0Akdf8f8ENzShX4c2f4OKVYvvATztKDsHf2Snqq9Ezw1L6wM8U1XAqzkk8NN
xJFbG73LNPirsSnjJ2iBQbcB3xqsEQmSSOq8lkRdyXpM/obZv6QZyEO+QuJs/HZw
ZqrMFVbU8m7X3OJoagIX0Y8e2Tuk21qWHyU7f7ZXmcbxq+4BJo5bprdt7aI/0z3f
4rv6aPth6O/wW6PmGTQZT/QX+sq0xFo=
=SMwT
-----END PGP SIGNATURE-----