Path: csiph.com!usenet.pasdenom.info!news.albasani.net!rt.uk.eu.org!newsfeed.xs4all.nl!newsfeed4a.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!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.010 X-Spam-Evidence: '*H*': 0.98; '*S*': 0.00; 'argument': 0.05; 'cpython': 0.05; 'variables': 0.07; 'builtin': 0.09; 'exist,': 0.09; 'sanity': 0.09; 'python': 0.11; 'def': 0.12; 'created.': 0.16; 'from:addr:mrabarnett.plus.com': 0.16; 'from:addr:python': 0.16; 'from:name:mrab': 0.16; 'message-id:@mrabarnett.plus.com': 0.16; 'received:192.168.1.4': 0.16; 'received:84.93': 0.16; 'received:84.93.230': 0.16; 'exception': 0.16; 'wrote:': 0.18; 'variable': 0.18; 'module': 0.19; 'example': 0.22; 'python?': 0.22; 'header:User-Agent:1': 0.23; "haven't": 0.24; 'somewhere': 0.26; 'header:In-Reply-To:1': 0.27; 'function': 0.29; 'correct': 0.29; 'chris': 0.29; 'am,': 0.29; 'raise': 0.29; '(although': 0.31; 'slot': 0.31; 'spam,': 0.31; 'run': 0.32; 'problem': 0.35; 'agree': 0.35; 'except': 0.35; 'knows': 0.35; 'no,': 0.35; 'received:84': 0.35; 'but': 0.35; 'there': 0.35; 'to:addr:python- list': 0.38; 'fact': 0.38; 'does': 0.39; 'to:addr:python.org': 0.39; 'skip:u 10': 0.60; 'is.': 0.60; 'tell': 0.60; 'simple': 0.61; "you're": 0.61; 'name': 0.63; 'more': 0.64; 'different': 0.65; 'compliant': 0.68; 'detail.': 0.68; 'sound': 0.68; 'behavior': 0.77; 'clearer': 0.84; 'ethan': 0.84; 'furman': 0.84 X-CM-Score: 0.00 X-CNFS-Analysis: v=2.1 cv=MtHc6gqe c=1 sm=1 tr=0 a=0nF1XD0wxitMEM03M9B4ZQ==:117 a=0nF1XD0wxitMEM03M9B4ZQ==:17 a=0Bzu9jTXAAAA:8 a=1oDRhzKAKukA:10 a=ihvODaAuJD4A:10 a=IkcTkHD0fZMA:10 a=EBOSESyhAAAA:8 a=d1lqOtu9ocdXOQESPdAA:9 a=QEXdDO2ut3YA:10 X-AUTH: mrabarnett:2500 Date: Sat, 10 May 2014 20:22:12 +0100 From: MRAB User-Agent: Mozilla/5.0 (Windows NT 6.3; WOW64; rv:24.0) Gecko/20100101 Thunderbird/24.5.0 MIME-Version: 1.0 To: python-list@python.org Subject: Re: Values and objects References: <235C4BFA-9770-481A-9FCF-21C3F036769C@gmail.com> <5368681D.8070602@islandtraining.com> <87ppjpwafk.fsf@elektro.pacujo.net> <536ad8f2$0$29965$c3e8da3$5496439d@news.astraweb.com> <87zjiqbmy5.fsf@elektro.pacujo.net> <536d7a7d$0$29980$c3e8da3$5496439d@news.astraweb.com> <9cc0ebf9-dbed-4d3d-91fc-2abb9b0103d0@googlegroups.com> <536dc3f7$0$29980$c3e8da3$5496439d@news.astraweb.com> <536decca$0$29980$c3e8da3$5496439d@news.astraweb.com> <536E799D.6080602@stoneleaf.us> In-Reply-To: <536E799D.6080602@stoneleaf.us> Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit 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: 36 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1399749735 news.xs4all.nl 2913 [2001:888:2000:d::a6]:35102 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:71265 On 2014-05-10 20:10, Ethan Furman wrote: > On 05/10/2014 02:32 AM, Chris Angelico wrote: >> >> Tell me, what may this function do in a compliant Python? >> >> def demo(): >> ret = spam >> spam = 23 >> return ret >> >> In CPython, that'll raise UnboundLocalError, because the local >> variable 'spam' does already exist, and currently has no value (no >> object bound to it). > > No, it does not exist -- or, more accurately, it does not exist *yet* but will. The fact that there is a slot waiting > for what will be spam is a cpython implementation detail. > > And if you don't like that argument (although it is a perfectly sound and correct argument), think of the module name space: > > ret = spam > spam = 23 > > will net you a simple NameError, because spam has not yet been created. > > >> If a compliant Python implementation is allowed >> to have this return the value of a global or builtin spam, then I >> would agree that you can create variables at run time. > > See module example above. This behavior is not allowed in functions for scope and sanity (mostly sanity) reasons. > UnboundLocalError is like NameError, except that Python knows that the name is local because somewhere in the function you're binding to that name and you haven't said that it's global or nonlocal. Having a different exception for that case makes it clearer to the user what the problem is.