Re: If/then style question

2010-12-21 Thread Francesco
I'd bet you would stress your point Steven! But you don't need to persuade me, I do already agree. I just meant to say that, when the advantage is little, there's no need to rewrite a working function. And that with modern CPUs, if tests take so little time, that even some redundant one is not

Re: If/then style question

2010-12-19 Thread Steven D'Aprano
On Sat, 18 Dec 2010 19:59:45 -0800, Carl Banks wrote: > On Dec 17, 12:23 am, Steven D'Aprano +comp.lang.pyt...@pearwood.info> wrote: >> On Thu, 16 Dec 2010 20:32:29 -0800, Carl Banks wrote: >> > Even without the cleanup issue, sometimes you want to edit a function >> > to affect all return values

Re: If/then style question

2010-12-18 Thread Carl Banks
On Dec 17, 12:23 am, Steven D'Aprano wrote: > On Thu, 16 Dec 2010 20:32:29 -0800, Carl Banks wrote: > > Even without the cleanup issue, sometimes you want to edit a function to > > affect all return values somehow.  If you have a single exit point you > > just make the change there; if you have mu

Re: If/then style question

2010-12-18 Thread Steven D'Aprano
On Sat, 18 Dec 2010 12:29:31 +0100, Francesco wrote: [...] > I agree to your point, but I'm afraid you chose a wrong example (AFAIK, > and that's not much). Sure, the second version of function(arg) is much > more readable, but why do you think the first one would do "*lots* of > unnecessary work

Re: If/then style question

2010-12-18 Thread Francesco
On 17/12/2010 0.51, Steven D'Aprano wrote: Don't get me wrong... spaghetti code is*bad*. But there are other ways of writing bad code too, and hanging around inside a function long after you've finished is also bad: def function(arg): done = False do_something() if some_condition:

Re: If/then style question

2010-12-17 Thread Steven D'Aprano
On Fri, 17 Dec 2010 17:26:08 +, Grant Edwards wrote: > Give me code that's easy-to-read and doesn't work rather code that works > and can't be read any day. Well, in that case, you'll love my new operating system, written in 100% pure Python: [start code] print("this is an operating system"

Re: If/then style question

2010-12-17 Thread Steven D'Aprano
On Fri, 17 Dec 2010 10:53:45 -0500, Steve Holden wrote about for...else: > This construct appears to be unpopular in actual use, and when it comes > up in classes and seminars there is always interesting debate as people > discuss potential uses and realise there are useful applications. Yes, I f

Re: If/then style question

2010-12-17 Thread Kev Dwyer
On Thu, 16 Dec 2010 21:49:07 +, John Gordon wrote: > (This is mostly a style question, and perhaps one that has already been > discussed elsewhere. If so, a pointer to that discussion will be > appreciated!) > > When I started learning Python, I wrote a lot of methods that looked > like this

RE: If/then style question

2010-12-17 Thread Rob Richardson
-Original Message- You have outlined what happens when cond1 and cond2 both evaluate to True -- what happens if, say, cond2 evaluates to False? - I reply And the light goes on! (And palm strikes forehead.) I was thinking that the error we were processing was raised by

Re: If/then style question

2010-12-17 Thread Grant Edwards
On 2010-12-16, Steven D'Aprano wrote: > On Thu, 16 Dec 2010 21:49:07 +, John Gordon wrote: > >> (This is mostly a style question, and perhaps one that has already been >> discussed elsewhere. If so, a pointer to that discussion will be >> appreciated!) >> >> When I started learning Python, I

Re: If/then style question

2010-12-17 Thread Grant Edwards
On 2010-12-16, Stefan Sonnenberg-Carstens wrote: > The advantage in latter case is fewer operations, because you can > skip the assignments, and it is more readable. > > The "one entry, one exit" is an advice. Not a law. > Your code is OK. > > As long as it works ;-) Even that last bit isn't th

Re: If/then style question

2010-12-17 Thread python
> I now remember this idiom as the "break else" construct: either the loop > breaks, or the "else:" suite is executed. A perfect description. Malcolm -- http://mail.python.org/mailman/listinfo/python-list

Re: If/then style question

