'local' var in module

  • Thread starter Andreas Lobinger
  • Start date
A

Andreas Lobinger

Aloha,

i'm i little bit confused by the following behaviour...

lobingera@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@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.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 assignmentSomehow 'rstate' is readable in f, but not in g, but paras is?

Wishing a happy day
LOBI
 
A

Aaron Bingham

Andreas said:
Aloha,

i'm i little bit confused by the following behaviour...

lobingera@sibyl: cat flip.py
[working code deleted]

If you assign to a variable inside a function, it will define a variable
local to that function, unless you explicity declare that variable as a
global, like so:

global rstate
random.seed(rstate)
s = list()

for i in range(paras['flength']):
s.append(random.choice(paras['cset']))

rstate = random.getseed()
return "".join(s)

Aaron
 
J

Josiah Carlson

lobingera@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)

When you are only reading things, Python will pull variables from the
local or global scope as necessary.

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)

If you try to write to a variable in a scope, Python believes that the
variable is local to this scope, so doesn't attempt to fetch it from
globals, and will only write to the local copy.

That is, unless you include the 'global' keyword. Add:
global rstate
to g() and watch everything work the way you expect.

- Josiah
 
M

Mel Wilson

Aloha,

i'm i little bit confused by the following behaviour...

lobingera@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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top