A class I cannot change the code of has a method from_lowercase
that returns an instance of itself. Now I would like to inherit a class. However, when calling the inherited class over from_lowercase
an instance of the original class is returned.
class Animal:
"""I CANNOT change the code in this class"""
def __init__(self, uppercase_name: str):
self.uppercase_name = uppercase_name
def say(self) -> str:
return f"I am {self.uppercase_name}"
@staticmethod
def from_lowercase(lowercase_name: str) -> "Animal":
return Animal(lowercase_name.upper())
class PoliteAnimal(Animal):
def say(self) -> str:
return f"Hello dear sir, {super().say()}!"
# insert magic here
calling some examples:
animal = Animal("JOHN")
animal_from_lowercase = Animal.from_lowercase("john")
nice_animal = PoliteAnimal("MARIA")
nice_animal_from_lowercase = PoliteAnimal.from_lowercase("maria")
print(animal.say())
print(animal_from_lowercase.say())
print(nice_animal.say())
print(nice_animal_from_lowercase.say())
leads to the following:
I am JOHN
I am JOHN
Hello dear sir, I am MARIA!
I am MARIA
However, the desired output is the following:
I am JOHN
I am JOHN
Hello dear sir, I am MARIA!
Hello dear sir, I am MARIA!
Obviously nice_animal_from_lowercase
is an instance of Animal
and not PoliteAnimal
. How can this be fixed?