

Transforming Code into Beautiful, Idiomatic Python [video] - s16h
http://pyvideo.org/video/1780/transforming-code-into-beautiful-idiomatic-pytho

======
Monkeyget
Slides: [https://speakerdeck.com/pyconslides/transforming-code-
into-b...](https://speakerdeck.com/pyconslides/transforming-code-into-
beautiful-idiomatic-python-by-raymond-hettinger-1)

Notes:
[https://gist.github.com/JeffPaine/6213790](https://gist.github.com/JeffPaine/6213790)

------
azeirah
After a while, he shows the example of a find function

    
    
      def find(seq, target):
        for i, value in enumerate(seq):
          if value == target
            break
        else:
          return -1
        return i
    

Can't you just remove the whole break and else thing and just do this?

    
    
      def find(seq, target):
        for i, value in enumerate(seq):
          if value == target:
            return i
        return -1

~~~
bkcooper
Your second example looks fine to me.

EDIT: My best guess as to what's going on is that they want to display the
for-else idiom which could be useful in other contexts (e.g. if you don't want
to return anything from inside the loop and can't break out that way.) It's a
kinda surprising idiom; I had forgotten you could even do that in Python and
at first thought it might be a misindentation until I realized the logic would
be wrong if the else paired with if.

