Create a subclass from an existing superclass

So, I interact with a third-party library that raises someti0mes exceptions, but raises the same exception for everything (in particular suds.WebFault. However, it is possible to determine the exact type of the exception from the data on Exclusion. I am looking for an abstract exception in the exceptions that are subclasses of this type, those.

from suds import WebFault

class MoreSpecificError(WebFault):
    pass

I like the subclass because it will not break the existing implementations that are expected WebFault. The problem that I have encountered, is transferring data already WebFaultin MoreSpecificError. What I would like to do is take an existing exception object and simply β€œturn it into” a subclass without any change or re-launch __init__. What I have done so far to accomplish this looks something like this:

from suds import WebFault

class MoreSpecificError(WebFault):
    # You pass the old WebFault when instantiating this exception.
    # Then it gets poofed into a MoreSpecificError type
    def __new__(cls, internal_fault):
        return internal_fault
    # This is necessary to prevent the parent class' __init__ from being run
    def __init__(self, internal_fault):
        pass

Edit: now I know that this does not work, the new object will not have a subclass type

"", __init__, pass . , __new__ (: deepcopy, , () , .)

, ? , , . , Python - . .

+4
1

, , :

class MoreSpecificError(WebFault):
    def __new__(self, old):
        old.__class__ = MoreSpecificError
        return old

, . , , , , .

, . , , , API . , , API, - API.

+1

All Articles