How Does This Static Variable Work?

Victor Subervi victorsubervi at gmail.com
Fri Jan 4 10:17:09 EST 2008


 Hi;
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

TIA,
Victor
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20080104/32c835c8/attachment.html>


More information about the Python-list mailing list