'local' var in module

Mel Wilson mwilson at the-wire.com
Thu Oct 7 17:37:43 CEST 2004


In article <ck3lq0$egb$1 at news.mch.sbs.de>,
Andreas Lobinger <andreas.lobinger at netsurf.de> wrote:
>Aloha,
>
>i'm i little bit confused by the following behaviour...
>
>lobingera at sibyl: cat flip.py
>import random
>
>paras = { 'flength' : 22,
>           'cset' : 'abcdefghijkl' }
>rstate = 1
>
>def f():
>    random.seed(rstate)
>    s = list()
>
>    for i in range(paras['flength']):
>       s.append(random.choice(paras['cset']))
>
>    return "".join(s)
>
>def g():
>    random.seed(rstate)
>    s = list()
>
>    for i in range(paras['flength']):
>       s.append(random.choice(paras['cset']))
>
>    rstate = random.getseed()
>    return "".join(s)
>
[ ... ]
>Somehow 'rstate' is readable in f, but not in g, but paras is?

   It's the `rstate = random.getseed()` in g, which forces
rstate to be local.  Put a `global rstate` in g and you
solve the visiblity problem.

        Regards.        Mel.



More information about the Python-list mailing list