Extending SWIG builtin classes

前端 未结 2 418
庸人自扰
庸人自扰 2021-01-18 08:05

The -builtin option of SWIG has the advantage of being faster, and of being exempt of a bug with multiple inheritance.
The setback is I can\'t set any attribute on the g

2条回答
  •  南笙
    南笙 (楼主)
    2021-01-18 08:57

    I found a solution quite by accident. I was experimenting with metaclasses, thinking I could manage to override the setattr and getattr functions of the builtin type in the subclass.

    Doing this I discovered the builtins already have a metaclass (SwigPyObjectType), so my metaclass had to inherit it.

    And that's it. This alone solved the problem. I would be glad if someone could explain why :

    SwigPyObjectType = type(SWIGBuiltinClass)
    
    class Meta(SwigPyObjectType):
        pass
    
    class Thing(SWIGBuiltinClass):
        __metaclass__ = Meta
    
    Thing.myattr = 'anything' # Works fine this time
    

提交回复
热议问题