def get_optional_vim_attr(vim_object, attr_name):
  """
  Returns either the attribute value for the attribute 'attr_name' on the
  object 'vim_object', else None if the attribute isn't set.
  """
  try:
    return getattr(vim_object, attr_name)
  except IndexError:
    # IndexError is raised if an optional, non-array "May not be present" (see
    # vSphere API documentation) attribute is accessed. For optional, array
    # attributes, it appears the attribute is always present although the array
    # could consist of zero elements.
    return None