By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
444,199 Members | 1,064 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 444,199 IT Pros & Developers. It's quick & easy.

Understanding closures

P: n/a
Hi,

I want to use variables passed to a function in an inner defined
function. Something like-

def fun1(method=None):
def fun2():
if not method: method = 'GET'
print '%s: this is fun2' % method
return
fun2()

fun1()

However I get this error-
UnboundLocalError: local variable 'method' referenced before
assignment

This however works fine.

def fun1(method=None):
if not method: method = 'GET'
def fun2():
print '%s: this is fun2' % method
return
fun2()

fun1()

Is there a simple way I can pass on the variables passed to the outer
function to the inner one without having to use or refer them in the
outer function?

Thanks,
Ramashish

Aug 19 '07 #1
Share this Question
Share on Google+
3 Replies


P: n/a
Ramashish Baranwal <ra*************@gmail.comwrote:
Hi,

I want to use variables passed to a function in an inner defined
function. Something like-

def fun1(method=None):
def fun2():
if not method: method = 'GET'
print '%s: this is fun2' % method
return
fun2()

fun1()

However I get this error-
UnboundLocalError: local variable 'method' referenced before
assignment

This however works fine.

def fun1(method=None):
if not method: method = 'GET'
def fun2():
print '%s: this is fun2' % method
return
fun2()

fun1()

Is there a simple way I can pass on the variables passed to the outer
function to the inner one without having to use or refer them in the
outer function?
Sure, just don't ASSIGN TO those names in the inner function. Any name
ASSIGNED TO in a given function is local to that specific function (save
for global statements, which bypass variable of containing functions
anyway).
Alex
Aug 19 '07 #2

P: n/a
On Aug 18, 11:03 pm, Ramashish Baranwal <ramashish.li...@gmail.com>
wrote:
Hi,

I want to use variables passed to a function in an inner defined
function. Something like-

def fun1(method=None):
def fun2():
if not method: method = 'GET'
print '%s: this is fun2' % method
return
fun2()

fun1()

However I get this error-
UnboundLocalError: local variable 'method' referenced before
assignment

This however works fine.

def fun1(method=None):
if not method: method = 'GET'
def fun2():
print '%s: this is fun2' % method
return
fun2()

fun1()

Is there a simple way I can pass on the variables passed to the outer
function to the inner one without having to use or refer them in the
outer function?

Thanks,
Ramashish
This works error free:
def fun1(x=None):
y = 10
def fun2():
print x
if not x: method = 'GET'
print '%s: this is fun2' % method
fun2()
fun1()
Python reads x from the fun1 scope. But the following produces an
error for the print x line:

def fun1(x=None):
y = 10
def fun2():
print x
if not x: x = 'GET' #CHANGED THIS LINE
print '%s: this is fun2' % method
fun2()
fun1()

Traceback (most recent call last):
File "3pythontest.py", line 8, in ?
fun1()
File "3pythontest.py", line 7, in fun1
fun2()
File "3pythontest.py", line 4, in fun2
print x
UnboundLocalError: local variable 'x' referenced before assignment
It's as if python sees the assignment to x and suddenly decides that
the x it was reading from the fun1 scope is the wrong x. Apparently,
the assignment to x serves to insert x in the local scope, which makes
the reference to x a few lines earlier an error.

Aug 19 '07 #3

P: n/a
Ramashish Baranwal wrote:
Hi,

I want to use variables passed to a function in an inner defined
function. Something like-

def fun1(method=None):
def fun2():
if not method: method = 'GET'
print '%s: this is fun2' % method
return
fun2()

fun1()

However I get this error-
UnboundLocalError: local variable 'method' referenced before
assignment

This however works fine.

def fun1(method=None):
if not method: method = 'GET'
def fun2():
print '%s: this is fun2' % method
return
fun2()

fun1()

Is there a simple way I can pass on the variables passed to the outer
function to the inner one without having to use or refer them in the
outer function?
Why do you have this latter requirement? Will /method/ change in the
outer scope before it is called in the inner scope? E.g.:
def fun1(method=None):
def fun2():
if not method: method = 'GET'
print '%s: this is fun2' % method
return
method = 'POST'
# Now its going to be 'POST' in fun2
fun2()
To avoid this problem, you want something like this:
def fun1(method=None):
def fun2(method=method):
if not method: method = 'GET'
print '%s: this is fun2' % method
return
method = 'POST'
# Now the preceding line has no effect on fun2
fun2()
# Now method is explicitly 'POST' in fun2
fun2('POST')
But a deeper question is: Why do you need to define fun2 with fun1 to
begin with?
def fun2(method=None):
if not method: method = 'GET'
print '%s: this is fun2' % method
return

def fun1(method=None):
method = 'POST'
# will be 'GET'
fun2()
# will be 'POST'
fun2(method)
Also, empty "return" statements are unnecessary if they are the last
statement in a function.

James
Aug 19 '07 #4

This discussion thread is closed

Replies have been disabled for this discussion.