2010-12-17 Thread Arnaud Delobelle
Tim Golden writes: > On 17/12/2010 15:53, Steve Holden wrote: > > [... snip example of for-else ...] > >> This construct appears to be unpopular in actual use, and when it comes >> up in classes and seminars there is always interesting debate as people >> discuss potential uses and realise there

Re: If/then style question

2010-12-17 Thread Mark Wooding
Steve Holden writes: > I think the choice of keyword is probably not Guido's crowning > language achievement, I remember the behaviour by considering a typical application: for thing in things: if shinyp(thing): break else: raise DullError, 'nothi

Re: If/then style question

2010-12-17 Thread Steve Holden
On 12/17/2010 11:13 AM, Tim Golden wrote: > On 17/12/2010 15:53, Steve Holden wrote: > > [... snip example of for-else ...] > >> This construct appears to be unpopular in actual use, and when it comes >> up in classes and seminars there is always interesting debate as people >> discuss potential

Re: If/then style question

2010-12-17 Thread Ethan Furman
Rob Richardson wrote: My thanks for pointing out the existence of the else: suite in the for statement. However, I remain confused. For reference, here's the original code: def myMethod(): for condition, exitCode in [ (cond1, 'error1'), (cond2, 'very bad error'),

Re: If/then style question

2010-12-17 Thread Paul Rubin
Jean-Michel Pichavant writes: > What about, > > def myMethod(): >for condition, exitCode in [ >(cond1, 'error1'), >(cond2, 'very bad error'), >]: >if not condition: >break >else: > do_some_usefull_stuff() # executed only if the we never

Re: If/then style question

2010-12-17 Thread Tim Golden
On 17/12/2010 15:53, Steve Holden wrote: [... snip example of for-else ...] This construct appears to be unpopular in actual use, and when it comes up in classes and seminars there is always interesting debate as people discuss potential uses and realise there are useful applications. I use t

RE: If/then style question

2010-12-17 Thread Rob Richardson
My thanks for pointing out the existence of the else: suite in the for statement. However, I remain confused. For reference, here's the original code: > def myMethod(): > for condition, exitCode in [ > (cond1, 'error1'), > (cond2, 'very bad error'), > ]: >

Re: If/then style question

2010-12-17 Thread Steve Holden
On 12/17/2010 9:38 AM, Steven D'Aprano wrote: > On Fri, 17 Dec 2010 09:09:49 -0500, Rob Richardson wrote: > > >> First, just to clarify, I don't think the indentation I saw was what was >> originally posted. The "else" must be indented to match the "if", and >> the two statements under "else" ar

Re: If/then style question

2010-12-17 Thread David Robinow
On Thu, Dec 16, 2010 at 6:51 PM, Steven D'Aprano wrote: ... > Functions always have one entry. The only way to have multiple entry > points is if the language allows you to GOTO into the middle of a > function, and Python sensibly does not allow this. The "one entry, one > exit" rule comes from th

Re: If/then style question

2010-12-17 Thread Jean-Michel Pichavant
Rob Richardson wrote: -Original Message- What about, def myMethod(): for condition, exitCode in [ (cond1, 'error1'), (cond2, 'very bad error'), ]: if not condition: break else: do_some_usefull_stuff() # executed only if the

Re: If/then style question

2010-12-17 Thread Steven D'Aprano
On Fri, 17 Dec 2010 09:09:49 -0500, Rob Richardson wrote: > First, just to clarify, I don't think the indentation I saw was what was > originally posted. The "else" must be indented to match the "if", and > the two statements under "else" are in the else block. The return > statement is indente

RE: If/then style question

2010-12-17 Thread Rob Richardson
-Original Message- What about, def myMethod(): for condition, exitCode in [ (cond1, 'error1'), (cond2, 'very bad error'), ]: if not condition: break else: do_some_usefull_stuff() # executed only if the we never hit the break

Re: If/then style question

2010-12-17 Thread Jean-Michel Pichavant
John Gordon wrote: (This is mostly a style question, and perhaps one that has already been discussed elsewhere. If so, a pointer to that discussion will be appreciated!) When I started learning Python, I wrote a lot of methods that looked like this: def myMethod(self, arg1, arg2): if s

Re: If/then style question

