How to detect the last element in a for loop

Tom Verbeure tom.verbeure at verizon.no.sp.am.net
Sun Jul 28 13:23:57 EDT 2002


Roy Smith wrote:

> Tom Verbeure <tom.verbeure at verizon.no.sp.am.net> wrote:
>> say, 10 lines of code. Too much to duplicate it outside the loop, not
>> enough for a separate function...
> 
> Why is 10 lines of code not enough for a separate function?  I use
> functions to group code into conceptual blocks that can be thought about
> and understood as a unit.  Sometimes that block is 100 lines of code,
> sometimes it's 3 or 4 lines.  I've even written one-line functions.
> Whatever makes sense to factor out as an atomic unit.  Line count has
> little to do with it.

Ha! Now we are talking style! :-)
Yes, that's, of course, an option also, but we are moving away from the 
original question and I feel guilty already wasting this forums' reader 
time with my trival question...

My main reason not to use function in this case, is that it may result in 
moving meaning full code too far away from where the action is. However, 
after reading some things on the web, I can solve this by added nested 
functions:

def BigFunction: 

    < lots of code > 

    def DefaultForLoopCode():
        pass

    for a in myList[:-1]:
        DefaultForLoopCode()
    DoExtra()
    DefaultForLoopCode()
    
I think this will solve my problem, while still keeping everything 
relatively clean. 

Thanks,
Tom





More information about the Python-list mailing list