Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #72910 > unrolled thread

strange behaivor of nested function

Started by1989lzhh <1989lzhh@gmail.com>
First post2014-06-07 19:17 +0800
Last post2014-06-07 15:06 +0000
Articles 2 — 2 participants

Back to article view | Back to comp.lang.python


Contents

  strange behaivor of nested function 1989lzhh <1989lzhh@gmail.com> - 2014-06-07 19:17 +0800
    Re: strange behaivor of nested function Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2014-06-07 15:06 +0000

#72910 — strange behaivor of nested function

From1989lzhh <1989lzhh@gmail.com>
Date2014-06-07 19:17 +0800
Subjectstrange behaivor of nested function
Message-ID<mailman.10849.1402140210.18130.python-list@python.org>
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

[toc] | [next] | [standalone]


#72913

FromSteven D'Aprano <steve+comp.lang.python@pearwood.info>
Date2014-06-07 15:06 +0000
Message-ID<53932a75$0$29988$c3e8da3$5496439d@news.astraweb.com>
In reply to#72910
On Sat, 07 Jun 2014 19:17:23 +0800, 1989lzhh wrote:

> 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.



-- 
Steven D'Aprano
http://import-that.dreamwidth.org/

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web