Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Make a method return an object of a subclass

I want to provide method to a class, so that it gets transformed into a special case, represented by a subclass. I’m not sure, however what is the best way to do it.

Here is an illustrative example:

class Vector(pd.Series):
    def normalize(self) -> "NormalizedVector":
        result: "NormalizedVector" = self / sum(self)  # type:ignore
        return result

class NormalizedVector(Vector):
    pass

There are issues with this implementation, though:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

  • isinstance(vector.normalize(), NormalizedVector) will be False
  • (relatedly) the # type: ignore annotation is required for static type checking

So my question is: what is a clean way of achieving this?

The alternative I found was using dynamic allocation:

class Vector(pd.Series):
    pass

class NormalizedVector(Vector):
    pass

def normalize_vector(self: Vector) -> NormalizedVector:
    return NormalizedVector(self / sum(self))

Vector.normalize = normalize_vector

However:

  • I find it makes the code much less readable
  • I’m afraid to be messing with the end method metadata, such as __name__

>Solution :

A type hint alone doesn’t cause a value to become the hinted type. You need to create an instance. Your second approach works because you actually created that instance. The explicit assignment is unnecessary.

class Vector(pd.Series):
    def normalize(self) -> "NormalizedVector":
        return NormalizedVector(self / sum(self))

class NormalizedVector(Vector):
    pass
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading