Lazy evaluated

Habibutsu habibutsu at gmail.com
Fri Mar 29 06:37:53 EDT 2013


On 03/29/2013 05:40 AM, Steven D'Aprano wrote:
> On Thu, 28 Mar 2013 22:37:47 +0300, Habibutsu wrote:
>
>> For example, we have  following code:
>>
>> 01|    def foo():
>> 02|        return 1
>> 03|
>> 04|    value = foo()
>> 05|
>> 06|    if value == 1:
>> 07|        print value,"- equal 1"
>> 08|
>> 09|    if isinstance(value, int):
>> 10|        print value,"- is int"
>> 11|    else:
>> 12|        print value,"- is not int"
>>
>> Task is to create lazy evaluation for function 'foo'. For decision this
>> task we create special function 'lazy' which turn original function into
>> a lazy evaluated function by means of creating proxy object that
>> evaluated value if needed. We add following code:
>>
>>
>> 01|    def lazy(func):
>> 02|
>> 03|        class ProxyLazy(object):
>> 04|            _result_value = None
>> 05|
>> 06|            @property
>> 07|            def result_value(self):
> [...]
>
>
> It is often useful for the reader to copy and paste code blocks like this
> into the interactive interpreter. It is more friendly and useful if you
> paste it in a format that makes that easy (no prompts, no line numbers,
> no blank lines inside classes or functions). Otherwise the reader has to
> copy your code, paste it into an editor, strip out the line numbers and
> blank lines, copy and paste it into the interpreter, and THEN they can
> finally test your code and see what it does.
>
Previously I not to do it, yesterday decide to experiment. It seemed to 
me more readable if to see in the email-clent. Sorry, I will keep in 
mind in the future.
>> 30|    lazy_foo = lazy(foo)
>> 31|    value = lazy_foo()
>>
>> Unfortunately, this method not allow to use 'isinstance' for check type.
> Correct. And so it should not, because it value is not an int, it is a
> ProxyLazy object. You cannot use a ProxyLazy object where an int is
> expected:
>
> py> value + 1
> Traceback (most recent call last):
>    File "<stdin>", line 1, in <module>
> TypeError: unsupported operand type(s) for +: 'ProxyLazy' and 'int'
>
Why not?

     def __add__(self, other):
         return self._result_value + other

and then
 >>> print value + 1
2

> so it is inappropriate to claim that value is an int.

I can overload all arithmetic operators and get almost really int value, 
but I don't like to write many trivial code.

> I must admit, I don't understand the value of this "lazy proxy" type you
> have created. You get a proxy like this:
>
> value = lazy(foo)()  # get a lazy proxy
> # much later
> value = value.func()  # now safe to use as an int
> print(value + 1)
>
> What benefit does the lazy proxy give? Why not just do this?
Because some people like to use "list comprehensions" and other not, 
some people like labmda and other not. Advantage is that I can write 
more expressive code and use different strategies for different cases. 
"value = value.func()" is looks sad while I just need the value.
>
> value = foo()  # functions are already a lazy proxy
> # much later
> value = value()  # now safe to use as an int
> print(value + 1)
>
>
> [...]
>> And everything seems to work, but appear other questions. If original
>> function return different types - what to do in this case? Where i am
>> wrong? What other way to do that. Was no idea to create keyword 'lazy'
>> in Python?
> Why should it be a keyword? Python has very few keywords, and most of
> them are for programming flow control, like "if", "else", "for", "while",
> "pass", etc.
>
Question not in why I can't use exist tools - I can. If i can write code 
without classes that it does not mean that classes need to delete from 
language. Question why in language not present this ability?

Thanks



More information about the Python-list mailing list