0

If I have some function like that:

def function(some number, steps):
  if steps == 1: return 1
  result = some calculation + function(some number, steps - 1)
  return round(result, 2)

which returns some float number.
My question is: Is there a way to return rounded result only when I return result from whole function but not from recursive call (to save precision to calculation).
I hope you understand my question.
Thanks in advance.

Zarkos
  • 29
  • 4

4 Answers4

2

Generally speaking, if you want to detect that you are inside a recursive call in Python, you can add a default parameter to the function like so:

def my_func(arg1, arg2, _inside_recursion=False):
   # Default calls will have _inside_recursion as False
   # Make sure that your recursive calls set the parameter
   recursive_result = my_func(arg1, arg2, _inside_recursion=True)
Barak Itkin
  • 4,872
  • 1
  • 22
  • 29
2

There's no way you can distinguish whether your current function is being invoked as part of the recursion or as it's first step. You can, however, just introduce a new function which will call the existing one and do any extra rounding.

def internal_function(some number, steps):
    if steps == 1: return 1
    return some calculation + function(some number, steps - 1)

def function(some_number, steps):
    return round(internal_function(some_number, steps), 2)

Edit: while true that the approach with adding an optional argument to the function can serve as well it has two downsides:

  • pollutes the public interface of the function - your users are now wondering whether they should give the argument a value and what value should they give exactly
  • makes the computational part longer to read and can also potentially destroy tail-call optimizations.
Nikola Dimitroff
  • 6,127
  • 2
  • 25
  • 31
  • Pollution can be solved with a default value for the parameter and AFAIK python does not do any TCO [source](https://stackoverflow.com/questions/13591970/does-python-optimize-tail-recursion) – Frieder May 09 '19 at 11:21
  • Pollution is somewhat hidden with a default value but it would still appear if running `help` / reading docs / checking the code. And although CPython doesn't implement TCO, this doesn't mean that other implementations don't / won't do it in the future. – Nikola Dimitroff May 09 '19 at 14:42
2

Yes! You can provide a flag that will get triggered for consecutive calls:

def function(some number, steps, do_rounding=True):
    if steps == 1:
        return 1
    result = some calculation + function(some number, steps - 1, False)
    if do_rounding:
        return round(result, 2)
    else:
        return result
Frieder
  • 1,208
  • 16
  • 25
0

Maybe you want something like this? This is not equivalent to OP code, but makes sense. It applies some_calculation recursively steps times and rounds the final value.

def function(x, steps):
  if steps == 1: return round(x, 2)
  return function(some_calculation(x), steps - 1)
ziggystar
  • 28,410
  • 9
  • 72
  • 124