code: def format(
      tokens, formatter, outfile=None
  ):  # pylint: disable=redefined-builtin
      """
      Format ``tokens`` (an iterable of tokens) with the formatter ``formatter``
      (a `Formatter` instance).
  
      If ``outfile`` is given and a valid file object (an object with a
      ``write`` method), the result will be written to it, otherwise it
      is returned as a string.
      """
      try:
          if not outfile:
              realoutfile = (
                  getattr(formatter, "encoding", None)
                  and BytesIO()
                  or StringIO()
              )
              formatter.format(tokens, realoutfile)
              return realoutfile.getvalue()
          else:
              formatter.format(tokens, outfile)
      except TypeError:
          # Heuristic to catch a common mistake.
          from pygments.formatter import Formatter
  
          if isinstance(formatter, type) and issubclass(
              formatter, Formatter
          ):
              raise TypeError(
                  "format() argument must be a formatter instance, "
                  "not a class"
              )
          raise
  
imports: from io import StringIO, BytesIO
label_desc: Use image node_id 2 for calling a global function with example usage: format(tokens, formatter, outfile) and returns: realoutfile