'local' var in module
Andreas Lobinger
andreas.lobinger at netsurf.de
Thu Oct 7 11:02:24 EDT 2004
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)
I try to save the state of the random module locally in flip.
I also have other parameters (paras) in the module.
lobingera at sibyl: python
Python 2.2.2 (#3, Apr 10 2003, 17:06:52)
[GCC 2.95.2 19991024 (release)] on sunos5
Type "help", "copyright", "credits" or "license" for more information.
>>> import flip
>>> flip.f()
'akdkdgkaiflieafceagele'
>>> flip.f()
'akdkdgkaiflieafceagele'
>>> flip.g()
Traceback (most recent call last):
File "<stdin>", line 1, in ?
File "flip.py", line 17, in g
random.seed(rstate)
UnboundLocalError: local variable 'rstate' referenced before assignment
>>> flip.f()
'akdkdgkaiflieafceagele'
>>>
Somehow 'rstate' is readable in f, but not in g, but paras is?
Wishing a happy day
LOBI
More information about the Python-list
mailing list