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

海韵 lyricconch at gmail.com
Mon Jul 25 20:56:37 CEST 2011


1.
but what about ( t * s + t / s )? temp var always keep in memory.
(should not reduce ( t + s ) to ( 2 * x ) since x, y may not a number.
any operator here should be conside as  independent function. )

2.
try convert this to the lambda form:
(x+y as t, x-y as s)(t * s as u, t/s as v)(u << v as p, u>>v as q) p ** q

In runtime:
lambda = MAKE_FUNCTION/MAKE_CLOSURE =  PyFunction_New
lambda call = CALL_FUNCTION =  PyFrame_New +  PyEval_EvalFrameEx
all of them are heavy, (you can check the source code)

but:
as = PyCell_New
as enter = SETUP_FINALLY = PyFrame_BlockSetup
as leave = END_FINALLY = PyFrame_BlockPop
which is much much much more more more cheaper than above.

2011/7/26 Bruce Leban <bruce at leapyear.org>:
>
>
> On Mon, Jul 25, 2011 at 9:50 AM, 海韵 <lyricconch at gmail.com> wrote:
>>
>> Dupilcate K is used only for describe the scope rule.
>> Advantage:
>>
>> 1.  the same grammer, the same behave
>> "t" of  """(x+y as t, x-y as s) t * s + s + t""" is only valid in the
>> expression which require
>> just as
>> "e" of """except ... as e""" only live in the suite which it belongs
>>
>> 2. readability includes laconic
>> a = (x+y as t, x-y as s) t * s + s + t
>> it doesnt break thinking. from left to right, people finish thinking
>> inplace.
>> temp var will not keep in memory, people can forget them since they
>> are not valid outside the expression. everything is clean.
>> t, s = x+y,  x-y
>> a = t * s + s + t
>> in this case, thinking is break and temp var keep in memory,
>> people should take some care about it (for example, in nested loop)
>
> (x + y) * (x - y) + 2 * x
> is IMHO more readable - especially note the absence of y in the third term.
>>
>> 3. this enchant the "lambda" syntax
>> list.sort(key = lambda p: (sqrt(p.x**2+p.y**2) as r) r**2+ A*r + B)
>
> I don't know what "enchant" means here, but you can do this if you want:
> list.sort(key = lambda p: (lambda r: r**2 + A*r + B) (sqrt(p.x**2+p.y**2)))
> I'm not recommending that as I think it would be more readable to name that
> function so that it would be more clear why you're using that function as
> the sort key. But, to me, this example does not justify new syntax.
> --- Bruce



More information about the Python-ideas mailing list