--

But that isn't what's happening here in your first example. The intermediate results are completely new objects with different values.

Also, your API is not good because it pretends to be functional, but it isn't: it also mutates the original object, which is a bad feature.

Consider this code sample:

calc = Calculator(10)
print(calc.value) # 10

result = calc.add(5).multiply(2).subtract(3).value
print(result) # 27
print(calc.value) # Also 27!

In this example, it’s easy to see the issue, but in a longer code snippet, it’d be much harder to see what was going on.

--

--