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


Groups > comp.lang.python > #105002 > unrolled thread

Re: empty clause of for loops

Started byPeter Otten <__peter__@web.de>
First post2016-03-16 11:47 +0100
Last post2016-03-17 09:23 +0100
Articles 6 — 4 participants

Back to article view | Back to comp.lang.python

This discussion starts older than the indexed window; earlier articles aren't shown. The article labeled Started by below is the oldest one visible, not the original post.


Contents

  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

#105002 — Re: empty clause of for loops

FromPeter Otten <__peter__@web.de>
Date2016-03-16 11:47 +0100
SubjectRe: empty clause of for loops
Message-ID<mailman.189.1458125264.12893.python-list@python.org>
Sven R. Kunze wrote:

> Hi,
> 
> a colleague of mine (I write this mail because I am on the list) has the
> following issue:
> 
> 
> for x in my_iterable:
>      # do
> empty:
>      # do something else
> 
> 
> What's the most Pythonic way of doing this?

What would you expect?

>>> class Empty(Exception): pass
... 
>>> def check_empty(items):
...     items = iter(items)
...     try:
...         yield next(items)
...     except StopIteration:
...         raise Empty
...     yield from items
... 
>>> try:
...    for item in check_empty("abc"): print(item)
... except Empty: print("oops")
... 
a
b
c
>>> try:
...    for item in check_empty(""): print(item)
... except Empty: print("oops")
... 
oops

I'm kidding, of course. Keep it simple and use a flag like you would in any 
other language:

empty = True:
for item in items:
    empty = False
    ...
if empty:
    ...

[toc] | [next] | [standalone]


#105021

Fromalister <alister.ware@ntlworld.com>
Date2016-03-16 13:25 +0000
Message-ID<E9dGy.48569$4l5.10304@fx37.am4>
In reply to#105002
On Wed, 16 Mar 2016 11:47:31 +0100, Peter Otten wrote:

> Sven R. Kunze wrote:
> 
>> Hi,
>> 
>> a colleague of mine (I write this mail because I am on the list) has
>> the following issue:
>> 
>> 
>> for x in my_iterable:
>>      # do
>> empty:
>>      # do something else
>> 
>> 
>> What's the most Pythonic way of doing this?
> 
> What would you expect?
> 
>>>> class Empty(Exception): pass
> ...
>>>> def check_empty(items):
> ...     items = iter(items)
> ...     try:
> ...         yield next(items)
> ...     except StopIteration:
> ...         raise Empty ...     yield from items ...
>>>> try:
> ...    for item in check_empty("abc"): print(item)
> ... except Empty: print("oops")
> ...
> a
> b
> c
>>>> try:
> ...    for item in check_empty(""): print(item)
> ... except Empty: print("oops")
> ...
> oops
> 
> I'm kidding, of course. Keep it simple and use a flag like you would in
> any other language:
> 
> empty = True:
> for item in items:
>     empty = False ...
> if empty:
>     ...

or even use the loop variable as the flag

item=None
for item in items:
	#do stuff
if ex is None:
	#do something else




-- 
Love means never having to say you're sorry.
		-- Eric Segal, "Love Story"

That's the most ridiculous thing I've ever heard.
		-- Ryan O'Neill, "What's Up Doc?"

[toc] | [prev] | [next] | [standalone]


#105026

