[Python-ideas] Another use case for the 'lazy' (aka 'delayed') keyword

David Mertz mertz at gnosis.cx
Tue Feb 28 12:10:00 EST 2017

On Tue, Feb 28, 2017 at 8:54 AM, M.-A. Lemburg <mal at egenix.com> wrote:

> On 28.02.2017 17:35, David Mertz wrote:
> > Clearly there is SOME semantics that is consistent and coherent since
> many
> > languages have either a lazy or eager declaration syntax, with different
> > defaults between languages but both being built in.  There are a lot of
> > ways that Python isn't Haskell, obviously.  But both Scheme and OCaml are
> > eager by default with a lazy declaration (and Haskell or Miranda have an
> > eager declaration correspondingly).
> >
> > It might be worth looking at their semantics in the PEP.
> Scheme, for example, uses an explicit approach to turning
> a promise into a value:
> http://www.shido.info/lisp/scheme_lazy_e.html
> This makes a lot of sense, but you can already have the
> same in Python using generators.

I think the closer equivalent is a lambda.  And it's quite true that
Scheme's `force` is pretty much the same thing as making a call to the
lambda later.  E.g.

def force(x):

    if callable(x):

        return x()


        return x

In Python we only have pass by value (parameters to functions
> get pushed onto the VM stack). Pass by name can also be had,
> but requires explicit indirection, e.g. by using a generator
> as wrapper.

I wouldn't want to change pass-by-value semantics.  What I imagine instead
is a new type `lazy` or `delayed` or `deferred` (whatever spelling).
Delayed objects would have that type, and access to objects would need to
do an implementation-level call to a "_force" that looks something like:

def _force(x):

    if isinstance(x, delayed):

        return _concretize(x)


        return x

I *do* see the obvious likely problem here.  Object access is perhaps the
most common thing that happens in Python, and adding a new conditional
check to every one of those could very well slow down all the non-lazy
behavior far too much.

Keeping medicines from the bloodstreams of the sick; food
from the bellies of the hungry; books from the hands of the
uneducated; technology from the underdeveloped; and putting
advocates of freedom in prisons.  Intellectual property is
to the 21st century what the slave trade was to the 16th.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20170228/2117c577/attachment.html>

More information about the Python-ideas mailing list