I have some Python classes being generated automatically using SWIG and I want to extend them.
The problem is, those classes have another method other than __init__, called create that returns a class instance (they are the real init).
I want to extend those classes with new methods but keep the original name.
I tried to implement __new__ method but it returns an instance of the parent class
I get why it happens, but I can't seem to find a way to change it while keeping the create methods.
Let me show you an example with my current implementation attempt in order to explain:
File a:
class A:
def __init__(self, ...):
pass
def create(self, ...):
# do some stuff
return object_of_A
File b:
from a import A as AClass
class A(AClass):
def __init__(self, ...):
pass
def __new__(cls, ...):
# do some stuff
return super(A, cls).create(...)
def foo(self):
print('Hi')
Wanted behavior:
>>> from b import A
>>> a = A(...)
>>> a.foo()
>>> Hi
Actual behavior:
>>> from b import A
>>> a = A(...)
>>> a.foo()
>>> AttributeError: 'A' object has no attribute 'foo'
Thanks!