Liam Clarke said unto the world upon 2004-12-16 02:05:
Alright, so that was a quick example, but....


return not x % 2


A light dawns.

On Thu, 16 Dec 2004 15:58:38 +0900, Guillermo Fernandez Castellanos
<[EMAIL PROTECTED]> wrote:

Well...


I find multiple returns to be rather useful
def isOdd(x):
   if not x % 2: return False
   return True

I find this easier though...

def isOdd(x):
   return not x % 2

Enjoy,

Guille

Hi Liam, Guille, and all,

Liam, the relationship between the code you put up and the suggestion Guille made closely parallel the my code from Marc's check_range thread and the suggestion DogWalker made. Thinking that our common slip in these two chucks might be more common in my code than I can currently recognize is what moved me to start the present thread.

If my original bit of code, the structure was like:

output_value = False
if condition:
    output_value = True
return output_value

Mine would be like yours if transformed to:

if condition:
    return True
return False

It seems to me that the two structures are inter-substitutable without difference to outcome (speed, readability, and other 'pragmatics' aside -- my structure will be slower for the assignments, for instance).

What makes me lean toward mine still? I'd encountered a general injunction to avoid multiple exit point somewhere on the wild web (cannot recall where). At the time, it didn't make sense to me. But, as I posted earlier today in reply to Kent, I was burned by multiple exits a while ago. (To be fair, by multiple exits and overlong functions which obscured the multiplicity from me -- stupid low res monitor!)

The best discussion I found with a quick google was <http://c2.com/cgi/wiki?SingleFunctionExitPoint>. Much of the anti-multiple-exits rational there only applies to languages like C that make the coder manage the memory. But "Having MultipleReturns is the 21 century equivalent of GotoConsideredHarmful" resonates with me. The idea is that multiple returns, particularly when separated by numerous lines of code, makes for spaghetti code.

('Goto Considered Harmful' is a famous short and fairly accessible paper [no math or code] by Dijkstra arguing that the use of Goto's makes code hard to follow <http://www.acm.org/classics/oct95/>. When I first started with Python, I'd not programmed in a decade or so after some highschool BASIC. My first reaction was "where's the bleedin' goto?". Once I coded some and noticed substantially less frustration than I recall from managing my gotos, I learned to appreciate that there wasn't a goto after all.)

Anyway, that's plenty of ramble for ce soir.

But, Liam, this is the first post I've made in your direction since I read you mention it: congrats for your new son!

Best to all,

Brian vdB
_______________________________________________
Tutor maillist  -  [EMAIL PROTECTED]
http://mail.python.org/mailman/listinfo/tutor

Reply via email to