Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #65324

Re: generator slides review

Path csiph.com!usenet.pasdenom.info!weretis.net!feeder1.news.weretis.net!feeder.erje.net!eu.feeder.erje.net!newsfeed.xs4all.nl!newsfeed3a.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail
Return-Path <python-python-list@m.gmane.org>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.000
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; 'static': 0.04; 'explicitly': 0.05; '---------': 0.07; 'expressions': 0.07; '[1,': 0.09; 'builtin': 0.09; 'generators': 0.09; 'logic': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'to)': 0.09; 'python': 0.11; 'def': 0.12; 'jan': 0.12; 'anyway': 0.14; '(should': 0.16; '-------': 0.16; '3):': 0.16; 'buggy': 0.16; 'check.': 0.16; 'comments:': 0.16; 'creation.': 0.16; 'defined.': 0.16; 'entry.': 0.16; 'evaluates': 0.16; 'exception?': 0.16; 'iterator': 0.16; 'iterator,': 0.16; 'iterators': 0.16; 'iterators,': 0.16; 'questioned': 0.16; 'received:80.91.229.3': 0.16; 'received:plane.gmane.org': 0.16; 'reedy': 0.16; 'stop):': 0.16; 'subject:generator': 0.16; 'supplement': 0.16; 'term.': 0.16; 'think?': 0.16; 'valid.': 0.16; 'valueerror': 0.16; 'appropriate': 0.16; 'wrote:': 0.18; 'items.': 0.19; 'normally': 0.19; 'work,': 0.20; '>>>': 0.22; 'example': 0.22; 'saying': 0.22; 'print': 0.22; 'header:User-Agent:1': 0.23; 'pointer': 0.24; 'non': 0.24; 'define': 0.26; 'defined': 0.27; 'header:X -Complaints-To:1': 0.27; 'header:In-Reply-To:1': 0.27; 'fixed': 0.29; 'leave': 0.29; 'am,': 0.29; 'raise': 0.29; 'label': 0.30; 'work.': 0.31; 'code': 0.31; 'lines': 0.31; "skip:' 10": 0.31; 'assert': 0.31; 'away.': 0.31; 'exceptions': 0.31; 'class': 0.32; 'probably': 0.32; 'skip:_ 10': 0.34; 'maybe': 0.34; 'could': 0.34; 'classes': 0.35; 'knows': 0.35; 'something': 0.35; 'definition': 0.35; 'more,': 0.35; 'but': 0.35; 'there': 0.35; 'version': 0.36; 'yield': 0.36; 'method': 0.36; 'thanks': 0.36; 'should': 0.36; 'wrong': 0.37; 'list': 0.37; 'sometimes': 0.38; 'skip:[ 10': 0.38; 'to:addr:python-list': 0.38; 'explain': 0.39; 'bad': 0.39; 'generating': 0.39; 'sure': 0.39; 'to:addr:python.org': 0.39; 'received:org': 0.40; 'skip:x 10': 0.40; 'how': 0.40; 'expression': 0.60; 'range': 0.61; 'received:173': 0.61; 'first': 0.61; 'address': 0.63; 'show': 0.63; 'such': 0.63; 'different': 0.65; 'specialized': 0.65; 'here': 0.66; 'believe': 0.68; 'yes': 0.68; 'optimized': 0.68; 'wish': 0.70; 'evaluate': 0.72; '100': 0.79; '(better': 0.84; 'andrea': 0.84; 'commenting': 0.84; 'confusing': 0.84; 'received:fios.verizon.net': 0.84; 'seldom': 0.84; 'slides,': 0.84
X-Injected-Via-Gmane http://gmane.org/
To python-list@python.org
From Terry Reedy <tjreedy@udel.edu>
Subject Re: generator slides review
Date Mon, 03 Feb 2014 02:17:51 -0500
References <CAF_E5JYWZXkq98ri63BMQst3kpPVhO1K5xQbQ0ZHCuRTYZXvUA@mail.gmail.com> <lck5pr$reg$1@ger.gmane.org> <CAF_E5JbHE_RC79M09WDWhB5PYJeo_qs-Z4yWZQCqvRQJLpdm3Q@mail.gmail.com>
Mime-Version 1.0
Content-Type text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding 7bit
X-Gmane-NNTP-Posting-Host pool-173-75-254-207.phlapa.fios.verizon.net
User-Agent Mozilla/5.0 (Windows NT 6.1; WOW64; rv:24.0) Gecko/20100101 Thunderbird/24.2.0
In-Reply-To <CAF_E5JbHE_RC79M09WDWhB5PYJeo_qs-Z4yWZQCqvRQJLpdm3Q@mail.gmail.com>
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 <https://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 <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.6329.1391411892.18130.python-list@python.org> (permalink)
Lines 128
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1391411892 news.xs4all.nl 2975 [2001:888:2000:d::a6]:47911
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:65324

