Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #50161
| Path | csiph.com!usenet.pasdenom.info!weretis.net!feeder1.news.weretis.net!feeder.erje.net!eu.feeder.erje.net!newsfeed.freenet.ag!news2.euro.net!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail |
|---|---|
| Return-Path | <joshua.landau.ws@gmail.com> |
| X-Original-To | python-list@python.org |
| Delivered-To | python-list@mail.python.org |
| X-Spam-Status | OK 0.032 |
| X-Spam-Evidence | '*H*': 0.94; '*S*': 0.00; 'none:': 0.07; 'variables': 0.07; 'namespace': 0.09; 'stealing': 0.09; 'cc:addr:python-list': 0.11; 'def': 0.12; 'closure.': 0.16; 'dictionary,': 0.16; 'foo():': 0.16; 'personally,': 0.16; 'wrote:': 0.18; 'thu,': 0.19; '>>>': 0.22; 'import': 0.22; 'cc:addr:python.org': 0.22; 'creating': 0.23; 'skip:i 40': 0.24; 'cc:2**0': 0.24; 'this:': 0.26; 'pass': 0.26; 'header:In-Reply-To:1': 0.27; 'chris': 0.29; 'said,': 0.30; 'skip:@ 10': 0.30; 'message-id:@mail.gmail.com': 0.30; "i'm": 0.30; 'that.': 0.31; 'inspect': 0.31; 'but': 0.35; 'received:google.com': 0.35; 'yield': 0.36; 'too': 0.37; 'pm,': 0.38; 'skip:i 50': 0.60; 'new': 0.61; 'july': 0.63; 'skip:n 10': 0.64; 'talking': 0.65; 'to:addr:gmail.com': 0.65; 'reads': 0.68; 'jul': 0.74; '2:30': 0.84; 'hardly': 0.84; 'do:': 0.91; '2013': 0.98 |
| DKIM-Signature | v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:from:date:message-id:subject:to :cc:content-type; bh=lbDjHWH5H4Baa4pGisezmFtbRRXLJVnb2mMJORIQSrQ=; b=Y9+aTscolbAlwN6ztUgPCIm0e3xXkbZmKxK4EfWtMOHGaUPnk/BdqTIoQ7xnlo6mr8 5f0gp8mlY+mv92nmW/sRdotwBh6HjNP8yKVs62n9WCUSgtdG0K5HqUbrPBHlPE5+R2uS kLOKJVPr9FPXAEKV0D09RvlTuge0xN41jroSbDsvCxwXNn7sH8qw5cpbub4ROP2M9w0D 9sJwU55rCHFwWuOkcYI4t5qiYz7WkYndXDIcYB9tdaY9JtlDe0GXGVLZKhv91wz3rvHo ro7Y9L45DxdeuwgJoxCQv0G3O35tUsO4c1JXiKz6hgz10XScf4xA1Jt2nRpKWj0met13 wKZQ== |
| X-Received | by 10.112.170.166 with SMTP id an6mr11213973lbc.22.1373302736912; Mon, 08 Jul 2013 09:58:56 -0700 (PDT) |
| MIME-Version | 1.0 |
| In-Reply-To | <CAPTjJmr9K0eW6Giwifb1iBmavCoZEuY4mTQ7xBDWi1Ko=Qex1w@mail.gmail.com> |
| References | <51d4eb9c$0$29999$c3e8da3$5496439d@news.astraweb.com> <CAPTjJmp2dpbRxbWct-oBuhM9b1WynjP-w0WVN=C1vEk0oJMBMA@mail.gmail.com> <CAN1F8qU=SPw=1nR9G_LR0BKfpr8NSNcL1iepiHo0XzXoAMYA4Q@mail.gmail.com> <CAPTjJmr9K0eW6Giwifb1iBmavCoZEuY4mTQ7xBDWi1Ko=Qex1w@mail.gmail.com> |
| From | Joshua Landau <joshua.landau.ws@gmail.com> |
| Date | Mon, 8 Jul 2013 17:58:16 +0100 |
| Subject | Re: Default scope of variables |
| To | Chris Angelico <rosuav@gmail.com> |
| Content-Type | text/plain; charset=UTF-8 |
| Cc | python-list <python-list@python.org> |
| 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.4389.1373302745.3114.python-list@python.org> (permalink) |
| Lines | 73 |
| NNTP-Posting-Host | 2001:888:2000:d::a6 |
| X-Trace | 1373302745 news.xs4all.nl 15886 [2001:888:2000:d::a6]:50761 |
| X-Complaints-To | abuse@xs4all.nl |
| Xref | csiph.com comp.lang.python:50161 |
Show key headers only | View raw
On 4 July 2013 05:36, Chris Angelico <rosuav@gmail.com> wrote:
> On Thu, Jul 4, 2013 at 2:30 PM, Joshua Landau
> <joshua.landau.ws@gmail.com> wrote:
>> That said, I'm not too convinced. Personally, the proper way to do
>> what you are talking about is creating a new closure. Like:
>>
>> for i in range(100):
>> with new_scope():
>> for i in range(100):
>> func(i)
>> func(i) # Using i from original loop
>>
>> But it's not like Python'll ever support that.
>>
>
> def foo():
> for i in range(3):
> print("outer",i)
> def inner():
> for i in range(4):
> print("inner",i)
> inner()
> print("outer",i)
>
> That works, but you then have to declare all your nonlocals, and it
> hardly reads well.
Stealing concepts shamelessly from
http://www.slideshare.net/r1chardj0n3s/dont-do-this-24000445, you can
do this:
import inspect
from contextlib import contextmanager
@contextmanager
def scope(namespace=None):
old_names = inspect.currentframe().f_back.f_back.f_locals.copy()
yield
names = inspect.currentframe().f_back.f_back.f_locals
if namespace is not None:
new_names = {k:v for k, v in names.items() if k not in
old_names and v is not namespace}
namespace.update(**new_names)
names.clear()
names.update(old_names)
So you *can* do:
>>> for i in range(3):
... with scope():
... for i in range(3):
... print("Inner:", i)
... print("Outer", i)
Inner: 0
Inner: 1
Inner: 2
Outer 0
Inner: 0
Inner: 1
Inner: 2
Outer 1
Inner: 0
Inner: 1
Inner: 2
Outer 2
:)
If you pass scope() a dictionary, all the new variables will get added to it.
Back to comp.lang.python | Previous | Next — Previous in thread | Find similar | Unroll thread
Default scope of variables Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2013-07-04 03:27 +0000
Re: Default scope of variables Chris Angelico <rosuav@gmail.com> - 2013-07-04 14:07 +1000
Re: Default scope of variables Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2013-07-04 05:32 +0000
Re: Default scope of variables Chris Angelico <rosuav@gmail.com> - 2013-07-04 15:47 +1000
Re: Default scope of variables Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2013-07-04 16:38 +0000
Re: Default scope of variables Chris Angelico <rosuav@gmail.com> - 2013-07-05 02:58 +1000
Re: Default scope of variables Wayne Werner <wayne@waynewerner.com> - 2013-07-07 08:13 -0500
Re: Default scope of variables Chris Angelico <rosuav@gmail.com> - 2013-07-07 23:43 +1000
Re: Default scope of variables Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2013-07-07 16:03 +0000
Re: Default scope of variables Chris Angelico <rosuav@gmail.com> - 2013-07-08 10:46 +1000
Re: Default scope of variables alex23 <wuwei23@gmail.com> - 2013-07-09 14:52 +1000
Re: Default scope of variables Chris Angelico <rosuav@gmail.com> - 2013-07-09 15:07 +1000
Re: Default scope of variables alex23 <wuwei23@gmail.com> - 2013-07-09 16:08 +1000
Re: Default scope of variables Chris Angelico <rosuav@gmail.com> - 2013-07-09 16:13 +1000
Re: Default scope of variables "Frank Millman" <frank@chagford.com> - 2013-07-09 09:35 +0200
Re: Default scope of variables Chris Angelico <rosuav@gmail.com> - 2013-07-09 17:45 +1000
Re: Default scope of variables Ian Kelly <ian.g.kelly@gmail.com> - 2013-07-09 01:56 -0600
Re: Default scope of variables "Frank Millman" <frank@chagford.com> - 2013-07-09 10:22 +0200
Re: Default scope of variables "Frank Millman" <frank@chagford.com> - 2013-07-09 10:38 +0200
Re: Default scope of variables Ethan Furman <ethan@stoneleaf.us> - 2013-07-09 09:07 -0700
Re: Default scope of variables Ian Kelly <ian.g.kelly@gmail.com> - 2013-07-09 10:44 -0600
Re: Default scope of variables Ethan Furman <ethan@stoneleaf.us> - 2013-07-09 10:23 -0700
Re: Default scope of variables Ian Kelly <ian.g.kelly@gmail.com> - 2013-07-09 12:41 -0600
Re: Default scope of variables Ethan Furman <ethan@stoneleaf.us> - 2013-07-09 12:19 -0700
Re: Default scope of variables "Frank Millman" <frank@chagford.com> - 2013-07-10 07:54 +0200
Re: Default scope of variables Ian Kelly <ian.g.kelly@gmail.com> - 2013-07-10 09:42 -0600
Re: Default scope of variables Ethan Furman <ethan@stoneleaf.us> - 2013-07-10 08:29 -0700
Re: Default scope of variables "Frank Millman" <frank@chagford.com> - 2013-07-11 07:52 +0200
Re: Default scope of variables Ethan Furman <ethan@stoneleaf.us> - 2013-07-07 09:52 -0700
Re: Default scope of variables Ethan Furman <ethan@stoneleaf.us> - 2013-07-07 11:59 -0700
Re: Default scope of variables Chris Angelico <rosuav@gmail.com> - 2013-07-08 10:48 +1000
Re: Default scope of variables Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2013-07-08 02:23 +0000
Re: Default scope of variables Chris Angelico <rosuav@gmail.com> - 2013-07-08 13:11 +1000
Re: Default scope of variables Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2013-07-08 05:00 +0000
Re: Default scope of variables Chris Angelico <rosuav@gmail.com> - 2013-07-08 15:14 +1000
Re: Default scope of variables Peter Otten <__peter__@web.de> - 2013-07-04 08:48 +0200
Re: Default scope of variables Ian Kelly <ian.g.kelly@gmail.com> - 2013-07-04 01:12 -0600
Re: Default scope of variables Dave Angel <davea@davea.name> - 2013-07-04 03:06 -0400
Re: Default scope of variables Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2013-07-04 15:52 +0000
Re: Default scope of variables Rotwang <sg552@hotmail.co.uk> - 2013-07-04 17:54 +0100
Re: Default scope of variables Peter Otten <__peter__@web.de> - 2013-07-04 20:36 +0200
Re: Default scope of variables Joshua Landau <joshua.landau.ws@gmail.com> - 2013-07-05 01:04 +0100
Re: Default scope of variables Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2013-07-05 01:24 +0000
Re: Default scope of variables Dave Angel <davea@davea.name> - 2013-07-04 22:03 -0400
Re: Default scope of variables Joshua Landau <joshua.landau.ws@gmail.com> - 2013-07-05 03:29 +0100
Re: Default scope of variables Joshua Landau <joshua.landau.ws@gmail.com> - 2013-07-05 03:27 +0100
Re: Default scope of variables Rotwang <sg552@hotmail.co.uk> - 2013-07-05 07:28 +0100
Re: Default scope of variables Neil Cerutti <neilc@norwich.edu> - 2013-07-05 13:24 +0000
Re: Default scope of variables Chris Angelico <rosuav@gmail.com> - 2013-07-05 23:43 +1000
Re: Default scope of variables Neil Cerutti <neilc@norwich.edu> - 2013-07-05 15:36 +0000
Re: Default scope of variables Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2013-07-07 16:08 +0000
Re: Default scope of variables Neil Cerutti <neilc@norwich.edu> - 2013-07-08 11:54 +0000
Re: Default scope of variables Joshua Landau <joshua.landau.ws@gmail.com> - 2013-07-08 14:14 +0100
Re: Default scope of variables Lele Gaifax <lele@metapensiero.it> - 2013-07-04 14:43 +0200
Re: Default scope of variables Wayne Werner <wayne@waynewerner.com> - 2013-07-04 10:45 -0500
Re: Default scope of variables Joshua Landau <joshua.landau.ws@gmail.com> - 2013-07-04 05:30 +0100
Re: Default scope of variables Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2013-07-04 05:45 +0000
Re: Default scope of variables Chris Angelico <rosuav@gmail.com> - 2013-07-04 14:36 +1000
Re: Default scope of variables Joshua Landau <joshua.landau.ws@gmail.com> - 2013-07-04 06:09 +0100
Re: Default scope of variables Joshua Landau <joshua.landau.ws@gmail.com> - 2013-07-08 17:58 +0100
csiph-web