Path: csiph.com!usenet.pasdenom.info!gegeweb.org!de-l.enfer-du-nord.net!feeder1.enfer-du-nord.net!feeds.phibee-telecom.net!newsfeed.xs4all.nl!newsfeed6.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.009 X-Spam-Evidence: '*H*': 0.98; '*S*': 0.00; 'versions,': 0.05; 'ugly': 0.07; 'python': 0.09; 'globals': 0.09; 'overwrite': 0.09; 'pointless': 0.09; 'portable': 0.09; 'restored': 0.09; 'threads.': 0.09; 'variables,': 0.09; 'b=2)': 0.16; 'bit.': 0.16; 'dictionary.': 0.16; 'fail,': 0.16; 'scope,': 0.16; 'unbound': 0.16; 'wrote:': 0.17; 'certainly': 0.17; 'variables': 0.17; 'variable': 0.20; 'define': 0.20; 'fairly': 0.21; 'command': 0.24; 'header:In-Reply-To:1': 0.25; 'header:User-Agent:1': 0.26; 'entries': 0.27; "doesn't": 0.28; 'this?': 0.28; 'dictionary': 0.29; 'exposed': 0.29; 'that.': 0.30; 'could': 0.32; 'print': 0.32; 'to:addr:python-list': 0.33; 'stores': 0.35; 'pm,': 0.35; 'subject:?': 0.35; 'there': 0.35; 'but': 0.36; 'anything': 0.36; 'should': 0.36; 'possible': 0.37; 'subject:: ': 0.38; 'things': 0.38; 'to:addr:python.org': 0.39; 'received:192': 0.39; 'called': 0.39; 'received:192.168': 0.40; 'remove': 0.61; 'received:62': 0.62; 'within': 0.64; 'here': 0.65; 'believe': 0.69; 'subject:this': 0.84; "'with'": 0.84; '*you': 0.84; 'from:addr:t': 0.84; "it'd": 0.84; 'locals': 0.84; 'received:62.75': 0.84; 'reasons:': 0.91 Date: Tue, 18 Sep 2012 22:31:41 +0200 From: Thomas Jollans User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:15.0) Gecko/20120827 Thunderbird/15.0 MIME-Version: 1.0 To: python-list@python.org Subject: Re: User defined lexical scoping... can I do this? References: In-Reply-To: Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit 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: 40 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1348000902 news.xs4all.nl 6930 [2001:888:2000:d::a6]:45991 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:29451 On 09/18/2012 10:10 PM, porkfried wrote: > I want to define a 'with' command that makes entries > in dictionary available within the local scope, and > stores new local variables into that dictionary. The > original scope should be restored on exit, and called > functions should not see anything special. Can I do this? No.* It is not possible to set locals by ways other than an assignment**, and it is certainly not possible to set locals in a scope other than the one you're in**. You should simply type out the dict's name. This is a lot clearer. If you want to minimize typing, you can give the variable a one-character name. Also, Python scope simply doesn't work like that. There is no block scope, only local (=function) scope, and global scope, with a dash of non-locals to spice things up a bit. > > my_dict = dict(a=1, b=2) > with MyScope(my_dict): > print "A", a, "B", b > x = 3 > print my_dict["x"] > print x # FAIL, unbound > *You could set global variables, and remove them on exit, but this is ugly for a number of reasons: this could overwrite existing globals if you're not very careful, called functions would see these globals, and they would also be exposed to other threads. **I believe there is actually a way to edit a caller's locals, but this is not documented, not portable across Python implementations and versions, and you couldn't create new locals like this, so it'd be fairly pointless here