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


Groups > comp.lang.python > #29451

Re: User defined lexical scoping... can I do this?

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 <t@jollybox.de>
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 <t@jollybox.de>
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 <d9aec95e-aedb-442d-ab6d-320f43f61b93@googlegroups.com>
In-Reply-To <d9aec95e-aedb-442d-ab6d-320f43f61b93@googlegroups.com>
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 <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.891.1348000902.27098.python-list@python.org> (permalink)
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

Show key headers only | View raw


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

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


Thread

User defined lexical scoping... can I do this? porkfried <weissman.mark@gmail.com> - 2012-09-18 13:10 -0700
  Re: User defined lexical scoping... can I do this? Thomas Jollans <t@jollybox.de> - 2012-09-18 22:31 +0200
  Re: User defined lexical scoping... can I do this? weissman.mark@gmail.com - 2012-09-18 13:50 -0700
    Re: User defined lexical scoping... can I do this? Thomas Jollans <t@jollybox.de> - 2012-09-18 23:51 +0200
    Re: User defined lexical scoping... can I do this? Terry Reedy <tjreedy@udel.edu> - 2012-09-18 21:38 -0400
      Re: User defined lexical scoping... can I do this? Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2012-09-19 04:03 +0000
  Re: User defined lexical scoping... can I do this? Mark Lawrence <breamoreboy@yahoo.co.uk> - 2012-09-19 00:47 +0100

csiph-web