Example:
def func(n):
if n > 10:
return
CONSTANT = n
print(n CONSTANT)
func(n 1)
>>> func(1)
2
3
4
5
6
7
8
9
10
11
>>> func(7)
14
15
16
17
Obviously this specific task can be achieved via different routes, but I'm wondering if this is possible to do: have a constant value in a recursive function that's expressed in terms of a changing variable.
CodePudding user response:
Make it an optional parameter that you only pass in the recursive calls.
def func(n, CONSTANT = None):
if n > 10:
return
if CONSTANT is None:
CONSTANT = n
print(n CONSTANT)
func(n 1, CONSTANT)
CodePudding user response:
Why don't you just make the value you passed as argument a global variable and access it from your function?