Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #42185
| Path | csiph.com!newsfeed.hal-mli.net!feeder3.hal-mli.net!newsfeed.hal-mli.net!feeder2.hal-mli.net!npeer02.iad.highwinds-media.com!feed-me.highwinds-media.com!cyclone02.ams2.highwinds-media.com!news.highwinds-media.com!voer-me.highwinds-media.com!eweka.nl!lightspeed.eweka.nl!194.134.4.91.MISMATCH!news2.euro.net!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail |
|---|---|
| Return-Path | <habibutsu@gmail.com> |
| X-Original-To | python-list@python.org |
| Delivered-To | python-list@mail.python.org |
| X-Spam-Status | OK 0.003 |
| X-Spam-Evidence | '*H*': 0.99; '*S*': 0.00; 'else:': 0.03; 'args': 0.07; 'none:': 0.07; 'parameter': 0.09; 'variant': 0.09; 'wrapper': 0.09; 'def': 0.12; '@property': 0.16; '__ne__': 0.16; 'bases,': 0.16; 'foo()': 0.16; 'foo():': 0.16; 'func': 0.16; 'int)': 0.16; 'int):': 0.16; 'other)': 0.16; 'value.': 0.19; 'work,': 0.20; 'seems': 0.21; 'python?': 0.22; 'print': 0.22; 'creating': 0.23; 'header:User-Agent:1': 0.23; 'proxy': 0.24; 'task': 0.26; 'code:': 0.26; 'idea': 0.28; 'function': 0.29; 'appear': 0.29; 'evaluation': 0.30; 'needed.': 0.30; "skip:' 10": 0.31; 'that.': 0.31; 'class': 0.32; 'skip:_ 10': 0.34; 'created': 0.35; 'equal': 0.35; 'but': 0.35; 'received:google.com': 0.35; 'add': 0.35; 'keyword': 0.36; 'method': 0.36; 'thanks': 0.36; 'example,': 0.37; 'turn': 0.37; 'message-id:@gmail.com': 0.38; 'to:addr:python-list': 0.38; 'to:addr:python.org': 0.39; 'skip:u 10': 0.60; 'different': 0.65; 'helping': 0.70; 'received:178': 0.74; 'special': 0.74; 'case?': 0.84; 'lazy': 0.91 |
| DKIM-Signature | v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=x-received:message-id:date:from:user-agent:mime-version:to:subject :content-type:content-transfer-encoding; bh=eNu6Z6UI0h6PGUOYaQaOsFl/sY9JgimRxqMfBXGuPBk=; b=lraGBw9uyclI5CT1+GPo2PEbplqHg2BSbM3pDx6/kdWY70ZgU1qRb+RKtzhkGg4XcB DxCtERfAXbAR7TiRq6K9ZsH5ZA2pfbh9lLA9/9+Gp1fuogZcbAAmOKP6BodyN84vPqj0 fgTA3WrRmmyMMQ3Q0FrO4kfrh7IYJHPnGWjvG/tHe8vwfLqF0dk7qYheyjgCOnTrzH/y IItB/kS9Y/um2fVThEfN5f6X5ikgWE+1rgbdXlv2/C1DpCwt3flU3/5W8OZ8urL9B6VV djPSPsNoeSnbaeGTyXzEkUk0MwKdxR0/iodJXJO6jhpljX41t6DbmRtYwFVqAL6RuRHG lEnw== |
| X-Received | by 10.14.193.134 with SMTP id k6mr70234585een.37.1364499529508; Thu, 28 Mar 2013 12:38:49 -0700 (PDT) |
| Date | Thu, 28 Mar 2013 22:37:47 +0300 |
| From | Habibutsu <habibutsu@gmail.com> |
| User-Agent | Mozilla/5.0 (X11; Linux i686; rv:17.0) Gecko/20130110 Thunderbird/17.0.2 |
| MIME-Version | 1.0 |
| To | python-list@python.org |
| Subject | Lazy evaluated |
| Content-Type | text/plain; charset=UTF-8; format=flowed |
| Content-Transfer-Encoding | 7bit |
| X-BeenThere | python-list@python.org |
| X-Mailman-Version | 2.1.15 |
| Precedence | list |
| List-Id | General discussion list for the Python programming language <python-list.python.org> |
| List-Unsubscribe | <http://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe> |
| List-Archive | <http://mail.python.org/pipermail/python-list/> |
| List-Post | <mailto:python-list@python.org> |
| List-Help | <mailto:python-list-request@python.org?subject=help> |
| List-Subscribe | <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe> |
| Newsgroups | comp.lang.python |
| Message-ID | <mailman.3910.1364499536.2939.python-list@python.org> (permalink) |
| Lines | 77 |
| NNTP-Posting-Host | 2001:888:2000:d::a6 |
| X-Trace | 1364499536 news.xs4all.nl 6911 [2001:888:2000:d::a6]:53927 |
| X-Complaints-To | abuse@xs4all.nl |
| X-Received-Bytes | 5843 |
| Xref | csiph.com comp.lang.python:42185 |
Show key headers only | View raw
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): 08| if self._result_value is not None: 09| return self._result_value 10| self._result_value = self.func(*self.args, **self.kw) 11| return self._result_value 12| 13| def __str__(self): 14| return str(self.result_value) 15| 16| def __cmp__(self, other): 17| return cmp(self.result_value, other) 18| 19| # and other __unicode__, __eq__, __ne__ and so on 20| 21| def wrapper(*args, **kw): 22| proxy = ProxyLazy() 23| proxy.func = func 24| proxy.args = args 25| proxy.kw = kw 26| return proxy 27| 28| return wrapper 29| 30| lazy_foo = lazy(foo) 31| value = lazy_foo() Unfortunately, this method not allow to use 'isinstance' for check type. We can create other variant of function 'lazy' is 'lazy_promise' in which additional parameter will be passed with type of result value. After we can create 'LazyMetaClass' with helping of which will be created 'ProxyLazy' 01| def lazy_promise(func, resultclass): 02| 03| class LazyMetaClass(type): 04| def __new__(cls, name, bases, attrs): 05| return super(LazyMetaClass, cls).__new__(cls, name, (resultclass,), attrs) 06| 07| class ProxyLazy(object): 08| __metaclass__ = LazyMetaClass ... 35| lazy_foo = lazy_promise(foo, int) 36| value = lazy_foo() 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? Thanks
Back to comp.lang.python | Previous | Next — Next in thread | Find similar | Unroll thread
Lazy evaluated Habibutsu <habibutsu@gmail.com> - 2013-03-28 22:37 +0300
Re: Lazy evaluated Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2013-03-29 02:40 +0000
Re: Lazy evaluated Habibutsu <habibutsu@gmail.com> - 2013-03-29 13:37 +0300
csiph-web