Show key headers only | View raw


On 2/2/2014 5:40 AM, andrea crotti wrote:
> 2014-02-02 Terry Reedy <tjreedy@udel.edu>:
>> On 2/1/2014 9:12 AM, andrea crotti wrote:
>>
>> Comments:
>>
>> The use is assert in the first slide seem bad in a couple of different
>> respects.
>>
>
> Why is it bad? It's probably not necessary but since we ask for a
> range it might be good to check if the range is valid.
> Maybe I should raise ValueError instead for a better exception?

In general, use assert (== AssertionError) to check program logic 
(should never raise). Remember that assert can be optimized away. Use 
other exceptions to check user behavior. So I believe that ValueError is 
appropriate here. I think I also questioned the particular check.

>> The use of 'gen_even' before it is defined.
>>
>
> Well this is because l'm saying that I wish I had something like this,
> which I define just after. It might be confusing if it's not defined
> but I thought it's nice to say what I would like to do and then
> actually define it, what do you think?

In commenting on the slides, I did not know what you would say to 
supplement them.

>> A generator expression evaluates (better than 'yields') to a generator, not
>> just an iterator.
>>
>
> Ok thanks fixed
>
>> The definition of 'generator' copies the wrong and confused glossary entry.
>> Generator functions return generators, which are iterators with extra
>> behavior.
>>
>
> I understood instead that it was the opposite, a generator is a
> specialized iterator,

'Generator functions', which you labeled 'generators', are functions, 
not iterators. The generators they return (and the generators that 
generator expressions evaluate to) are iterators, and more.

 >>> type(a for a in 'abc')
<class 'generator'>

I am not sure whether 'specialized' or 'generalized' is the better term.


>> I would leave out For loop(2). The old pseudo-getitem iterator protocol is
>> seldom explicitly used any more, in the say you showed.

/say/way/

> This was mainly to explain how something like
> for el in [1, 2, 3]:
>      print(el)
>
> can work,

But it is no longer has that *does* work. All the builtin xyz collection 
classes have a corresponding xyz_iterator class with a __next__ method 
that knows how to sequentially access collection items. We do not 
normally see or think about them, but they are there working for us 
every time we do 'for item in xyz_instance:'

 >>> [].__iter__()
<list_iterator object at 0x00000000035096A0>

In Python one could write the following:

class list_iterator:
   def __init__(self, baselist):
     self.baselist = baselist
     self.index = -1  # see __next__ for why
   def __iter__(self):
     return self
   def __next__(self):
     self.index += 1
     return self.baselist[self.index]

but the C version should use a static pointer into the object address array,

>> 'Lazyness drawbacks' overflow_list is bizarre and useless.  overflow_gen is
>> bizarre and buggy. If you are intentionally writing buggy code to make a
>> point, label it as such on the slide.
>>
>
> Yes this is intentionally buggy. The thing is that I wanted to show
> that sometimes generating things makes it harder to debug, and delays
> some errors, which are anyway there but would come up immediately in
> case of a list creation.
> I could not find a better non artificial example for this, any
> suggestion is welcome..

slide 1
---------
def recip_list(start, stop):
   lis []
   for i range(start, stop):
     list.append(1/i)
   return lis

for x in recip_list(-100, 3):  # fail here
   print x

<immediate traceback that include the for line>

slide 2
-------
def recip_gen(start, stop):
   for i in range(start, stop):
     yield 1/i


for x in recip_gen(-100, 3):
   print x  # fail here after printing 100 lines
...
<delayed traceback that omits for line with args that caused problem>

-- 
Terry Jan Reedy

Back to comp.lang.python | Previous | Next | Find similar | Unroll thread


Thread

Re: generator slides review Terry Reedy <tjreedy@udel.edu> - 2014-02-03 02:17 -0500

csiph-web