Support Anchoring Mechanism for Method Execution in Inherited Classes

I may be wrong about naming but i’ll do my best to express it in the easiest language
anchoring mechanism that enables to specify a point within a method of the parent class up to which point execution can be continued in the subclass.

basically you could easily specify a point within a parent method where execution should stop when called from a child class using super().

for instance the easiest thing came to my mind is this:

class Parent:
    def test(self, a, b):
        # Line 1
        result = a + b
      @anchor_point
        return result / 2

class Child(Parent):
    def test(self, a, b):
        # Call the parent method, but stop at anchor_point
        result = super().test(a, b)
        return result ** 2

or is there any alternative options to fix this issue ?

You can do this without a language change if you move the “point within a parent method where execution should stop” to its own function:

class Parent:
    def _compute_result(a, b):
        return a + b

    def test(self, a, b):
        result = self._compute_result(a, b)
        return result / 2

class Child(Parent):
    def test(self, a, b):
        result = self._compute_result(a, b)
        return result ** 2
2 Likes

The variable result is not the same one in the parent and child.
Which you seem to be assuming it seems?

class Parent:
    def test(self, a, b):
        # Line 1
        x = a + b
      @anchor_point
        return x / 2

This version is semantically identical using local variable `x’.

Suspending execution is precisely what Generators are for. It’s not limited to children calling their parent methods, but they can call a generator on super, just like any other method.

The caller can obtain however many values they wish from the returned iterator; they just have to iterate over them (e.g. with next calls, or a for loop), instead of only using an assignment with a normal function call. Statements in the function are only executed up to the point that the last yielded value is required.

Lazy Evaluation, is the term for this whole area in CS.

1 Like

Agree this would fulfill the requirements either but i was seeking a way to avoid separating operations from eachother Thnx for the reply . :pray:

I find this approach easier, I also can add my logic to break if it exceeds the specified needs

Thanks i do appreciate :pray: