Path: csiph.com!newsfeed.hal-mli.net!feeder3.hal-mli.net!newsfeed.hal-mli.net!feeder1.hal-mli.net!newsfeed.xs4all.nl!newsfeed1.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.003 X-Spam-Evidence: '*H*': 0.99; '*S*': 0.00; 'python.': 0.02; 'attribute': 0.07; 'subject:bug': 0.07; 'function,': 0.09; 'cc:addr:python-list': 0.11; 'python': 0.11; 'def': 0.12; 'assume': 0.14; 'useful,': 0.14; 'changes': 0.15; 'assignment.': 0.16; 'assignments': 0.16; 'assigns': 0.16; 'behaviour.': 0.16; 'foo"': 0.16; 'jumping': 0.16; 'lhs': 0.16; 'saying.': 0.16; 'unhelpful': 0.16; 'weird': 0.16; 'sender:addr:gmail.com': 0.17; 'wrote:': 0.18; 'variable': 0.18; 'later': 0.20; 'saying': 0.22; 'cc:addr:python.org': 0.22; 'error': 0.23; 'refers': 0.24; 'fine': 0.24; 'cc:2**0': 0.24; 'header:In-Reply-To:1': 0.27; 'testing': 0.29; 'skip:- 40': 0.29; 'wondering': 0.29; 'external': 0.29; 'properties': 0.29; 'raise': 0.29; 'message-id:@mail.gmail.com': 0.30; "i'm": 0.30; 'code': 0.31; 'getting': 0.31; 'too.': 0.31; 'dramatic': 0.31; 'yes.': 0.31; 'class': 0.32; 'languages': 0.32; 'stuff': 0.32; 'supposed': 0.32; 'skip:- 30': 0.32; 'knows': 0.35; 'something': 0.35; 'but': 0.35; 'received:google.com': 0.35; 'version': 0.36; 'consistent': 0.36; 'doing': 0.36; 'subject:?': 0.36; 'should': 0.36; 'so,': 0.37; 'too': 0.37; 'two': 0.37; 'being': 0.38; 'that,': 0.38; 'does': 0.39; 'sure': 0.39; 'changed': 0.39; 'skip:u 10': 0.60; 'no.': 0.61; "you're": 0.61; 'july': 0.63; 'refer': 0.63; 'different': 0.65; 'special': 0.74; 'subject:this': 0.83; 'conclusions': 0.84; 'local,': 0.84; 'responses': 0.93; '2013': 0.98 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:sender:in-reply-to:references:from:date :x-google-sender-auth:message-id:subject:to:cc:content-type; bh=gmrGitjNrVLPGQREaCq+eicAVAAF4cPrYMhbmosNUaY=; b=ouc7F1VmQ9tNJAPDo0ExcvF5wNMgBEaLuWr9bKfHCBWtvjAci1T6GrdCuQhybV6YDo Pi1tBXkXudH3pK5XYrKVc8KzIfuwy1VadFyKOYAEDROhN2uk028WARrgWtoJegRdhYAU TFl7Q18a0x9QRtE9DgwLPhBItRYgscxGP29gI5wiR2n/v2rehzaNcOlkUNqq7UoUuuMb TOnCyUsokDJJ8bUBRBEdGRmBtfL1Kr/bSN6hgfxKKbixWCEIoy2jc1abB2kKGfQzed7P ykRxUXFDXHmygMBd6khzdfYkWXRw3ZUIPxWfEHUTNSbv/+QuSfIgSSrbB3HJ39KSHrUc x/Ow== X-Received: by 10.152.45.65 with SMTP id k1mr25087747lam.78.1373904832117; Mon, 15 Jul 2013 09:13:52 -0700 (PDT) MIME-Version: 1.0 Sender: joshua.landau.ws@gmail.com In-Reply-To: <51E41A5C.7060903@nottheoilrig.com> References: <51E41A5C.7060903@nottheoilrig.com> From: Joshua Landau Date: Mon, 15 Jul 2013 17:13:12 +0100 X-Google-Sender-Auth: 31gfZd05GWwftA_sGM6zK_hfmDQ Subject: Re: Is this a bug? To: Jack Bates Content-Type: text/plain; charset=UTF-8 Cc: python-list 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: 58 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1373904834 news.xs4all.nl 15882 [2001:888:2000:d::a6]:45875 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:50698 On 15 July 2013 16:50, Jack Bates wrote: > Hello, > > Is the following code supposed to be an UnboundLocalError? > Currently it assigns the value 'bar' to the attribute baz.foo > > foo = 'bar' > class baz: > foo = foo I have two responses because I'm not sure what you're saying. Take your pick. ---------------------------------- If you're getting an error from that, then yes. The code works fine for me. I assume you're just jumping to conclusions and not testing code and were doing something like: foo = "Outside foo" def this_will_break(): foo foo = "Inside foo" and wondering why it breaks. Is this correct? If so, it's because Python has consistent ideas about whether something is local to its scope -- some other languages will think that foo is nonlocal *until* they see the assignment. Python knows that foo will be assigned later in the function, so is a local variable the whole time. Being local, the outside version refers to *a different name*. If you want them to refer to the same name, use "nonlocal" or "global", depending on circumstance. foo = "Outside foo" def this_will_break(): global foo foo foo = "Still outside foo (outside foo is changed too)" ---------------------------------------- ALTERNATIVELY Are you saying you think that instead of doing what it does it should raise an UnboundLocalError? If so, then no. Assignments inside class bodies are special-cased in Python. This is because all assignments refer to properties of "self" on the LHS but external things too on the RHS. This is why you can do "def x(): ..." instead of "def self.x(): ..." or some other weird thing. There's also some extra special stuff that goes on. In order to make this an UnboundLocalError, lots of dramatic and unhelpful changes would have to take place, hence the current behaviour. The current behaviour is useful, too.