Dumb newbie back in shell

MartinRinehart at gmail.com MartinRinehart at gmail.com
Mon Dec 10 12:10:13 EST 2007


Thanks, Marc.

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?

Marc 'BlackJack' Rintsch wrote:
> On Mon, 10 Dec 2007 08:31:01 -0800, MartinRinehart wrote:
>
> > But that gets me to:
> >
> > ... line 110, in get_toks
> > UnboundLocalError: local variable 'line_ptr' referenced before
> > assignment
> >
> > Here's a bit of the code, with line #s
> >
> > ...
> > 68 global line_ptr
> > 69 global char_ptr
> > ...
> > 75 line_ptr = 0
> > 76 char_ptr = 0
> > ...
> > 109 def get_toks( text ):
> > 110     while line_ptr < last_line:
> > ...
> > So when is a global var global?
>
> When you declare it ``global`` *in the function*.  ``global`` on module
> level has no effect.  IMHO that should emit at least a warning...
>
> Ciao,
> 	Marc 'BlackJack' Rintsch



More information about the Python-list mailing list