mercredi 29 août 2018

(How) can I put an apply_to_all_members_of_class object/method between child and parent methods in command chain?

How can I easily apply the methods of a parent to all instances (registered in a child)?

For ease of programming, I am looking to implement something like this:

SpecialCaseChildClass.apply_to_all_members_of_child_class.any_method_of_parent()

Even more awesome would be an implementation in which I can supply a list of arguments that allows me to iterate arguments:

SpecialCaseChildClass.apply_to_all_members_of_child_class.any_method_of_Parent(
argument1_of_parent_method = range(x), argument2_of_parent_method = range(y))

I have tried to implement this functionality with a class method:

class SpecialCaseChildClass(Parent):

    registry = []

    def __init__(self, *child_argv):
        super().__init__(self, *child_argv)
        registry.append(self)

    @classmethod
    def apply_to_all(function, *parent_argv):
        for child in registry:
            function(child, *parent_argv)

which would (hopefully) allow something like this:

SpecialCaseChildClass.apply_to_all_members_of_child_class(any_Parent_method, *argv)

but that is not pretty, and requires me to add named lists etc.

Aucun commentaire:

Enregistrer un commentaire