docs: def encode(obj, sedes=None, infer_serializer=True, cache=True):
      """Encode a Python object in RLP format.
  
      By default, the object is serialized in a suitable way first (using
      :func:`rlp.infer_sedes`) and then encoded. Serialization can be explicitly
      suppressed by setting `infer_serializer` to ``False`` and not passing an
      alternative as `sedes`.
  
      If `obj` has an attribute :attr:`_cached_rlp` (as, notably,
      :class:`rlp.Serializable`) and its value is not `None`, this value is
      returned bypassing serialization and encoding, unless `sedes` is given (as
      the cache is assumed to refer to the standard serialization which can be
      replaced by specifying `sedes`).
  
      If `obj` is a :class:`rlp.Serializable` and `cache` is true, the result of
      the encoding will be stored in :attr:`_cached_rlp` if it is empty.
  
      :param sedes: an object implementing a function ``serialize(obj)`` which will be used to
                    serialize ``obj`` before encoding, or ``None`` to use the infered one (if any)
      :param infer_serializer: if ``True`` an appropriate serializer will be selected using
                               :func:`rlp.infer_sedes` to serialize `obj` before encoding
      :param cache: cache the return value in `obj._cached_rlp` if possible
                    (default `True`)
      :returns: the RLP encoded item
      :raises: :exc:`rlp.EncodingError` in the rather unlikely case that the item is too big to
               encode (will not happen)
      :raises: :exc:`rlp.SerializationError` if the serialization fails
      """
      if isinstance(obj, Serializable):
          cached_rlp = obj._cached_rlp
          if sedes is None and cached_rlp:
              return cached_rlp
          else:
              really_cache = (
                  cache and
                  sedes is None
              )
      else:
          really_cache = False
  
      if sedes:
          item = sedes.serialize(obj)
      elif infer_serializer:
          item = infer_sedes(obj).serialize(obj)
      else:
          item = obj
  
      result = encode_raw(item)
      if really_cache:
          obj._cached_rlp = result
      return result