Re: [myhdl-list] strange problem
Brought to you by:
jandecaluwe
From: Neal B. <ndb...@gm...> - 2009-03-24 13:25:56
|
Jan Decaluwe wrote: > Neal Becker wrote: >> I'm still trying to wrap my head around myhdl. This one is weird. > > It is purely a Python scope issue. Consider: > > def f(): > n = 0 > def g(): > n += 1 > print n > g() > > f() > > Running this gives: > >> python tmp.py > Traceback (most recent call last): > File "tmp.py", line 8, in <module> > f() > File "tmp.py", line 6, in f > g() > File "tmp.py", line 4, in g > n += 1 > UnboundLocalError: local variable 'n' referenced before assignment > > > To understand what happens, consider that 'n += 1' is equivalent to: > n = n + 1 > > When you *assign* to n somewhere locally, Python considers it a > local variable. As the n in the rhs as not been locally assigned yet, > you get the error. > Note that as long you don't assign to n, you would have read > access to the n in the enclosing scope. > > For your case, put the initialization in the generator before > the while loop. > > Jan That worked fine, until I needed to access (read) a variable also in another generator. I find that: x = Signal (0) def gen1... x.next = x + 1 def gen2... print x Works fine, but if x was just a simple variable, say int, we have the referenced before assignment (of course, using x = x+1 instead of x.next = x+1). Other than using a Signal, any other solution? It seems silly to use Signal for something that is just part of my test setup. |