mardi 3 août 2021

Programming pattern for combining factory functions

Say you have a bunch of factory functions, each of which does two things:

  1. Modify or add arguments to the initialization of a class
  2. Does something with the class instance afterwards

E.g.

class Dog:
  def __init__(self, **very_many_kwargs):
    pass

def create_police_dog(department, **dog_kwargs):
  dog_kwargs['race'] = 'pitbull_terrier'
  dog = Dog(**dog_kwargs)
  police_academy = PoliceAcademy()
  police_academy.train(dog)
  return dog

def create_scary_dog(**dog_kwargs):
  dog_kwargs['teeth_size'] = 'MAX'
  dog_kwargs['eye_color'] = fetch_angry_eye_colors('https://dogs.com')
  dog = Dog(**dog_kwargs)
  dog.experience_unhappy_childhood()
  return dog

How to combine multiple of such functions in series?

Aucun commentaire:

Enregistrer un commentaire