Re: Python scope question

2019-03-12 Thread Arup Rakshit
Hello, Thanks for this beautiful link. This is amazing. Thanks, Arup Rakshit a...@zeit.io > On 13-Mar-2019, at 12:11 AM, DL Neil wrote: > > Arup, > > > On 13/03/19 3:38 AM, Arup Rakshit wrote: >> I have questions how nonlocal and global affecting the variable assignment. >> Also how

Re: Python scope question

2019-03-12 Thread DL Neil
Arup, On 13/03/19 3:38 AM, Arup Rakshit wrote: I have questions how nonlocal and global affecting the variable assignment. Also how each print statement looking up the values for the spam variable. This scope thing in python is very confusing too me still. Can anyone help me to understand

Re: Python scope question

2019-03-12 Thread Grant Edwards
On 2019-03-12, Arup Rakshit wrote: > I have questions how nonlocal and global affecting the variable > assignment. Also how each print statement looking up the values for > the spam variable. This scope thing in python is very confusing too > me still. Can anyone help me to understand this code

Python scope question

2019-03-12 Thread Arup Rakshit
I have questions how nonlocal and global affecting the variable assignment. Also how each print statement looking up the values for the spam variable. This scope thing in python is very confusing too me still. Can anyone help me to understand this code w.r.t to scope in Python? def

Re: Scope (?) question

2010-06-16 Thread Peter Otten
Inyeol Lee wrote: On Jun 15, 3:22 pm, Peter peter.milli...@gmail.com 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:

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 reliably

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

Scope (?) question

2010-06-15 Thread Peter
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') print data

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: Scope (?) question

2010-06-15 Thread Inyeol Lee
On Jun 15, 3:22 pm, Peter peter.milli...@gmail.com 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'

Re: Scope (?) question

2010-06-15 Thread Peter
This one seems to do the trick - thanks! :-) On Jun 16, 10:12 am, Inyeol Lee inyeol@gmail.com wrote: On Jun 15, 3:22 pm, Peter peter.milli...@gmail.com wrote: I am puzzled by what appears to be a scope issue - obviously I have something wrong :-) Why does this work: if

variable scope question?

2008-05-13 Thread globalrev
http://mail.python.org/pipermail/python-list/2003-October/233435.html why isnt it printing a in the second(second here, last one in OP) example before complaining? def run(): a = 1 def run2(b): a = b print a run2(2) print a run() def run(): a = 1 def run2(b): print a

Re: variable scope question?

2008-05-13 Thread Gary Herron
globalrev wrote: http://mail.python.org/pipermail/python-list/2003-October/233435.html why isnt it printing a in the second(second here, last one in OP) example before complaining? def run(): a = 1 def run2(b): a = b print a run2(2) print a run() def run(): a = 1 def

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

scope question in a switch mixin

2008-01-11 Thread browerg
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 switch variable. My question is, why

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 switch

Re: Another dumb scope question for a closure.

2008-01-10 Thread Fredrik Lundh
Steven W. Orr wrote: The problem only happens if I try to modify jj. It only happens if you try to *bind* the name jj to an object inside the function. What am I not understanding? My guess is that you have a C/C++ view of variables and values, where variables are locations in memory that

Re: Another dumb scope question for a closure.

2008-01-10 Thread Steven W. Orr
On Wednesday, Jan 9th 2008 at 14:01 -, quoth Fredrik Lundh: =Steven W. Orr wrote: = = So sorry because I know I'm doing something wrong. = = 574 cat c2.py = #! /usr/local/bin/python2.4 = = def inc(jj): = def dummy(): = jj = jj + 1 = return jj = return dummy = =

Another dumb scope question for a closure.

2008-01-09 Thread Steven W. Orr
So sorry because I know I'm doing something wrong. 574 cat c2.py #! /usr/local/bin/python2.4 def inc(jj): def dummy(): jj = jj + 1 return jj return dummy h = inc(33) print 'h() = ', h() 575 c2.py h() = Traceback (most recent call last): File ./c2.py, line 10, in

Re: Another dumb scope question for a closure.

2008-01-09 Thread Fredrik Lundh
Steven W. Orr wrote: So sorry because I know I'm doing something wrong. 574 cat c2.py #! /usr/local/bin/python2.4 def inc(jj): def dummy(): jj = jj + 1 return jj return dummy h = inc(33) print 'h() = ', h() 575 c2.py h() = Traceback (most recent

Re: Another dumb scope question for a closure.

2008-01-09 Thread Mike Meyer
On Wed, 9 Jan 2008 13:47:30 -0500 (EST) Steven W. Orr [EMAIL PROTECTED] wrote: So sorry because I know I'm doing something wrong. 574 cat c2.py #! /usr/local/bin/python2.4 def inc(jj): def dummy(): jj = jj + 1 return jj return dummy h = inc(33) print

Re: Another dumb scope question for a closure.

2008-01-09 Thread Ben Fisher
One way to get this to work is: def inc(jj): def dummy(jj = jj): jj = jj + 1 return jj return dummy h = inc(33) print h() It's not very pretty though, especially when you have many variables you want to have in the inner scope. -Ben On 1/9/08,

