[Python-ideas] Default return values to int and float

Ron Adam ron3200 at gmail.com
Wed Oct 5 19:58:41 CEST 2011


On Wed, 2011-10-05 at 07:31 -0700, Ethan Furman wrote:
> Ron Adam wrote:
> > On Wed, 2011-10-05 at 19:08 +1300, Greg Ewing wrote:
> >> Guido van Rossum wrote:
> >>
> >>> I'm out of ideas here. But of all these, str.find is probably still
> >>> the worst -- I've flagged bugs caused by it too many times to count.
> >> Could a with-statement be used here somehow?
> >>
> >>      with finding(x, s) as i:
> >>          ...
> > 
> > Or an iterator.
> > 
> >      for i in finding(x, s):
> >           ...
> 
> How would the case of not found be handled in either of these proposals?
> 
>      with finding(x, s) as i:
>          ...
>      if not i:  # same problem as str.find, unless i is not a simple int

I'll let Nick answer this one because I'm not sure about it.


>      for i in finding(x, s):
>          ...
>      else:
>          # unless for loop has a break, this will happen...
>          # not a problem until you want more than just the first
>          # occurrence of s in x


      for i in finding(x, s):
          if i > 25:
              break
          <build or get a result>
      else:
          return result
      raise(ValueError("string 's' had an 'x' after position 25")

Cheers,
   Ron






















More information about the Python-ideas mailing list