[Python-ideas] Another way to avoid clumsy lambdas, while adding new functionality

Carl Smith carl.input at gmail.com
Wed Mar 5 00:09:02 CET 2014


The tentatively proposed idea here is using dollar signed expressions to
define 'bills'. A bill object is essentially an expression which can be
evaluated any number of times, potentially in different scopes.

The following expression [a bill literal] would be pointless, but would
define a bill that always evaluates to 1.

    $1

So, eval($1)==1.

Some better examples...

* assign a bill to `a` so that `a` will evaluate to the value of the name
`foo` any time that `a` is evaluated, in the scope of that evaluation

   a = $foo

* as above, but always plus one

   a = $foo + 1

* make `a` a bill that evaluates to the value of the name `foo` at the time
that `a` is evaluated, in that scope, plus the value of `bar` **at the time
and in the scope of the assignment to `a`**

   a = $foo + bar

Note. Similarly to mixing floats with ints, any expression that contains a
bill evaluates to a bill, so if `a` is a bill, `b=a+1` makes `b` a bill
too. Passing a bill to eval should be the obvious way to get the value.

The point? It allows functions to accept bills to use internally. The
function would specify any names the bill can reference in the function's
API, like keywords.

def f(b): # the bill arg `b` can reference `item`
    for item in something:
        if eval(b): return True

f($item < 0)

You could also use a function call, for example `$foo()` would evaluate to
a bill that evaluates to a call to `foo` in the scope and at the time of
any evaluation of the bill.

I've no idea if this is even possible in Python, and have no hope of
implementing it, but thought I'd share :)
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20140304/f5479771/attachment.html>


More information about the Python-ideas mailing list