Computing the "closure" of the attributes of an object given functions that change the attributes

Viewed 293

I have an object obj and a number of functions

    def func1(obj):
    #...

    def func2(obj):
    #...

    def func3(obj):
    #...

that each change the values of the attributes of obj.

I want my input to be something like

obj = MyObject()
obj.attr=22

This should be passed to a function closure() that computes all possible applictions of the functions above, meaning func1(func2(obj)), func3(func1(func1(obj))) etc. up to a certain stopping condition (e.g. no more than 20 function compositions).

The output should be a list of all possible outputs together with all paths leading there. So if, say 104 and 93 are the one possible final outputs if obj.attr=22, and there are two ways to arrive at 104 and one to arrive at 93. Then

print closure(obj)

should be something like

[22, 64, 21, 104] #first path to 104 through , func1(obj),func1(func1(obj)), func1(func1(func3(obj)))

[22, 73, 104] #second path to 104 through , func3(obj),func3(func2(obj)), 

[22, 11, 93] #the only path to arrive at 94

How could I implement this? As was suggested in the comments, this is best done with trees, but although I tried 2 days I almost didn't make any progress implementing that (I'm new to Python/programming)!
My example is so simple that instead of func(obj) we could directly use func(22) but the example I need to work on is more complicated, where I definitely will need to use objects, so this would only be a minimal working example for that.

The tree will probably not be a full n-ary tree, since each function application will contain a test whether it can be applied to the current state (of the attributes) of obj and in some cases the test will fail leaving (the attributes of) obj unchanged.

2 Answers
Related