strange behaivor of nested function

1

1989lzhh

here is code

def make():
def jit(sig):
def wrap(function):
sig=sig[0] # unbound local error, if change to sig='' would be just fine
return function
return wrap
return jit
jit=make()
@jit('')
def f():
pass

It is strange that the interpreter complain about unbound local error.
please give me some suggestion, thanks!
Ps: I am using python 2.7
Liu Zhenhai




·¢×ÔÎÒµÄ iPhone
 
S

Steven D'Aprano

here is code

def make():
def jit(sig):
def wrap(function):
sig=sig[0] # unbound local error

You are saying:

"sig is a local variable. Assign sig to the value of sig[0]."

But what is sig? You've said it is a local variable, and at this point it
doesn't have a value yet.

Lua allows you to do this:

sig = sig[0]

will look up a global sig and assign it to the local sig first, but that
can be confusing and Python doesn't do that.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,756
Messages
2,569,535
Members
45,008
Latest member
obedient dusk

Latest Threads

Top