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


Groups > comp.lang.python > #34095

Re: amazing scope?

Path csiph.com!usenet.pasdenom.info!aioe.org!news.stack.nl!newsfeed.xs4all.nl!newsfeed3.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail
Return-Path <prvs=674d5ed43=jeanmichel@sequans.com>
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; '__name__': 0.07; 'defines': 0.07; 'python': 0.09; "'w')": 0.09; 'explanation': 0.09; 'script,': 0.09; 'wrong,': 0.09; 'bug': 0.10; 'def': 0.10; '2.7': 0.13; 'to:name:python-list': 0.15; "'__main__':": 0.16; 'out"': 0.16; 'refactored': 0.16; 'scope.': 0.16; 'somehow,': 0.16; 'variable.': 0.16; 'module': 0.19; 'define': 0.20; 'to:2**1': 0.23; 'header:In-Reply-To:1': 0.25; 'looks': 0.26; 'wrote': 0.26; 'block,': 0.29; 'declared': 0.29; 'identifies': 0.29; 'url:mailman': 0.29; 'url:python': 0.32; '-----': 0.32; 'url:listinfo': 0.32; 'to:addr:python-list': 0.33; 'languages': 0.33; 'subject:?': 0.35; 'really': 0.36; 'but': 0.36; 'url:org': 0.36; 'level.': 0.36; "didn't": 0.36; 'thank': 0.36; 'does': 0.37; 'level': 0.37; 'subject:: ': 0.38; 'store': 0.38; 'some': 0.38; 'nothing': 0.38; 'url:docs': 0.38; 'to:addr:python.org': 0.39; 'where': 0.40; 'url:mail': 0.40; 'you.': 0.61; 'received:194': 0.61; 'information': 0.63; 'within': 0.64; 'limit': 0.65; 'person,': 0.65; 'disclose': 0.69; 'below:': 0.71; 'notice:': 0.71; 'privileged.': 0.72; 'shocking': 0.84; 'url:reference': 0.84; 'working,': 0.84; 'medium.': 0.91
X-IronPort-AV E=Sophos;i="4.83,347,1352070000"; d="scan'208";a="950700"
X-Virus-Scanned amavisd-new at zimbra.sequans.com
Date Fri, 30 Nov 2012 12:24:59 +0100 (CET)
From Jean-Michel Pichavant <jeanmichel@sequans.com>
To andrea crotti <andrea.crotti.0@gmail.com>, python-list <python-list@python.org>
In-Reply-To <CAF_E5JZ2infhbG+ZQVw7GTeSQMHH=6H06f3hsgSyfydVaqopsg@mail.gmail.com>
Subject Re: amazing scope?
MIME-Version 1.0
X-Mailer Zimbra 7.2.0_GA_2669 (ZimbraWebClient - GC7 (Linux)/7.2.0_GA_2669)
Content-Type text/plain; charset="utf-8"
Content-Transfer-Encoding base64
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.15
Precedence list
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.382.1354274740.29569.python-list@python.org> (permalink)
Lines 28
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1354274740 news.xs4all.nl 6919 [2001:888:2000:d::a6]:39569
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:34095

Show key headers only | View raw


----- Original Message -----
> I wrote a script, refactored it and then introducing a bug as below:
> 
> def record_things():
>     out.write("Hello world")
> 
> if __name__ == '__main__':
>     with open('output', 'w') as out:
>         record_things()
> 
> 
> but the shocking thing is that it didn't actually stopped working, it
> still works perfectly!
> 
> What my explanation might be is that the "out" is declared at module
> level somehow,
> but that's not really intuitive and looks wrong, and works both on
> Python 2.7 and 3.2..
> --
> http://mail.python.org/mailman/listinfo/python-list
> 

From what I understand from the with documentation http://docs.python.org/2/reference/compound_stmts.html#with

The with block has nothing to do with scopes. It does not define the target within that block, well, actually it does but it defines it for the current scope.
The with block only identifies where to call __enter__ and __exit__.

So I would say that "with open('output', 'w') as out" assignes "out" at the module level.

You may have been mislead by some other languages where a with block purpose is to limit the scope of a variable.

JM



-- IMPORTANT NOTICE: 

The contents of this email and any attachments are confidential and may also be privileged. If you are not the intended recipient, please notify the sender immediately and do not disclose the contents to any other person, use it for any purpose, or store or copy the information in any medium. Thank you.

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


Thread

Re: amazing scope? Jean-Michel Pichavant <jeanmichel@sequans.com> - 2012-11-30 12:24 +0100

csiph-web