[Python-ideas] use "as" for block scope support
海韵
lyricconch at gmail.com
Mon Jul 25 13:52:45 CEST 2011
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
More information about the Python-ideas
mailing list