Re: Another dumb scope question for a closure.

2008-01-09 Thread Arnaud Delobelle
On Jan 9, 8:24 pm, Mike Meyer [EMAIL PROTECTED] wrote: On Wed, 9 Jan 2008 13:47:30 -0500 (EST) Steven W. Orr [EMAIL PROTECTED] wrote: So sorry because I know I'm doing something wrong. 574 cat c2.py #! /usr/local/bin/python2.4 def inc(jj):      def dummy():          jj = jj +

Re: Another dumb scope question for a closure.

2008-01-09 Thread Fredrik Lundh
Ben Fisher wrote: One way to get this to work is: def inc(jj): def dummy(jj = jj): jj = jj + 1 return jj return dummy h = inc(33) print h() It's not very pretty though, especially when you have many variables you want to have in the inner

Re: Another dumb scope question for a closure.

2008-01-09 Thread Waldemar Osuch
On Jan 9, 11:47 am, Steven W. Orr [EMAIL PROTECTED] wrote: So sorry because I know I'm doing something wrong. 574 cat c2.py #! /usr/local/bin/python2.4 def inc(jj): def dummy(): jj = jj + 1 return jj return dummy h = inc(33) print 'h() = ', h() 575 c2.py

Re: Another dumb scope question for a closure.

2008-01-09 Thread Waldemar Osuch
On Jan 9, 3:52 pm, Waldemar Osuch [EMAIL PROTECTED] wrote: On Jan 9, 11:47 am, Steven W. Orr [EMAIL PROTECTED] wrote: So sorry because I know I'm doing something wrong. 574 cat c2.py #! /usr/local/bin/python2.4 def inc(jj): def dummy(): jj = jj + 1

Scope question

2007-08-06 Thread Nitro
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 change the function like this: y = 0 def

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 change the

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 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' referenced before

newb: Scope Question

2007-06-22 Thread johnny
Scope of ids: When I print ids, it's always empty string '', as I have intialized before. That's not what I want. I want the ids to have str(r['id']).join(',') if res: ids = '' for r in res['key']: ids = str(r['id']).join(',')

Re: newb: Scope Question

2007-06-22 Thread James Stroud
johnny wrote: Scope of ids: When I print ids, it's always empty string '', as I have intialized before. That's not what I want. I want the ids to have str(r['id']).join(',') if res: ids = '' for r in res['key']: ids =

Re: newb: Scope Question

2007-06-22 Thread attn . steven . kuo
On Jun 22, 3:53 pm, johnny [EMAIL PROTECTED] wrote: Scope of ids: When I print ids, it's always empty string '', as I have intialized before. That's not what I want. I want the ids to have str(r['id']).join(',') if res: ids = '' for r in res['key']:

Re: why is this not working? (nested scope question)

2006-07-27 Thread biner . sebastien
Actually, the code in the book is: def f1(): x = 88 f2(x) def f2(x): print x f1() which makes all the difference in the world. Not to mention that this particular section of the book is giving an example of how to write the code *without* using nested functions.

Re: why is this not working? (nested scope question)

2006-07-27 Thread John Salerno
[EMAIL PROTECTED] wrote: Ouch! You got me there, I did not copy the code properly. Now I feel stupid. Thanks for the enlightment. I think I am starting to get it. P.S. The point of the example was to show how nesting isn't necessary much of the time. The authors wanted to show that it is

why is this not working? (nested scope question)

2006-07-26 Thread biner . sebastien
I have a problem understanding the scope of variable in nested function. I think I got it nailed to the following example copied from Learning Python 2nd edition page 205. Here is the code. def f1() : x=88 f2() def f2() : print 'x=',x f1() that returns an error saying that NameError:

Re: why is this not working? (nested scope question)

2006-07-26 Thread Tim Chase
[EMAIL PROTECTED] wrote: I have a problem understanding the scope of variable in nested function. I think I got it nailed to the following example copied from Learning Python 2nd edition page 205. Here is the code. def f1() : x=88 f2() def f2() : print 'x=',x f1() that

Re: why is this not working? (nested scope question)

2006-07-26 Thread Peter Otten
[EMAIL PROTECTED] wrote: I have a problem understanding the scope of variable in nested function. I think I got it nailed to the following example copied from Learning Python 2nd edition page 205. Here is the code. def f1() : x=88 f2() def f2() : print 'x=',x f1() that

Re: why is this not working? (nested scope question)

2006-07-26 Thread bryanjugglercryptographer
[EMAIL PROTECTED] wrote: [...] def f1() : x=88 f2() def f2() : print 'x=',x f1() that returns an error saying that NameError: global name 'x' is not defined. I expected f2 to see the value of x defined in f1 since it is nested at runtime. Ah, no, Python uses static scoping.

Re: why is this not working? (nested scope question)

2006-07-26 Thread John Salerno
[EMAIL PROTECTED] wrote: I do understand (and verified) that if I define f2 within f1, it works as expected. But in the learning pyton 2nd edition at page 205 it is said that Programs are much simpler if you do not nest defs within defs (juste before the code mentioned in my initial message).