Path: csiph.com!usenet.pasdenom.info!aioe.org!news.stack.nl!newsfeed.xs4all.nl!newsfeed4.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.008 X-Spam-Evidence: '*H*': 0.98; '*S*': 0.00; '16,': 0.03; 'assignment': 0.07; 'attribute': 0.07; 'subject:bug': 0.07; 'arguments': 0.09; 'function,': 0.09; 'function:': 0.09; 'def': 0.12; '"in': 0.16; 'assigns': 0.16; 'complete;': 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'scope.': 0.16; 'scopes': 0.16; 'wrote:': 0.18; 'work,': 0.20; 'creating': 0.23; 'header:In-Reply- To:1': 0.27; 'am,': 0.29; 'message-id:@mail.gmail.com': 0.30; 'url:mailman': 0.30; 'code': 0.31; 'class': 0.32; 'supposed': 0.32; 'url:python': 0.33; 'received:google.com': 0.35; 'url:listinfo': 0.36; 'useful': 0.36; 'possible': 0.36; 'subject:?': 0.36; 'url:org': 0.36; 'sometimes': 0.38; 'to:addr :python-list': 0.38; 'to:addr:python.org': 0.39; 'url:mail': 0.40; 'skip:u 10': 0.60; 'then,': 0.60; "you're": 0.61; 'name': 0.63; 'skip:n 10': 0.64; 'due': 0.66; 'default': 0.69; 'jul': 0.74; 'subject:this': 0.83; '2013': 0.98 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:to :content-type; bh=srtETO5FrUfhujUAnVbX7BIDpx1Q9/V2wV0ivWY0K3Q=; b=WJ+y/z0pAWb5OlmKkB/GlHMLOJJUaNT9mtLXfStzx15b//7gRri/6k5YrqzETD4NIB BuDJnkNwMso2zXZ1Km8f8wXB0rei4XrPuUnsqtgzJsKQiUZxidPQ0cJk9FMali99PVE4 sU781EgqAqrmKRnSCCc2nzsYXC0MascwV3eWYx+N/eVSKQjaS6/VdaE0auafhuVENrMC 8FE0UimcVgP9fvkac1YC1z3TGkf9oykDF8fgn4qYby+YfW7A89H0TOe5oM1VmQBUdqqU 9vB5p+L5waC1EoiIxKvsca8E1U157mdZ/OinNCOkoHpHJIQARvsqeoiDBq/NuHj9fZng 50vQ== MIME-Version: 1.0 X-Received: by 10.52.16.105 with SMTP id f9mr23841365vdd.28.1373904359434; Mon, 15 Jul 2013 09:05:59 -0700 (PDT) In-Reply-To: <51E41A5C.7060903@nottheoilrig.com> References: <51E41A5C.7060903@nottheoilrig.com> Date: Tue, 16 Jul 2013 02:05:59 +1000 Subject: Re: Is this a bug? From: Chris Angelico To: python-list@python.org Content-Type: text/plain; charset=ISO-8859-1 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: 29 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1373904362 news.xs4all.nl 15913 [2001:888:2000:d::a6]:38059 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:50696 On Tue, Jul 16, 2013 at 1:50 AM, 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 > -- > http://mail.python.org/mailman/listinfo/python-list Unless you're creating that class inside a function, it would be NameError, not UnboundLocalError. Due to the way class scopes work, it's actually possible and sometimes useful to do this, as it "snapshots" the current referent of that name. It's like what happens with default arguments to a function: foo = 'bar' def func(foo=foo): return foo foo = 'quux' print(func()) The newly-defined name isn't "in scope" until its assignment is complete; until then, the old name is fully in scope. ChrisA