Python Inner Function Modify Outer Variable In Statistics