[Python-ideas] Idea: Lazy ... statement
Jim Jewett
jimjjewett at gmail.com
Wed Oct 15 02:25:20 CEST 2008
On Tue, Oct 14, 2008 at 4:39 PM, Mathias Panzenböck
<grosser.meister.morti at gmx.net> wrote:
> Jim Jewett schrieb:
>> On Mon, Oct 13, 2008 at 4:12 AM, Greg Ewing <greg.ewing at canterbury.ac.nz> wrote:
>> Since this can be (awkwardly) done now, I don't think the problem is
>> big enough for another keyword. On the other hand, a "lazy" keyword
>> that worked in other contexts might have some merit.
>> That would solve at least one major objection to setdefault-style functions.
>> def setdefault(self, k, lazy d=None): ...
> I don't understand what this lazy keyword should do?
"Hey, this next thing that you were about to execute? Don't do it yet."
Part of the problem with setdefault is that calculating the default
can be expensive.
profile.setdefault(user, askuser())
This should get the user's stored profile; if there is no stored
setting, it should ask the user and save the result for future
reference.
Unfortunately, askuser() gets evaluated before setdefault is called,
so the user is *always* asked, "just in case".
profile.setdefault(user, lazy askuser())
would say not to bother the user *unless* the default were actually
needed. (Just as the lazy import wouldn't bother setting up the other
module unless/until it were actually needed.)
lazyness can seem pretty important in some programming styles, but ...
those styles don't seem to be such a good fit for python anyhow.
Whether there are enough use cases in typical python ... I'm not sure.
(But I'm pretty sure that import alone doesn't make it.)
-jJ
More information about the Python-ideas
mailing list