FromMark Lawrence <breamoreboy@yahoo.co.uk>
Date2016-03-16 13:45 +0000
Message-ID<mailman.203.1458136003.12893.python-list@python.org>
In reply to#105021
On 16/03/2016 13:25, alister wrote:
> On Wed, 16 Mar 2016 11:47:31 +0100, Peter Otten wrote:
>
>> Sven R. Kunze wrote:
>>
>>> Hi,
>>>
>>> a colleague of mine (I write this mail because I am on the list) has
>>> the following issue:
>>>
>>>
>>> for x in my_iterable:
>>>       # do
>>> empty:
>>>       # do something else
>>>
>>>
>>> What's the most Pythonic way of doing this?
>>
>> What would you expect?
>>
>>>>> class Empty(Exception): pass
>> ...
>>>>> def check_empty(items):
>> ...     items = iter(items)
>> ...     try:
>> ...         yield next(items)
>> ...     except StopIteration:
>> ...         raise Empty ...     yield from items ...
>>>>> try:
>> ...    for item in check_empty("abc"): print(item)
>> ... except Empty: print("oops")
>> ...
>> a
>> b
>> c
>>>>> try:
>> ...    for item in check_empty(""): print(item)
>> ... except Empty: print("oops")
>> ...
>> oops
>>
>> I'm kidding, of course. Keep it simple and use a flag like you would in
>> any other language:
>>
>> empty = True:
>> for item in items:
>>      empty = False ...
>> if empty:
>>      ...
>
> or even use the loop variable as the flag
>
> item=None
> for item in items:
> 	#do stuff
> if ex is None:
> 	#do something else
>

Did you test this? :)


-- 
My fellow Pythonistas, ask not what our language can do for you, ask
what you can do for our language.

Mark Lawrence

[toc] | [prev] | [next] | [standalone]


#105028

Fromalister <alister.ware@ntlworld.com>
Date2016-03-16 13:58 +0000
Message-ID<YDdGy.48571$4l5.24270@fx37.am4>
In reply to#105026
On Wed, 16 Mar 2016 13:45:53 +0000, Mark Lawrence wrote:

> On 16/03/2016 13:25, alister wrote:
>> On Wed, 16 Mar 2016 11:47:31 +0100, Peter Otten wrote:
>>
>>> Sven R. Kunze wrote:
>>>
>>>> Hi,
>>>>
>>>> a colleague of mine (I write this mail because I am on the list) has
>>>> the following issue:
>>>>
>>>>
>>>> for x in my_iterable:
>>>>       # do
>>>> empty:
>>>>       # do something else
>>>>
>>>>
>>>> What's the most Pythonic way of doing this?
>>>
>>> What would you expect?
>>>
>>>>>> class Empty(Exception): pass
>>> ...
>>>>>> def check_empty(items):
>>> ...     items = iter(items)
>>> ...     try:
>>> ...         yield next(items)
>>> ...     except StopIteration:
>>> ...         raise Empty ...     yield from items ...
>>>>>> try:
>>> ...    for item in check_empty("abc"): print(item)
>>> ... except Empty: print("oops")
>>> ...
>>> a
>>> b
>>> c
>>>>>> try:
>>> ...    for item in check_empty(""): print(item)
>>> ... except Empty: print("oops")
>>> ...
>>> oops
>>>
>>> I'm kidding, of course. Keep it simple and use a flag like you would
>>> in any other language:
>>>
>>> empty = True:
>>> for item in items:
>>>      empty = False ...
>>> if empty:
>>>      ...
>>
>> or even use the loop variable as the flag
>>
>> item=None for item in items:
>> 	#do stuff
>> if ex is None:
>> 	#do something else
>>
>>
> Did you test this? :)

no , i just typed it, while trying to hold a conversation with swmbo :-(

apologies to the op if e could not see where i was intending to go with 
this.







-- 
Tip the world over on its side and everything loose will land in Los 
Angeles.
		-- Frank Lloyd Wright

[toc] | [prev] | [next] | [standalone]


#105037

From"Sven R. Kunze" <srkunze@mail.de>
Date2016-03-16 15:36 +0100
Message-ID<mailman.210.1458138972.12893.python-list@python.org>
In reply to#105028
On 16.03.2016 14:58, alister wrote:
> no , i just typed it, while trying to hold a conversation with swmbo 
> :-( apologies to the op if e could not see where i was intending to go 
> with this. 

No problem, I perform quite well at guessing folk's intention.

So, yes, I can extrapolate what you meant. Thanks. :)

Best,
Sven

[toc] | [prev] | [next] | [standalone]


#105072

FromPeter Otten <__peter__@web.de>
Date2016-03-17 09:23 +0100
Message-ID<mailman.259.1458203026.12893.python-list@python.org>
In reply to#105021
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

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web