Python: programmatically subclassing based on __init__ values

I have a base class from which I would like to make many subclasses. Subclasses differ only in the arguments used to invoke the base class during instance creation. The example below shows how to subclass Apple . Is there a way to do this programmatically, without having to code the subclass method __init__ ? This seems to work for metaclasses, but in this case I cannot change the base class.

 apple = {'color': 'red', 'shape': 'sphere'} pear = {'color': 'yellow', 'shape': 'cone'} melon = {'color': 'green', 'shape': 'prolate'} class Fruit(object): def __init__(self, color, shape): self.color = color self.shape = shape class Apple(Fruit): def __init__(self): Fruit.__init__(self, **apple) 
+4
source share
3 answers

See the type () function.

 def make_fruit(name, kwargs): def my_init(self): Fruit.__init__(self, **kwargs) return type(name, (Fruit,), {'__init__': my_init}) Apple = make_fruit('Apple', apple) 
+5
source

Using type :

 class Fruit(object): def __init__(self, color, shape): self.color = color self.shape = shape apple = {'color': 'red', 'shape': 'sphere'} pear = {'color': 'yellow', 'shape': 'cone'} melon = {'color': 'green', 'shape': 'prolate'} g = globals() for clsname, attrs in [('Apple', apple), ('Pear', pear), ('Melon', melon)]: def temp(attrs): g[clsname] = type(clsname, (Fruit,), { '__init__': lambda self: Fruit.__init__(self, **attrs) }) temp(attrs) 

 >>> a = Apple() >>> p = Pear() >>> m = Melon() >>> assert a.color == 'red' and a.shape == 'sphere' >>> assert p.color == 'yellow' and p.shape == 'cone' >>> assert m.color == 'green' and m.shape == 'prolate' 
+4
source

I think that the solution you are looking for does not exist: I have everything you did in the subclasses have the same constructor, then the only thing that makes the class unique is its name. And I don’t think you want to have a generic constructor that checks the class name to choose what to do.

So I think:

  • or you override the constructor in each subclass, and specify which parameter should go to the parent constructor,
  • or you put certain constant values ​​in some member of the class, and the constructor uses it to call the parent constructor

See an interesting thing in this other thread: Factory class in Python

0
source

Source: https://habr.com/ru/post/1498592/


All Articles