Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #34113

Re: amazing scope?

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 <d@davea.name>
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 <d@davea.name>
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 <andrea.crotti.0@gmail.com>
Subject Re: amazing scope?
References <mailman.380.1354273891.29569.python-list@python.org> <thhmo9-ie6.ln1@satorlaser.homedns.org> <CAF_E5JbeoJ__WTMy-omFPrWP23E=yyeEJ8ABaEJ5jaFP9vFbPw@mail.gmail.com>
In-Reply-To <CAF_E5JbeoJ__WTMy-omFPrWP23E=yyeEJ8ABaEJ5jaFP9vFbPw@mail.gmail.com>
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 <ulrich.eckhardt@dominolaser.com>
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 <python-list.python.org>
List-Unsubscribe <http://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.395.1354316296.29569.python-list@python.org> (permalink)
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

Show key headers only | View raw


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

Back to comp.lang.python | Previous | NextPrevious in thread | Find similar | Unroll thread


Thread

amazing scope? andrea crotti <andrea.crotti.0@gmail.com> - 2012-11-30 11:11 +0000
  Re: amazing scope? Ulrich Eckhardt <ulrich.eckhardt@dominolaser.com> - 2012-11-30 12:23 +0100
    Re: amazing scope? andrea crotti <andrea.crotti.0@gmail.com> - 2012-11-30 16:05 +0000
    Re: amazing scope? Chris Angelico <rosuav@gmail.com> - 2012-12-01 03:08 +1100
    Re: amazing scope? Dave Angel <d@davea.name> - 2012-11-30 17:57 -0500

csiph-web