2010-12-17 Thread Steven D'Aprano
On Thu, 16 Dec 2010 20:32:29 -0800, Carl Banks wrote: > Even without the cleanup issue, sometimes you want to edit a function to > affect all return values somehow. If you have a single exit point you > just make the change there; if you have mulitple you have to hunt them > down and change all o

Re: If/then style question

2010-12-16 Thread Steve Holden
On 12/16/2010 11:32 PM, Carl Banks wrote: > On Dec 16, 2:56 pm, Ryan Kelly wrote: >> On Thu, 2010-12-16 at 21:49 +, John Gordon wrote: >>> (This is mostly a style question, and perhaps one that has already been >>> discussed elsewhere. If so, a pointer to that discussion will be >>> appreciat

Re: If/then style question

2010-12-16 Thread Carl Banks
On Dec 16, 2:56 pm, Ryan Kelly wrote: > On Thu, 2010-12-16 at 21:49 +, John Gordon wrote: > > (This is mostly a style question, and perhaps one that has already been > > discussed elsewhere.  If so, a pointer to that discussion will be > > appreciated!) > > > When I started learning Python, I

Re: If/then style question

2010-12-16 Thread Joel Koltner
"Steven D'Aprano" wrote in message news:4d0aa5e7$0$29997$c3e8da3$54964...@news.astraweb.com... It doesn't look like you were learning Python. It looks like you were learning C with Python syntax :( True, although in many cases one has to interface to legacy C code where it'd be rather more co

Re: If/then style question

2010-12-16 Thread alex23
John Gordon wrote: > But lately I've been preferring this style: > >   def myMethod(self, arg1, arg2): > >     if some_bad_condition: >       return bad1 > >     elif some_other_bad_condition: >       return bad2 > >     elif yet_another_bad_condition: >       return bad3 > >     do_some_useful_st

Re: If/then style question

2010-12-16 Thread Steven D'Aprano
On Thu, 16 Dec 2010 21:49:07 +, John Gordon wrote: > (This is mostly a style question, and perhaps one that has already been > discussed elsewhere. If so, a pointer to that discussion will be > appreciated!) > > When I started learning Python, I wrote a lot of methods that looked > like this

Re: If/then style question

2010-12-16 Thread Ian Kelly
On Thu, Dec 16, 2010 at 3:41 PM, Stefan Sonnenberg-Carstens wrote: > return [x for x,y in > ((bad1,some_bad_condition),(bad2,some_other_bad_condition),(bad3,yet_another_bad_condition),(good1,do_some_useful_stuff() > or True)) if x][0] This doesn't work. do_some_usefull_stuff() gets called during

Re: If/then style question

2010-12-16 Thread Ryan Kelly
On Thu, 2010-12-16 at 21:49 +, John Gordon wrote: > (This is mostly a style question, and perhaps one that has already been > discussed elsewhere. If so, a pointer to that discussion will be > appreciated!) > > When I started learning Python, I wrote a lot of methods that looked like > this:

Re: If/then style question

2010-12-16 Thread Stefan Sonnenberg-Carstens
Am 16.12.2010 22:49, schrieb John Gordon: (This is mostly a style question, and perhaps one that has already been discussed elsewhere. If so, a pointer to that discussion will be appreciated!) When I started learning Python, I wrote a lot of methods that looked like this: def myMethod(self

Re: If/then style question

2010-12-16 Thread Grant Edwards
On 2010-12-16, John Gordon wrote: > (This is mostly a style question, and perhaps one that has already been > discussed elsewhere. If so, a pointer to that discussion will be > appreciated!) > > When I started learning Python, I wrote a lot of methods that looked like > this: > > > def myMethod

Re: If/then style question

2010-12-16 Thread Tim Harig
On 2010-12-16, John Gordon wrote: > I like this style more, mostly because it eliminates a lot of indentation. > > However I recall one of my college CS courses stating that "one entry, > one exit" was a good way to write code, and this style has lots of exits. So, take the good intentation from

Re: If/then style question

2010-12-16 Thread Ethan Furman
John Gordon wrote: (This is mostly a style question, and perhaps one that has already been discussed elsewhere. If so, a pointer to that discussion will be appreciated!) When I started learning Python, I wrote a lot of methods that looked like this: def myMethod(self, arg1, arg2): if so