code: def encoded(string, encoding='utf-8'):
      
      assert isinstance(string, string_types) or isinstance(string, binary_type)
      if isinstance(string, text_type):
          return string.encode(encoding)
      try:
          # make sure the string can be decoded in the specified encoding ...
          string.decode(encoding)
          return string
      except UnicodeDecodeError:
          # ... if not use latin1 as best guess to decode the string before encoding as
          # specified.
          return string.decode('latin1').encode(encoding)
docs: Cast string to binary_type.
  
      :param string: six.binary_type or six.text_type
      :param encoding: encoding which the object is forced to
      :return: six.binary_type