Path: csiph.com!usenet.pasdenom.info!dedibox.gegeweb.org!gegeweb.eu!nntpfeed.proxad.net!proxad.net!feeder1-2.proxad.net!news.tele.dk!news.tele.dk!small.news.tele.dk!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.017 X-Spam-Evidence: '*H*': 0.97; '*S*': 0.00; 'definitions': 0.07; 'executed': 0.07; 'indicated': 0.07; 'cc:addr:python-list': 0.10; 'anyway': 0.11; '(usually)': 0.16; 'assignment.': 0.16; 'assigns': 0.16; 'guilty': 0.16; 'namespace.': 0.16; 'scope.': 0.16; 'statement.': 0.16; 'wrote:': 0.17; 'variables': 0.17; 'creates': 0.18; 'module': 0.19; 'bit': 0.21; 'fine,': 0.22; 'object.': 0.22; 'defined': 0.22; 'references': 0.23; 'cc:no real name:2**0': 0.24; 'cc:2**1': 0.24; 'cc:addr:python.org': 0.25; 'header:In-Reply- To:1': 0.25; 'header:User-Agent:1': 0.26; 'looks': 0.26; 'am,': 0.27; 'guess': 0.27; 'execution': 0.27; 'module.': 0.27; 'object,': 0.27; 'post': 0.28; 'definition': 0.29; 'source': 0.29; 'function': 0.30; 'stuff': 0.30; 'code': 0.31; 'subject:?': 0.35; 'something': 0.35; 'but': 0.36; 'level.': 0.36; 'does': 0.37; 'being': 0.37; 'subject:: ': 0.38; 'object': 0.38; 'received:192': 0.39; 'received:192.168': 0.40; 'your': 0.60; 'most': 0.61; 'you.': 0.61; 'first': 0.61; 'within': 0.64; 'header:Reply-To:1': 0.68; 'obvious': 0.71; 'received:74.208': 0.71; 'reply-to:no real name:2**0': 0.72; 'andrea': 0.84; 'guessed': 0.84; 'received:74.208.4.194': 0.84; 'suprise': 0.84; 'valid,': 0.84 Date: Fri, 30 Nov 2012 17:57:44 -0500 From: Dave Angel User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:16.0) Gecko/20121011 Thunderbird/16.0.1 MIME-Version: 1.0 To: andrea crotti Subject: Re: amazing scope? References: In-Reply-To: Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit X-Provags-ID: V02:K0:5GqncaGbzhKYYWO2e2fXv8Y2uVsRfq5balGWzshsbFW JoJwIuJYBB2nS8elqxnmKZi558oAT/BjFjX9p1pDQ8WgrFn/+b v5RXoGFS+Q4f4/a+80y7nKkA8j9VwXcdcLyeua+SBSZc1YVgAw vEPBwffwcd2qsjqXDWLuM1x5l9HM/jUJz9kjlZgtyr7pX98Wtr 9+zGarPUURCRsUk4XIhf68GrFlxWWQ+s3cOIn23bQZurXBcIhc UGVEYw9DLcCy4kvxS5adbPJB9rmG/9ZVy1jNghVbotcic1ppuS 3TNagOKZEdIxlwxssNfgD+7JaYP0iyA6j7M9ayui2mO7vKXcA= = Cc: python-list@python.org, Ulrich Eckhardt X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list Reply-To: d@davea.name 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: 35 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1354316296 news.xs4all.nl 6921 [2001:888:2000:d::a6]:34236 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:34113 On 11/30/2012 11:05 AM, andrea crotti wrote: > Well I knew that this works fine, even if I feel a bit guilty to do > this, and better is: > > foo = 'bar' if some_condition else 'baz' > > Anyway for me the suprise is that something that is defined *later* at > the module scope is found in a function which is defined *earlier*. It would have been nice if you had indicated in your original post just *what* you considered "shocking" about the code sample. The code was valid, and we all had to guess what about it was puzzling you. Like most others, I first guessed you were confused about the definition being in an if statement. Then I thought you were confused by the with statement, since it's not as obvious that "as" binds the name to the object in the same way as assignment. But I never would have guessed that you were confused about the order of execution in a module. The source code in a module is executed in order, including function definitions defined at top level. However, such a function definition's execution creates a function object, and assigns a global name (usually) to that object. It does not "execute" the object. When that function is executed later, it then looks for global variables for stuff that's not defined within its own scope. So by the time the function references the 'out' name, it's been successfully added to the global namespace. -- DaveA