0

I am learning 8.6. bisect — Array bisection algorithm,

bisect.bisect_left(a, x, lo=0, hi=len(a)) has default values of lo=0, hi=len(a),
When I checked the source code and found that hi=len(a) is not set as default directly,

def bisect_right(a, x, lo=0, hi=None):
    if lo < 0:
        raise ValueError('lo must be non-negative')
    if hi is None:
        hi = len(a)
    while lo < hi:
        mid = (lo+hi)//2  
        if x < a[mid]:
            hi = mid
        else:
            lo = mid+1
    return lo

It's achieved by a if check None.

What's the benefit do extra checking than set it as default straightly?

AbstProcDo
  • 19,953
  • 19
  • 81
  • 138
  • 4
    Because `a` hasn't been defined yet. The parameter doesn't exist until inside the function definition; other parameters can't see it. – chrisaycock Aug 21 '18 at 02:34
  • How smart... Could you please transmit the comment to answer as a forwards reference. @chrisaycock – AbstProcDo Aug 21 '18 at 02:42
  • 1
    The other question does not attempt to apply `len` to the parameter, but the accepted answer fully covers your situation. – Mad Physicist Aug 21 '18 at 02:51

1 Answers1

2

Because a hasn't been defined yet. The parameter doesn't exist until inside the function definition; other parameters can't see it.

chrisaycock
  • 36,470
  • 14
  • 88
  • 125