[Python-ideas] Another use case for the 'lazy' (aka 'delayed') keyword
M.-A. Lemburg
mal at egenix.com
Tue Feb 28 11:54:18 EST 2017
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.
Haskell is lazy by default, but this only works because you
can pass parameters by name rather than value.
https://wiki.haskell.org/Lazy_evaluation
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.
> On Tue, Feb 28, 2017 at 7:57 AM, M.-A. Lemburg <mal at egenix.com> wrote:
>
>> On 28.02.2017 15:46, Michel Desmoulin wrote:
>>> lazy is not only practical, but it's also beautiful. It reads well. It
>>> solves a problem we all have on a regular basis.
>>
>> The only practical use case I ever ran into for lazy evaluation
>> are lazy imports, simply because imports cause a lot of
>> startup overhead. It's a case which the new keyword wouldn't
>> help with, though.
>>
>> For the discussion, it would help if you'd write up a definition
>> of where the lazy evaluation should finally happen, which use
>> cases would be allowed or not and how the compiler could detect
>> these.
>>
>> IMO, there are much better ways to write code which only evaluates
>> expensive code when really needed.
>>
>> I don't see how "lazy" could automate this in a well defined,
>> helpful and obvious way, simply because the side effects of moving
>> evaluation from the place of definition to an arbitrary other place
>> in the code are not easy to manage.
>>
>> Even if you do manage to clearly define when to evaluate a lazy
>> expression, the context in which it gets evaluated may
>> have already changed, giving wrong results, causing exceptions
>> due to missing information. Delaying exceptions can also
>> break the exception handling in your application.
>>
>> PS: You run into the same issues with lazy imports, which is
>> why special care has to be taken when using these.
>>
>> --
>> Marc-Andre Lemburg
>> eGenix.com
>>
>> Professional Python Services directly from the Experts (#1, Feb 28 2017)
>>>>> Python Projects, Coaching and Consulting ... http://www.egenix.com/
>>>>> Python Database Interfaces ... http://products.egenix.com/
>>>>> Plone/Zope Database Interfaces ... http://zope.egenix.com/
>> ________________________________________________________________________
>>
>> ::: We implement business ideas - efficiently in both time and costs :::
>>
>> eGenix.com Software, Skills and Services GmbH Pastor-Loeh-Str.48
>> D-40764 Langenfeld, Germany. CEO Dipl.-Math. Marc-Andre Lemburg
>> Registered at Amtsgericht Duesseldorf: HRB 46611
>> http://www.egenix.com/company/contact/
>> http://www.malemburg.com/
>>
>> _______________________________________________
>> Python-ideas mailing list
>> Python-ideas at python.org
>> https://mail.python.org/mailman/listinfo/python-ideas
>> Code of Conduct: http://python.org/psf/codeofconduct/
>>
>
>
>
--
Marc-Andre Lemburg
eGenix.com
Professional Python Services directly from the Experts (#1, Feb 28 2017)
>>> Python Projects, Coaching and Consulting ... http://www.egenix.com/
>>> Python Database Interfaces ... http://products.egenix.com/
>>> Plone/Zope Database Interfaces ... http://zope.egenix.com/
________________________________________________________________________
::: We implement business ideas - efficiently in both time and costs :::
eGenix.com Software, Skills and Services GmbH Pastor-Loeh-Str.48
D-40764 Langenfeld, Germany. CEO Dipl.-Math. Marc-Andre Lemburg
Registered at Amtsgericht Duesseldorf: HRB 46611
http://www.egenix.com/company/contact/
http://www.malemburg.com/
More information about the Python-ideas
mailing list