Println,Print,show,display 爱哪用哪?


#1

差别不大,就格式风格不同而已。

如:println多了一个回车。display在atom的repl栏中显示不全(用命令行执行文件没问题)。

没搞懂,干嘛不用参数,反而造这么多词语,又得多背几个…


#2

没事多看看帮助/文档

  • print 就是最常用的那个。内部依赖 show
  • println 真的就是 print 多加个换行。不想用就手动加换行呗
  • show 通常你定义了新类型想要格式化输出,就给 show 加个新方法。
    show 还可以指定输出的 mime 类型(MIME"image/png",MIME"text/plain",等)
  • display 一般与 show 联用进行多媒体输出。默认为信息最丰富的表示,比如图片或html,以纯文本作为 fallback。
    参见 多媒体 I/O

除此之外还有 reprsprint
这些东西搅在一起的关系见我的探究:

更多 IO 参见 - I/O 与网络 · Julia中文文档

help?> print
search: print println printstyled sprint isprint prevind parentindices precision escape_string setprecision

  print([io::IO], xs...)

  Write to io (or to the default output stream stdout if io is not given) a canonical (un-decorated) text
  representation of values xs if there is one, otherwise call show. The representation used by print includes minimal
  formatting and tries to avoid Julia-specific details.

  Printing nothing is not allowed and throws an error.

  Examples
  ≡≡≡≡≡≡≡≡≡≡

  julia> print("Hello World!")
  Hello World!
  julia> io = IOBuffer();

  julia> print(io, "Hello", ' ', :World!)

  julia> String(take!(io))
  "Hello World!"

help?> println
search: println printstyled print sprint isprint

  println([io::IO], xs...)

  Print (using print) xs followed by a newline. If io is not supplied, prints to stdout.

  Examples
  ≡≡≡≡≡≡≡≡≡≡

  julia> println("Hello, world")
  Hello, world

  julia> io = IOBuffer();

  julia> println(io, "Hello, world")

  julia> String(take!(io))
  "Hello, world\n"

help?> show
search: show showable showerror @show Cshort Cushort hasmethod searchsorted searchsortedlast searchsortedfirst

  show(x)

  Write an informative text representation of a value to the current output stream. New types should overload show(io,
  x) where the first argument is a stream. The representation used by show generally includes Julia-specific
  formatting and type information.

  ────────────────────────────────────────────────────────────────────────────────────────────────────────────────────

  show(io, mime, x)

  The display functions ultimately call show in order to write an object x as a given mime type to a given I/O stream
  io (usually a memory buffer), if possible. In order to provide a rich multimedia representation of a user-defined
  type T, it is only necessary to define a new show method for T, via: show(io, ::MIME"mime", x::T) = ..., where mime
  is a MIME-type string and the function body calls write (or similar) to write that representation of x to io. (Note
  that the MIME"" notation only supports literal strings; to construct MIME types in a more flexible manner use
  MIME{Symbol("")}.)

  For example, if you define a MyImage type and know how to write it to a PNG file, you could define a function
  show(io, ::MIME"image/png", x::MyImage) = ... to allow your images to be displayed on any PNG-capable
  AbstractDisplay (such as IJulia). As usual, be sure to import Base.show in order to add new methods to the built-in
  Julia function show.

  The default MIME type is MIME"text/plain". There is a fallback definition for text/plain output that calls show with
  2 arguments. Therefore, this case should be handled by defining a 2-argument show(io::IO, x::MyType) method.

  Technically, the MIME"mime" macro defines a singleton type for the given mime string, which allows us to exploit
  Julia's dispatch mechanisms in determining how to display objects of any given type.

  The first argument to show can be an IOContext specifying output format properties. See IOContext for details.

help?> display
search: display displaysize displayable redisplay popdisplay pushdisplay TextDisplay AbstractDisplay isdispatchtuple

  display(x)
  display(d::AbstractDisplay, x)
  display(mime, x)
  display(d::AbstractDisplay, mime, x)

  AbstractDisplay x using the topmost applicable display in the display stack, typically using the richest supported
  multimedia output for x, with plain-text stdout output as a fallback. The display(d, x) variant attempts to display
  x on the given display d only, throwing a MethodError if d cannot display objects of this type.

  In general, you cannot assume that display output goes to stdout (unlike print(x) or show(x)). For example,
  display(x) may open up a separate window with an image. display(x) means "show x in the best way you can for the
  current output device(s)." If you want REPL-like text output that is guaranteed to go to stdout, use show(stdout,
  "text/plain", x) instead.

  There are also two variants with a mime argument (a MIME type string, such as "image/png"), which attempt to display
  x using the requested MIME type only, throwing a MethodError if this type is not supported by either the display(s)
  or by x. With these variants, one can also supply the "raw" data in the requested MIME type by passing
  x::AbstractString (for MIME types with text-based storage, such as text/html or application/postscript) or
  x::Vector{UInt8} (for binary MIME types).

julia>  

#3

julia: print, show, display, dump, … – What to override? What to use?