“For” loop first iteration

前端 未结 13 2389
渐次进展
渐次进展 2020-12-04 15:17

I would like to inquire if there is an elegant pythonic way of executing some function on the first loop iteration. The only possibility I can think of is:

f         


        
13条回答
  •  醉话见心
    2020-12-04 15:49

    You have several choices for the Head-Tail design pattern.

    seq= something.get()
    root.copy( seq[0] )
    foo( seq[0] )
    for member in seq[1:]:
        somewhereElse.copy(member)
        foo( member )
    

    Or this

    seq_iter= iter( something.get() )
    head = seq_iter.next()
    root.copy( head )
    foo( head )
    for member in seq_iter:
        somewhereElse.copy( member )
        foo( member )
    

    People whine that this is somehow not "DRY" because the "redundant foo(member)" code. That's a ridiculous claim. If that was true then all functions could only be used once. What's the point of defining a function if you can only have one reference?

提交回复
热议问题