473,386 Members | 1,698 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,386 software developers and data experts.

Modifying a variable in a non-global outer scope?

#! /usr/bin/env python
"""
When I run the following program I get the error message:

UnboundLocalError: local variable 'x' referenced before assignment

Can "inner" change the value of a variable defined in "outer"? Where
is this explained in the docs?
"""
def outer():
def inner():
x = x + 1

x = 3
inner()
print x

outer()
May 19 '06 #1
1 1520
Edward C. Jones wrote:
#! /usr/bin/env python
"""
When I run the following program I get the error message:

UnboundLocalError: local variable 'x' referenced before assignment

Can "inner" change the value of a variable defined in "outer"?
Not this way
Where
is this explained in the docs?
IIRC,
http://www.python.org/doc/2.4.2/ref/naming.html
"""
def outer():
def inner():
x = x + 1

x = 3
inner()
print x

outer()


What are functions arguments and return values for ?

def outer():
def inner(x):
return x+1
x = 3
x = inner(x)
print x

outer()

Using side-effects - specially this way - is a Very Bad Thing(tm). It
makes code that is hard to read and hard to maintain.
--
bruno desthuilliers
python -c "print '@'.join(['.'.join([w[::-1] for w in p.split('.')]) for
p in 'o****@xiludom.gro'.split('@')])"
May 19 '06 #2

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

Similar topics

6
by: Uenal Mutlu | last post by:
Why is f() giving access violation when called from tf1() or tf2() ? Is this a compiler and/or language bug? void f(char* psz) { *psz = 0; } void tf1() {
9
by: Stefan Mueller | last post by:
I'd like to set a variable called 'FocusIsOn' if a button got the focus. Because my button is dynamically created I do it like xelement = document.createElement("input") xelement.type = "button"...
7
by: Jon | last post by:
Is it possible to modify an argument of a va_list and then pass the modified va_list to other functions? void c_fun1(int, va_list args) { int *firstIntPointer = &(va_arg(args, int)); ...
4
by: Frank Rizzo | last post by:
Hello, I want to use a variable as a signal to the thread that it should stop what it's doing (code below). As a rule, should I lock an object only when I am writing to it or do I have to lock...
19
by: Manish Tomar | last post by:
Hi All, The following code as per my knowledge should not work: int* some() { int b = 10; return &b; }
2
by: requeth | last post by:
I have 12 possible error codes that can be placed in an attribute, and multiple attributes with this in an XML file. I am attempting to use an XSLT (which makes the data viewable to non technical...
3
by: Jon Slaughter | last post by:
I'm tryign to display my own title bar. I've created a window without one but with a menu. The menu though is located in the upper left corner and I need to move it down. I need to set the client...
5
by: Amit Bhatia | last post by:
Hi, I was wondering if I have a hash_set, can I modify its elements using an iterator: given the fact that the changes I make will not change the position or the key of the object that the...
24
by: allpervasive | last post by:
hi all, this is reddy, a beginner to c lang,,here i have some problems in reading and modifying the contents of a file,, hope you can help to solve this problem. Here i attach the file to be...
14
by: jehugaleahsa | last post by:
I have a rather complex need. I have a class that parses web pages and extracts all relevant file addresses. It allows me to download every pdf on a web page, for instance. I would like to...
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: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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.