Some notes

Bengt Richter bokr at oz.net
Sun Oct 17 16:34:45 EDT 2004


On Sat, 16 Oct 2004 14:34:30 +0200, aleaxit at yahoo.com (Alex Martelli) wrote:
[...]
>
>The iterator over arguments, which I propose, could not be implemented
>in Python today (Python today has no way to collect yet-unevaluated
>arguments).  I do believe that the C interpreter would need few changes
>to implement it (what syntax to use to signal it being the one
>contentious part), and even fewer to accept a .skip method on generators
>and use it to set a local variable _must_skip_next_yield in the
>generator's frame (not particularly useful, I believe -- not all
>iterators would have to be skippable, and, unless strong use cases
>emerge, normal generators might well be deemed nonskippable ones).
>
If py3k eliminated current back-ticks in favor of a new use, namely

    `expr

being short spelling of 

    lambda:expr

(Actually, I think 'expr should not just be lambda:expr but capture snapshot
references to the variables in the expr. I.e., if expr was 2*a.x-3 you'd have
lambda a=a:2*a.x-3 -- while making sure that a etc are just bare name references,
but then I couldn't just type in the simplified examples below, which I don't
want to retype ;-)

then you could write stuff like

 >>> def ifelse(c, t, f):
 ...     if c(): return t()
 ...     else: return f()
 ...

and call that using

    ifelse(`condition, `value_if_true, `value_if_false)

except for now we have to use the long spelling of lambda
(and should use longer yet spelling ;-) :

 >>> def show(x): print 'evaluated %r'%(x,); return x
 ...
 >>> c = lambda: show(True)
 >>> t = lambda: show('true value')
 >>> f = lambda: show('false value')
 >>> ifelse(c, t, f)
 evaluated True
 evaluated 'true value'
 'true value'
 >>> ifelse(lambda:show(0), t, f)
 evaluated 0
 evaluated 'false value'
 'false value'

of course, if you are modifying python, you could make ifelse a special form,
along with cond, and have the back-ticks implied instead of explicit.

if you think of cond as a flat series of (condition, value_expr) pairs,
(was that the intent?)

 >>> def cond(*cvp):
 ...     cvp = iter(cvp)
 ...     for c in cvp:
 ...         if c(): return cvp.next()()
 ...         cvp.next()
 ...     return False
 ...
 >>> true = lambda:show(True)
 >>> false = lambda:show(False)
 >>>
 >>> cond(false, f, false, t, true, t, false, f)
 evaluated False
 evaluated False
 evaluated True
 evaluated 'true value'
 'true value'
 >>> cond(false, f, false, t, false, t, false, f)
 evaluated False
 evaluated False
 evaluated False
 evaluated False
 False
 >>> cond(false, f,  true, t, false, t, false, f)
 evaluated False
 evaluated True
 evaluated 'true value'
 'true value'

Regards,
Bengt Richter



More information about the Python-list mailing list