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
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
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
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
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:
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
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
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
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')
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'
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
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
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
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 . .
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
[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
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
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
=
=
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
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
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
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,
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 +
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
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
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
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
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
Thanks a lot for clearing this up, Diez!
-Matthias
--
http://mail.python.org/mailman/listinfo/python-list
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
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(',')
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 =
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']:
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.
[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
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:
[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
[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
[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.
[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).
40 matches
Mail list logo