def outerFunc(number):
if number < 0:
def innerFunc(factor):
return number * factor
else:
def innerFunc(summand):
return number + summand
return innerFunc
x = outerFunc(-8)
print(x(4))
The result of the print statement is -32, as expected. I'm using Python 3.5.2
I would like to ask two questions regarding this code snippet:
- Is it possible to access the inner function's
numberproperty, after having boundinnerFunctoxwith the statementx = outerFunc(-8)? In other words: is it possible to directly access the preserved value ofnumber, in this case-8, after having performed the closure? - Is it good programming style to return a function, depending on the evaluation of an if-statement? Personally, I think there is better approach, but I'm not sure.
Thanks for your help.
number?