Lexical Scope
From: Matt Knepley (knepley_at_mcs.anl.gov)
Date: 10/30/03
- Next message: Skip Montanaro: "Re: replacement of rexec?"
- Previous message: Samir Patel: "Re: plotting and diagramming library"
- Next in thread: Werner Schiendl: "Re: Lexical Scope"
- Reply: Werner Schiendl: "Re: Lexical Scope"
- Reply: Gary Herron: "Re: Lexical Scope"
- Reply: Paul Clinch: "Re: Lexical Scope"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: 30 Oct 2003 07:59:04 -0800
I must be misunderstanding how Python 2.3 handles lexical scoping.
Here is a sample piece of code:
def run():
a = 1
def run2(b):
print a
run2(2)
print a
run()
which gives the output:
1
1
whereas this piece of code:
def run():
a = 1
def run2(b):
a = b
print a
run2(2)
print a
run()
gives:
2
1
and finally this code bombs:
def run():
a = 1
def run2(b):
print a
a = b
run2(2)
print a
run()
with an error about UnboundLocal. It seems that lexical scope works
only for references, and as soon as I make an assignment a new local
is created. Is this true?
Matt
- Next message: Skip Montanaro: "Re: replacement of rexec?"
- Previous message: Samir Patel: "Re: plotting and diagramming library"
- Next in thread: Werner Schiendl: "Re: Lexical Scope"
- Reply: Werner Schiendl: "Re: Lexical Scope"
- Reply: Gary Herron: "Re: Lexical Scope"
- Reply: Paul Clinch: "Re: Lexical Scope"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|