def per_instance(*args, **kwargs):
  """A memoized key factory that works like `equal_args` except that the first parameter's identity
  is used when forming the key.

  This is a useful key factory when you want to enforce memoization happens per-instance for an
  instance method in a class hierarchy that defines a custom `__hash__`/`__eq__`.
  """
  instance_and_rest = (InstanceKey(args[0]),) + args[1:]
  return equal_args(*instance_and_rest, **kwargs)
A memoized key factory that works like `equal_args` except that the first parameter's identity
  is used when forming the key.

  This is a useful key factory when you want to enforce memoization happens per-instance for an
  instance method in a class hierarchy that defines a custom `__hash__`/`__eq__`.