def ToCacheKey(path):
  """Convert a list of items to a string suitable as a cache key.

  Args:
    path: A list; elements can be strings, objects with a __name__, or anything
    that can be converted to a string with str().

  Returns:
    A string serialization of the key.  The items of the list are joined with
    commas; commas and backslashes within items are escaped with backslashes.
    For non-string items, the item's __name__ is used if present, or the item
    is converted to a string with str().
  """
  return ','.join(str(getattr(item, '__name__', item) or '')
                  .replace('\\', '\\\\').replace(',', '\\,')
                  for item in path)