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


Groups > comp.lang.python > #105072

Re: empty clause of for loops

From Peter Otten <__peter__@web.de>
Newsgroups comp.lang.python
Subject Re: empty clause of for loops
Date 2016-03-17 09:23 +0100
Organization None
Message-ID <mailman.259.1458203026.12893.python-list@python.org> (permalink)
References <56E93413.6090108@mail.de> <mailman.189.1458125264.12893.python-list@python.org> <E9dGy.48569$4l5.10304@fx37.am4> <ncbvig$8bl$1@ger.gmane.org> <20160316143934.5ca70b77@bigbox.christie.dr>

Show all headers | View raw


Tim Chase wrote:

> On 2016-03-16 16:53, Peter Otten wrote:
>> > item=None
>> > for item in items:
>> >         #do stuff
>>   if item is None:
>> >         #do something else
>> 
>> I like that better now I see it.
> 
> The only problem with that is if your iterable returns None as the
> last item:

I was aware of that. In practice I'd ensure the stronger "sentinel must not 
occur in the iterable":

>   items = ["Something here", None]
>   item = None
>   for item in items:
      assert item is not None
>     print(repr(item))
>   if item is None:
>     print("Empty iterable") # wait, no it's not!
> 
> You'd have to use a sentinel like Ruud mentions further up-list:
> 
>   x = sentinal = object()
>   for x in sequence:
>     print(repr(x))
>   if x is sentinal:
>     print("Empty iterable")
> 
> -tkc

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


Thread

Re: empty clause of for loops Peter Otten <__peter__@web.de> - 2016-03-16 11:47 +0100
  Re: empty clause of for loops alister <alister.ware@ntlworld.com> - 2016-03-16 13:25 +0000
    Re: empty clause of for loops Mark Lawrence <breamoreboy@yahoo.co.uk> - 2016-03-16 13:45 +0000
      Re: empty clause of for loops alister <alister.ware@ntlworld.com> - 2016-03-16 13:58 +0000
        Re: empty clause of for loops "Sven R. Kunze" <srkunze@mail.de> - 2016-03-16 15:36 +0100
    Re: empty clause of for loops Peter Otten <__peter__@web.de> - 2016-03-17 09:23 +0100

csiph-web