How to mix-in __getattr__ after the fact?

Lie Ryan lie.1296 at gmail.com
Mon Nov 7 23:17:14 EST 2011


On 10/31/2011 11:01 PM, dhyams wrote:
>
> Thanks for all of the responses; everyone was exactly correct, and
> obeying the binding rules for special methods did work in the example
> above.  Unfortunately, I only have read-only access to the class
> itself (it was a VTK class wrapped with SWIG), so I had to find
> another way to accomplish what I was after.
>

As a big huge hack, you can always write a wrapper class:

class Wrapper(object):
     def __init__(self, *args, **kwargs):
         self.__object = MySWIGClass(*args, **kwargs)
     def __getattr__(self, attr):
         try:
             return getattr(self.__object, attr)
         except AttributeError:
             ...




More information about the Python-list mailing list