Advertisement
SepandMeenu

Mutable state for a function

Jul 10th, 2019
442
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.20 KB | None | 0 0
  1. # first attempt: explicit state
  2. def foo(x, state_mut):
  3.     state_mut[0] += 1
  4.     return x * 2
  5.  
  6. state = [0]  # initial state
  7. x = 3
  8. x1 = foo(x, state)
  9.  
  10. print("x1 = {}, state = {}".format(x1, state))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement