Re: empty clause of for loops

2016-03-21 Thread Rob Gaddi
Sven R. Kunze wrote: > On 16.03.2016 16:02, Tim Chase wrote: >> On 2016-03-16 15:29, Sven R. Kunze wrote: >>> I would re-use the "for-else" for this. Everything I thought I >>> could make use of the "-else" clause, I was disappointed I couldn't. >> Hmm...this must be a mind-set thing. I use the "

Re: empty clause of for loops

2016-03-20 Thread Ruud de Jong
Peter Otten schreef op 2016-03-16 13:57: If you don't like exceptions implement (or find) something like items = peek(items) if items.has_more(): # at least one item for item in items: ... else: # empty Only if such a function is used a lot or cannot be conceived without severe

Re: empty clause of for loops

2016-03-19 Thread Chris Angelico
On Sat, Mar 19, 2016 at 7:49 AM, Sven R. Kunze wrote: > On 18.03.2016 20:10, Palpandi wrote: >> >> You can do like this. >> >> if not my_iterable: >> >> for x in my_iterable: >> > > > Thanks for you help here, however as already pointed out, my_iterable is not > necessarily a list but

Re: empty clause of for loops

2016-03-19 Thread Peter Otten
alister wrote: > On Wed, 16 Mar 2016 11:47:31 +0100, Peter Otten wrote: >> 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 variabl

Re: empty clause of for loops

2016-03-19 Thread Random832
On Wed, Mar 16, 2016, at 13:01, Sven R. Kunze wrote: > On 16.03.2016 17:56, Sven R. Kunze wrote: > > On 16.03.2016 17:37, Random832 wrote: > >> for item in collection: > >> if good(item): > >>thing = item > >>break > >> else: > >> thing = default # or raise an exception, etc

Re: empty clause of for loops

2016-03-19 Thread Sven R. Kunze
On 16.03.2016 16:02, Tim Chase wrote: On 2016-03-16 15:29, Sven R. Kunze wrote: I would re-use the "for-else" for this. Everything I thought I could make use of the "-else" clause, I was disappointed I couldn't. Hmm...this must be a mind-set thing. I use the "else" clause with for/while loops

Re: empty clause of for loops

2016-03-19 Thread Sven R. Kunze
On 16.03.2016 18:08, Random832 wrote: Yeah, well, you can *almost* get there with: try: thing = next(item for item in collection if good(item)) except StopIteration: thing = default But the for/else thing seems like a more natural way to do it. Plus, this is a toy example, if the body

Re: empty clause of for loops

2016-03-19 Thread Tim Chase
On 2016-03-16 11:23, Sven R. Kunze wrote: > for x in my_iterable: > # do > empty: > # do something else > > What's the most Pythonic way of doing this? If you can len() on it, then the obvious way is if my_iterable: for x in my_iterable: do_something(x) else: somethin

Re: empty clause of for loops

2016-03-19 Thread Sven R. Kunze
On 16.03.2016 11:47, Peter Otten wrote: What would you expect? A keyword filling the missing functionality? Some Python magic, I haven't seen before. ;-) class Empty(Exception): pass ... def check_empty(items): ... items = iter(items) ... try: ... yield next(items) ...

Re: empty clause of for loops

2016-03-19 Thread Sven R. Kunze
On 18.03.2016 20:10, Palpandi wrote: You can do like this. if not my_iterable: for x in my_iterable: Thanks for you help here, however as already pointed out, my_iterable is not necessarily a list but more likely an exhaustible iterator/generator. Best, Sven -- https://mail.pyth

Re: empty clause of for loops

2016-03-19 Thread Peter Otten
André Roberge wrote: > On Wednesday, 16 March 2016 07:23:48 UTC-3, 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

Re: empty clause of for loops

2016-03-19 Thread Sven R. Kunze
On 16.03.2016 13:57, Peter Otten wrote: I'd put that the other way round: syntactical support for every pattern would make for a rather unwieldy language. You have to choose carefully, and this requirement could easily be fulfilled by a function, first in your personal toolbox, then in a public

Re: empty clause of for loops

2016-03-19 Thread Peter Otten
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

Re: empty clause of for loops

2016-03-19 Thread Peter Otten
Sven R. Kunze wrote: > On 16.03.2016 11:47, Peter Otten wrote: >> >> What would you expect? > > A keyword filling the missing functionality? Some Python magic, I > haven't seen before. ;-) > >> > class Empty(Exception): pass >> ... > def check_empty(items): >> ... items = iter(items)

Re: empty clause of for loops

2016-03-19 Thread Tim Chase
On 2016-03-16 15:29, Sven R. Kunze wrote: > I would re-use the "for-else" for this. Everything I thought I > could make use of the "-else" clause, I was disappointed I couldn't. Hmm...this must be a mind-set thing. I use the "else" clause with for/while loops fairly regularly and would be miffed

Re: empty clause of for loops

2016-03-19 Thread Sven R. Kunze
On 16.03.2016 17:37, Random832 wrote: On Wed, Mar 16, 2016, at 11:17, Sven R. Kunze wrote: I can imagine that. Could you describe the general use-case? From what I know, "else" is executed when you don't "break" the loop. When is this useful? for item in collection: if good(item): t

Re: empty clause of for loops

2016-03-19 Thread Sven R. Kunze
On 16.03.2016 13:08, Steven D'Aprano wrote: Doing what? What is the code supposed to do? What's "empty" mean as a keyword? If you explain what your friends wants, then perhaps we can suggest something. Otherwise we're just guessing. I can think of at least two different meanings: * run the "emp

Re: empty clause of for loops

2016-03-19 Thread alister
On Wed, 16 Mar 2016 13:45:53 +, 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:

Re: empty clause of for loops

2016-03-19 Thread Sven R. Kunze
On 16.03.2016 15:29, Sven R. Kunze wrote: On 16.03.2016 13:57, Peter Otten wrote: I'd put that the other way round: syntactical support for every pattern would make for a rather unwieldy language. You have to choose carefully, and this requirement could easily be fulfilled by a function, firs

Re: empty clause of for loops

2016-03-19 Thread Sven R. Kunze
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.

Re: empty clause of for loops

2016-03-19 Thread Sven R. Kunze
On 16.03.2016 14:09, Tim Chase wrote: If you can len() on it, then the obvious way is if my_iterable: for x in my_iterable: do_something(x) else: something_else() However, based on your follow-up that it's an exhaustible iterator rather than something you can len(), I'd u

Re: empty clause of for loops

2016-03-19 Thread Steven D'Aprano
On Wed, 16 Mar 2016 09:23 pm, 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? Doing what?

Re: empty clause of for loops

2016-03-19 Thread André Roberge
On Wednesday, 16 March 2016 07:23:48 UTC-3, 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?

Re: empty clause of for loops

2016-03-19 Thread Steven D'Aprano
On Wed, 16 Mar 2016 11:41 pm, André Roberge wrote: > for x in my_iterable: ># do something > > if not my_iterable: ># do something else Doesn't work for iterators. Iterators are (in general) always truthy, whether they are empty or not. -- Steven -- https://mail.python.org/mailman

Re: empty clause of for loops

2016-03-19 Thread Terry Reedy
On 3/16/2016 11:17 AM, Sven R. Kunze wrote: On 16.03.2016 16:02, Tim Chase wrote: Does it annoy me when I have to work in other languages that lack Python's {for/while}/else functionality? You bet. I can imagine that. Could you describe the general use-case? From what I know, "else" is exec

Re: empty clause of for loops

2016-03-19 Thread Tim Chase
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: items = ["Something here", N

Re: empty clause of for loops

2016-03-19 Thread Mark Lawrence
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

Re: empty clause of for loops

2016-03-19 Thread Palpandi
On Wednesday, March 16, 2016 at 3:53:48 PM UTC+5:30, 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 doi

Re: empty clause of for loops

2016-03-19 Thread Steven D'Aprano
On Thu, 17 Mar 2016 05:05 am, Sven R. Kunze wrote: > What I don't understand is why Python features "if break, then no else > clause", but "if empty, then empty clause". > > I found this excellent post: > https://shahriar.svbtle.com/pythons-else-clause-in-loops That post describes the motivating

Re: empty clause of for loops

2016-03-19 Thread alister
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

Re: empty clause of for loops

2016-03-19 Thread Sven R. Kunze
On 16.03.2016 17:20, Terry Reedy wrote: On 3/16/2016 11:17 AM, Sven R. Kunze wrote: On 16.03.2016 16:02, Tim Chase wrote: Does it annoy me when I have to work in other languages that lack Python's {for/while}/else functionality? You bet. I can imagine that. Could you describe the general

Re: empty clause of for loops

2016-03-18 Thread Sven R. Kunze
On 16.03.2016 17:56, Sven R. Kunze wrote: On 16.03.2016 17:37, Random832 wrote: On Wed, Mar 16, 2016, at 11:17, Sven R. Kunze wrote: I can imagine that. Could you describe the general use-case? From what I know, "else" is executed when you don't "break" the loop. When is this useful? for ite

Re: empty clause of for loops

2016-03-18 Thread Random832
On Wed, Mar 16, 2016, at 11:17, Sven R. Kunze wrote: > I can imagine that. Could you describe the general use-case? From what I > know, "else" is executed when you don't "break" the loop. When is this > useful? for item in collection: if good(item): thing = item break else: th

Re: empty clause of for loops

2016-03-18 Thread Sven R. Kunze
On 17.03.2016 01:27, Steven D'Aprano wrote: That post describes the motivating use-case for the introduction of "if...else", and why break skips the "else" clause: for x in data: if meets_condition(x): break else: # raise error or do additional processing It might help to r

Re: empty clause of for loops

2016-03-16 Thread Peter Otten
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(Exc

Re: empty clause of for loops

2016-03-16 Thread Sven R. Kunze
On 16.03.2016 11:28, Joaquin Alzola wrote: If len(my_iterable) is not 0: for x in my_iterable: # do else: # do something else I am sorry, I should have been more precise here. my_iterable is an iterator that's exhausted after a complete iteration and cannot be restored. I

RE: empty clause of for loops

2016-03-16 Thread Joaquin Alzola
Behalf Of Sven R. Kunze Sent: 16 March 2016 10:23 To: Python List Subject: empty clause of for loops 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 w

empty clause of for loops

2016-03-16 Thread Sven R. Kunze
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? Best, Sven -- https://mail.python.org/mailman/listinfo/python-list