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. > 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' so it is inappropriate to claim that value is an int. 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? 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. -- Steven -- http://mail.python.org/mailman/listinfo/python-list