Path: csiph.com!usenet.pasdenom.info!weretis.net!feeder4.news.weretis.net!ecngs!feeder2.ecngs.de!newsfeed.freenet.ag!news2.euro.net!newsfeed.xs4all.nl!newsfeed6.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.000 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'example:': 0.03; 'value,': 0.03; 'initialize': 0.05; '*args,': 0.07; 'function,': 0.07; 'python': 0.09; '**kwargs)': 0.09; '**kwargs):': 0.09; 'argument,': 0.09; 'func': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'shortcut': 0.09; 'def': 0.10; 'aim:': 0.16; 'macro,': 0.16; 'received:80.91.229.3': 0.16; 'received:dip.t-dialin.net': 0.16; 'received:plane.gmane.org': 0.16; 'received:t-dialin.net': 0.16; 'scopes': 0.16; 'variable.': 0.16; 'wrote:': 0.17; '>>>': 0.18; 'code,': 0.18; 'variable': 0.20; 'not,': 0.21; 'explicit': 0.22; 'tells': 0.22; 'defined': 0.22; 'thus': 0.24; 'header:User-Agent:1': 0.26; 'question': 0.27; 'header:X-Complaints-To:1': 0.28; 'run': 0.28; 'wrap': 0.29; "skip:' 10": 0.30; 'function': 0.30; 'error': 0.30; 'print': 0.32; 'function.': 0.33; 'to:addr:python-list': 0.33; 'code:': 0.33; 'hi,': 0.33; 'there': 0.35; 'received:org': 0.36; 'but': 0.36; 'test': 0.36; 'should': 0.36; 'turn': 0.36; 'execute': 0.37; 'does': 0.37; 'subject:: ': 0.38; 'some': 0.38; 'shows': 0.38; 'to:addr:python.org': 0.39; 'skip:" 10': 0.40; 'header:Received:5': 0.40 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org From: Peter Otten <__peter__@web.de> Subject: Re: confused in decorate and closure Date: Thu, 13 Sep 2012 08:32:38 +0200 Organization: None References: Mime-Version: 1.0 Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: 8Bit X-Gmane-NNTP-Posting-Host: p50849bb7.dip.t-dialin.net User-Agent: KNode/4.7.3 X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 64 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1347517951 news.xs4all.nl 6976 [2001:888:2000:d::a6]:57022 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:29016 月忧茗 wrote: > HI, I have some test code: > > > def num(num): > def deco(func): > def wrap(*args, **kwargs): > inputed_num = num > return func(*args, **kwargs) > return wrap > return deco > > > @num(5) > def test(a): > return a + inputed_num > > print test(1) > > > when run this code, I got an error shows that 'inputed_num' is not > defined > > My question is: > In wrap function, is there not a closure that func can got 'inputed_num' > ? > > > > Anyway, If not, how should I do to got my aim: Initialize some value, > and use this value directly in the main function. Variable scopes are determined statically. In > def test(a): > return a + inputed_num "inputed_num" is a global variable. > @num(5) is not a macro, but a shortcut that tells Python to execute test = num(5)(test) and thus does not change the scopes. To get the desired effect you have to turn "inputed_num" into an explicit function argument, for example: >>> def num(n): ... def deco(f): ... def wrap(*args, **kw): ... return f(n, *args, **kw) ... return wrap ... return deco ... >>> @num(42) ... def test(n, a): ... return n + a ... >>> test(1) 43