def _cast_to_int(num, param_name):
  """Casts integer-like objects to int.

  Args:
      num: the number to be casted.
      param_name: the name of the parameter to be displayed in an error
          message.

  Returns:
      num as an int.

  Raises:
      TypeError: if num is not integer-like.
  """

  try:
    # __index__() is the Pythonic way to convert integer-like objects (e.g.
    # np.int64) to an int
    return num.__index__()
  except (TypeError, AttributeError):
    raise TypeError('%s is not integer-like (found type: %s)'
                    %(param_name, type(num).__name__))