[Python-ideas] And now for something completely different

Josiah Carlson josiah.carlson at gmail.com
Thu Sep 18 05:26:04 CEST 2008

On Wed, Sep 17, 2008 at 8:05 PM, Scott Dial
<scott+python-ideas at scottdial.com> wrote:
> Josiah Carlson wrote:
>> I don't know of a spelling of deferred execution blocks that I would
>> find reasonable :/ .
> His request is worse than that. He didn't ask for deferred execution, he
> asked for deferred values. In python Right Now, you can already do
> deferred execution blocks (forgetting the "I don't like seeing lambdas!"
> argument). Rather, he wants to be able to have arbitrary expressions be
> treated as lazy, not at the call-site either but determined by the receiver:
> """
> I'd prefer to write
> def foo ( lambda: arg ):
>    return arg # evaluation happens here
> foo ( x )
> """
> I'm sorry but this will never, ever, ever happen in Python. That is such
> a *huge* shift in semantics that you should go use a different language.
> In such languages, it is *impossible* to know when the expression will
> actually be evaluated, and while this is fine in a purely functional
> environment, this is not easy to deal with in imperative programming.
> If you write "foo(bar())", then you have no way of knowing whether
> "bar()" is ever run. And in a world that depends on side-effects, that
> forces you to force evaluations of expressions. You would be forced to
> write "x = bar(); foo(x)" (assuming assignment isn't lazy), which is
> less clear than having to write "foo(lambda: bar())" in the opposite
> case. Explicit is better than implicit.

What I was thinking (completely unreasonable, I would be -1 on this)
is that the following two lines would be equivalent...

x = lambda: foo.bar
x = `foo.bar`

Sadly, that doesn't really gain you anything - though it would be
convenient in the case of an If function (or similar):

def If(condition, truev, falsev=`None`):
    if condition:
        return truev()
    return falsev()

result = If(x < 5, `foo.smallx(x)`, `foo.largex(x)`)

Though, because it's still a function call, it's still slow.  And even
worse, uses a previously decided-upon ugly pair of characters.

While I understand that Cliff wanted auto-execution, I think that
requiring a call isn't out of the question (what do Ruby thunks

 - Josiah

More information about the Python-ideas mailing list