[Python-ideas] use "as" for block scope support

Guido van Rossum guido at python.org
Mon Jul 25 17:02:28 CEST 2011


I *think* you're proposing that the variable named in the "as" clause
should go out of scope when the blog ends. We already do a similar
thing with "except ... as ...", but I don't see how we can do this
without breaking backwards compatibility. I also don't think there is
a particularly good reason to do (we did have a good reason in the
case of except). Finally, with has lot of special semantics, you
couldn't just use "with 2+2 as four: ...".

--Guido

On Mon, Jul 25, 2011 at 4:52 AM, 海韵 <lyricconch at gmail.com> wrote:
> New submission from HaiYun Yan <lyricconch at gmail.com>:
> just like mozilla javascript "let" extension
> https://developer.mozilla.org/en/new_in_javascript_1.7#Block_scope_with_let_%28Merge_into_let_Statement%29
> according function args can have the default value
> let_stmt is unnecessary for python.
> ============example 1===========
> f = None
> with open(...) as f:
>      assert hasattr(f, "read")
>      f = 1
>      assert f == 1
> assert f is None
> ==========example 2==============
> try:
>     e = None
>     1/0
> except ZeroDivisionError as e:
>     assert isinstance(e, ZeroDivisionError)
> except ... as e:
>     # the same as except: but has the exception instance store to e
>     assert isinstance(e, BaseException)
> else:
>     assert e is None
> finally:
>     assert e is None
> assert e is None
> ===========example 3==============
> while SEQ.next_availd() as tid:
>      assert bool(tid) is True
> assert "tid" not in dir()
> ===========example 4==============
> assert "zero" not in dir()
> A = (id(None) % 1 as zero) zero + 5
> assert A == 5
> assert "zero" not in dir()
> ======example 5==================
> K = 1
> A = (K+1 as K, K+2 as Q) K*Q
> assert A == 6 # (1+1) * (1+2)
> assert K==1
> ============example 6===============
> K = 1
> A = (K+1 as K) (K+2 as K) K * K
> assert A == 16  # ((1+1) + 2) * ((1+1) + 2)
> assert K == 1
> _______________________________________________
> Python-ideas mailing list
> Python-ideas at python.org
> http://mail.python.org/mailman/listinfo/python-ideas
>



-- 
--Guido van Rossum (python.org/~guido)



More information about the Python-ideas mailing list