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


Groups > comp.lang.python > #105028

Re: empty clause of for loops

From alister <alister.ware@ntlworld.com>
Subject Re: empty clause of for loops
Newsgroups comp.lang.python
References <56E93413.6090108@mail.de> <mailman.189.1458125264.12893.python-list@python.org> <E9dGy.48569$4l5.10304@fx37.am4> <mailman.203.1458136003.12893.python-list@python.org>
Message-ID <YDdGy.48571$4l5.24270@fx37.am4> (permalink)
Organization virginmedia.com
Date 2016-03-16 13:58 +0000

Show all headers | View raw


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

Back to comp.lang.python | Previous | NextPrevious in thread | Next 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