Re: Scope (?) question

2010-06-16 Thread Steven D'Aprano
On Tue, 15 Jun 2010 17:12:47 -0700, Inyeol Lee wrote: > > "execfile() cannot be used reliably to modify a function’s locals." [...] > This is due to CPython's static optimization of local name lookup. Dummy > 'exec' statement disables this and makes your example work: > > def X(): > exec "None"

Re: Scope (?) question

2010-06-16 Thread Steven D'Aprano
On Tue, 15 Jun 2010 15:22:17 -0700, Peter wrote: > I checked help on execfile and could only find the following > (mystifying) sentence: > > "execfile() cannot be used reliably to modify a function’s locals." What is mystifying about it? It's short and clear -- execfile cannot be used to reliab

Re: Scope (?) question

2010-06-16 Thread Peter Otten
Inyeol Lee wrote: > On Jun 15, 3:22 pm, Peter wrote: >> I am puzzled by what appears to be a scope issue - obviously I have >> something wrong :-) >> >> Why does this work: >> >> if __name__ == 'main': >> execfile('test-data.py') >> print data >> >> and yet this doesn't (I get "NameError: global

Re: Scope (?) question

2010-06-15 Thread Peter
This one seems to do the trick - thanks! :-) On Jun 16, 10:12 am, Inyeol Lee wrote: > On Jun 15, 3:22 pm, Peter wrote: > > > > > > > I am puzzled by what appears to be a scope issue - obviously I have > > something wrong :-) > > > Why does this work: > > > if __name__ == 'main': > >   execfile('

Re: Scope (?) question

2010-06-15 Thread Inyeol Lee
On Jun 15, 3:22 pm, Peter wrote: > I am puzzled by what appears to be a scope issue - obviously I have > something wrong :-) > > Why does this work: > > if __name__ == 'main': >   execfile('test-data.py') >   print data > > and yet this doesn't (I get "NameError: global name 'data' not > defined")

Re: Scope (?) question

2010-06-15 Thread MRAB
Peter wrote: I am puzzled by what appears to be a scope issue - obviously I have something wrong :-) Why does this work: if __name__ == 'main': execfile('test-data.py') print data and yet this doesn't (I get "NameError: global name 'data' not defined"): def X(): execfile('test-data.py')

Re: Re: scope question in a switch mixin

2008-01-16 Thread browerg
John, Thanks for writing, and I'm sorry it's taken so long to get back to you. Python is fun for me -- dinner guests and my boss got in the way. >> The code ... is the result of noodling around with switches as a learning >> tool. I've played with python for a few years, but I'm self-taught, so

Re: scope question in a switch mixin

2008-01-11 Thread John Machin
[EMAIL PROTECTED] wrote: > The code that follows is the result of noodling around with switches as a > learning tool. I've played with python for a few years, but I'm self-taught, > so . . . > > Class Switch builds a set of functions. Method switch executes one of them > given a value of the sw

Re: Scope question

2007-08-06 Thread Neil Cerutti
On 2007-08-06, Nitro <[EMAIL PROTECTED]> wrote: > Hello, > > today I wrote this piece of code and I am wondering why it does > not work the way I expect it to work. Here's the code: > > y = 0 > def func(): > y += 3 > func() > > This gives an > > UnboundLocalError: local variable 'y' reference

Re: Scope question

2007-08-06 Thread Nitro
Thanks a lot for clearing this up, Diez! -Matthias -- http://mail.python.org/mailman/listinfo/python-list

Re: Scope question

2007-08-06 Thread Diez B. Roggisch
Nitro wrote: > Hello, > > today I wrote this piece of code and I am wondering why it does not work > the way I expect it to work. Here's the code: > > y = 0 > def func(): > y += 3 > func() > > This gives an > > UnboundLocalError: local variable 'y' referenced before assignment > > If I c