+ [▶ Catching the Exceptions](#-catching-the-exceptions)
+ [▶ Same operands, different story!](#-same-operands-different-story)
+ [▶ Name resolution ignoring class scope](#-name-resolution-ignoring-class-scope)
+ [▶ Rounding like a banker *](#-rounding-like-a-banker-)
+ [▶ Needles in a Haystack *](#-needles-in-a-haystack-)
+ [▶ Splitsies *](#-splitsies-)
+ [▶ Wild imports *](#-wild-imports-)
@ -2532,6 +2533,58 @@ class SomeClass:
### ▶ Rounding like a banker *
Let's implement a naive function to get the middle element of a list:
mid_index = round(len(some_list) / 2)
return some_list[mid_index - 1]
>>> get_middle() # looks good
>>> get_middle([1,2,3]) # looks good
>>> get_middle([1,2,3,4,5]) # huh?
>>> len([1,2,3,4,5]) / 2 # good
>>> round(len([1,2,3,4,5]) / 2) # why?
It seems as though Python rounded 2.5 to 2.
#### 💡 Explanation:
This is not a float precision error, in fact, this behavior is intentional. Since Python 3.0, `round()` uses [banker's rounding](https://en.wikipedia.org/wiki/Rounding#Round_half_to_even) where .5 fractions are rounded to the nearest **even** number:
>>> import numpy # numpy does the same
See the [round() docs](https://docs.python.org/3/library/functions.html#round) or [this stackoverflow thread](https://stackoverflow.com/questions/10825926/python-3-x-rounding-behavior) for more information.
Note that `get_middle()` only returned 1 because the index was `round(0.5) - 1 = 0 - 1 = -1`, returning the last element in the list.
### ▶ Needles in a Haystack *
<!-- Example ID: 52a199b1-989a-4b28-8910-dff562cebba9 --->