473,385 Members | 1,907 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Lexical Scope

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
Jul 18 '05 #1
3 2283
Hi,

Matt Knepley wrote:

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?


Yes (short answer)
If you really need to modify some variable in an outer scope you can
use a mutable object for that kind of thing, like so:
def run(): .... a = [1]
.... def run2(b):
.... print a[0]
.... a[0] = b
.... run2(2)
.... print a[0]
.... run() 1
2

Or you can use a global variable like so:
def run(): .... global a
.... a = 1
.... def run2(b):
.... global a
.... print a
.... a = b
.... run2(2)
.... print a
.... run()

1
2
It depends on what you are trying to accomplish :-)
hth

Werner

Jul 18 '05 #2
On Thursday 30 October 2003 07:59 am, Matt Knepley wrote:
I must be misunderstanding how Python 2.3 handles lexical scoping.
Here is a sample piece of code:
The rule is this simple:

An assignment to a variable *ANYWHERE* within a block of code makes
that variable local *EVERYWHERE* within that block, possibly hiding
variables of the same name in outer scopes.

That rule will explain all three of your examples.

Gary Herron

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


Jul 18 '05 #3
From the language lawyers section of python doc.s :-

"If a name is assigned to anywhere in a code block (even in
unreachable code), and is not mentioned in a global statement in that
code block, then it refers to a local name throughout that code
block."

Try:-

def run():
a = 1
def run2(b):
global a
print a
a = b
run2(2)
print a
run()
Jul 18 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Collin VanDyck | last post by:
Hello! I have set up a pluggable SAX transformation pipeline which is made up of individual nodes that transform the source XML, and for the most part it works well. Each node in the pipeline...
4
by: bariole | last post by:
Hi I am trying to make lexical analysis of some simplified html code with flex tool. However that kind of work is new to me and I don't know where to start. I have searched a web but I didn't...
13
by: Lucas Zimmerman | last post by:
Is there any Lex code available that describes how to scan C programs? I'd like to read someting related to this. One of my doubs is how C deals with ambiguities, for example, `a = x/*p;' or `a =...
18
by: jslowery | last post by:
I am not completely knowledgable about the status of lexical scoping in Python, but it was my understanding that this was added in a long time ago around python2.1-python2.2 I am using python2.4...
2
by: Frank-René Schäfer | last post by:
penSource Project 'Quex': http://quex.sf.net Last weekend, the lexical analyser generator 'Quex' has been released on SourceForge. Quex provides advanced features for mode definitions and...
6
by: enaeher | last post by:
I would expect this code: globalFnArray = ; for (var i = 0; i < 5; i++) { globalFnArray.push (function () { alert (i) }); } for (var j = 0; j < 5; j++) { globalFnArray(); } to alert 0, 1, 2,...
14
by: Khookie | last post by:
Woah... is it just me or do C programmers don't bother talking about how cool C can be (compared to Lisp, Haskell, etc.) - functionally speaking? // Lexical scoping - via nested functions...
3
by: globalrev | last post by:
i cant figure outif python has lexical or general scope. it seems functions have lexical scope but with some restrictions and some non-function scopes are dynamic?
9
by: mrstevegross | last post by:
I ran into a weird behavior with lexical scope in Python. I'm hoping someone on this forum can explain it to me. Here's the situation: I have an Outer class. In the Outer class, I define a...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.