How Does This Static Variable Work?

Victor Subervi victorsubervi at
Fri Jan 4 16:17:09 CET 2008

I read this example somewhere, but I don't understand it <:-) Can someone
please explain how static variables work? Or recommend a good how-to?

import random

def randomwalk_static(last=[1]): # init the "static" var(s)

  rand = random.random() # init a candidate value

  if last[0] < 0.1: # threshhold terminator

    return None # end-of-stream flag

  while abs(last[0]-rand) < 0.4: # look for usable candidate

    print '*', # display the rejection

    rand = random.random() # new candidate

  last[0] = rand # update the "static" var
  return rand

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <>

More information about the Python-list mailing list