I am curious whether there is a better way to achieve something like this? My intentions are to avoid unnecessary boilerplate. The provided example is obviously just simple enough to let the others understand what I had on my mind.
def create_parametrized_class(animal):
class SomeClass:
def __init__(self, name):
self.name = name
def __str__(self):
return "{}: {}".format(animal, self.name)
return SomeClass
class Cat(create_parametrized_class("Cat")):
pass
class Dog(create_parametrized_class("Dog")):
pass
cat = Cat("Micka")
dog = Dog("Rex")
assert str(cat) == "Cat: Micka", "Cats..."
assert str(dog) == "Dog: Rex", "Dogs..."