The code you just posted doesn't compile successfully. However, in your code, you probably have char_ptr defined at the module level, and you're confused because you didn't declare it as global. Am I right? My crystal ball has a smudge on it, but I think I can still see okay.
You can still reference module level variables that aren't declared as global, but you can't assign to them. Or rather, when you try to, you create a new local variable that shadows the global one. So the first time through, your char_ptr in the while expression is a module level variable, but the second time through, it references a local variable, because it has now been defined. Cheers, Cliff On Tue, Dec 11, 2007 at 05:18:00AM -0800, [EMAIL PROTECTED] wrote regarding Re: Dumb newbie back in shell: > > I'm less confused. If someone can explain the wisdom of this design, > I'd be grateful. > > If someone can explain why the following compiles successfully, I'd be > even more grateful: > > def get_toks( text ): > global line_ptr, last_line > while line_ptr < last_line: > while char_ptr < len(text[line_ptr]): > if matches_EOI(): > tokens.append( Token(EOI) ) > elif matches_EOL(): > tokens.append( Token(EOL) ) > line_ptr += 1 > char_ptr = 0 > > Shouldn't "char_ptr" be flagged as an error, appearing in line 4 > before being a lhs in the last line? > > Martin > > [EMAIL PROTECTED] wrote: > > Peter, > > > > question is, why did the first one work? In my real code I've got > > module-level vars and an error msg trying to use them in a function. > > In my test example I've got them accessed from within a function w/o > > error message. > > > > I am confused. > > > > Martin > > > > Peter Otten wrote: > > > MartinRinehart wrote: > > > > > > > However, here's the little tester I wrote: > > > > > > > > # t.py - testing > > > > > > > > global g > > > > g = 'global var, here' > > > > > > > > def f(): > > > > print g > > > > > > > > f() > > > > > > > > It prints 'global var, here,' not an error message. Wassup? > > > > > > Try it again with a modified f(): > > > > > > def f(): > > > print g > > > g = 42 > > > > > > In Python variables that are assigned to in a function are > > > function-local by default. > > > > > > Peter > -- > http://mail.python.org/mailman/listinfo/python-list -- http://mail.python.org/mailman/listinfo/python-list