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


Groups > comp.lang.python > #72911

Re: strange behaivor of nested function

Path csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!eu.feeder.erje.net!newsfeed.xs4all.nl!newsfeed2a.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail
Return-Path <rosuav@gmail.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.001
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; 'anyway.': 0.05; 'interpreter': 0.05; 'assign': 0.07; 'element': 0.07; 'odd': 0.07; 'assigning': 0.09; 'cc:addr:python-list': 0.11; 'python': 0.11; 'def': 0.12; '2.7': 0.14; 'declaration': 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'pulling': 0.16; 'simple.': 0.16; 'subscripting': 0.16; 'unbound': 0.16; 'sat,': 0.16; 'do,': 0.16; 'wrote:': 0.18; 'variable': 0.18; 'seems': 0.21; 'cc:addr:python.org': 0.22; 'fine': 0.24; 'cc:2**0': 0.24; 'pass': 0.26; 'header:In-Reply-To:1': 0.27; 'function': 0.29; "doesn't": 0.30; 'message-id:@mail.gmail.com': 0.30; 'work.': 0.31; 'languages': 0.32; 'thanks!': 0.32; 'another': 0.32; 'quite': 0.32; 'something': 0.35; 'but': 0.35; 'received:google.com': 0.35; 'there': 0.35; 'surely': 0.36; 'error.': 0.37; 'pm,': 0.38; 'rather': 0.38; 'little': 0.38; 'anything': 0.39; 'simply': 0.61; "you're": 0.61; 'first': 0.61; 'making': 0.63; 'name': 0.63; 'to:none': 0.92
DKIM-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:cc :content-type; bh=tBA5rpyzeDQnfhhXjl1IvmUcwaAp5Eg3Mg2pRq2X/so=; b=wsnAdQXm3MPTRqWKWwR0GggmgjJ8q3P+Z5hY9daUAA+Eec6gD2sazXadpCLOlIuCCq 1WcG/g4UHRXK+KgJ7e6tn3avsBFjxdkdHghxC0DBXXVI3aiWL+3tiiZYxm+ErWAl2oka YkwRz3RmBn9F1k/HOHnrV53yJHG4yIsVNSWFzdw8ODcir1szkkJPTvLMgMyw55H9+zvX W0g37uP/sKsKe0yaHbhTRgAERzXsEZmNSPO1ZuZP1l/m4UcnIC/wv4aBoUCBWmW01EQv p/KAJuoSZy8vKqv2do0eOusQMbmRcmG8U8EEt4voeoBQ09wI+aglGep0Ei1m2frWXIJt /nEw==
MIME-Version 1.0
X-Received by 10.221.30.14 with SMTP id sa14mr10509306vcb.44.1402140745600; Sat, 07 Jun 2014 04:32:25 -0700 (PDT)
In-Reply-To <3FDC526D-B755-4C3A-8434-AFB5C7BFDF78@gmail.com>
References <3FDC526D-B755-4C3A-8434-AFB5C7BFDF78@gmail.com>
Date Sat, 7 Jun 2014 21:32:25 +1000
Subject Re: strange behaivor of nested function
From Chris Angelico <rosuav@gmail.com>
Cc python_list <python-list@python.org>
Content-Type text/plain; charset=UTF-8
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.15
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <https://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.10850.1402140748.18130.python-list@python.org> (permalink)
Lines 31
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1402140748 news.xs4all.nl 2917 [2001:888:2000:d::a6]:45070
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:72911

Show key headers only | View raw


On Sat, Jun 7, 2014 at 9:17 PM, 1989lzhh <1989lzhh@gmail.com> wrote:
> 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

It's quite simple. You're assigning to the name 'sig' inside the
function 'wrap', which means that - in the absence of a declaration -
'sig' is a local name. But before you assign anything to it, you first
try to reference it, by subscripting it (sig[0]). Python doesn't have
a rule about pulling something in from another scope at the same time
as making it local (some languages do, and it's rather handy, but it
can only work with variable declarations), so what you're attempting
to do there simply won't work.

But it seems a little odd anyway. Why do you take the first element of
sig every time the inner function is called? Surely you want to do
that just once?

ChrisA

Back to comp.lang.python | Previous | Next | Find similar | Unroll thread


Thread

Re: strange behaivor of nested function Chris Angelico <rosuav@gmail.com> - 2014-06-07 21:32 +1000

csiph-web