@show displays spurious information?

Sorry if this is a silly question but I don’t recall @show displaying spurious information like the following illustrates. Is that really the intended behavior?

   _       _ _(_)_     |  A fresh approach to technical computing
  (_)     | (_) (_)    |  Documentation: https://docs.julialang.org
   _ _   _| |_  __ _   |  Type "?help" for help.
  | | | | | | |/ _` |  |
  | | |_| | | | (_| |  |  Version 0.6.3 (2018-05-28 20:20 UTC)
 _/ |\__'_|_|_|\__'_|  |  Official http://julialang.org/ release
|__/                   |  x86_64-apple-darwin14.5.0

julia> A = rand(5,5);

julia> @show A
A = [0.738962 0.030631 0.0414155 0.92469 0.819403; 0.0795628 0.261736 0.467157 0.172408 0.564304; 0.752273 0.204407 0.0575049 0.778186 0.72415; 0.551209 0.121949 0.790782 0.376882 0.331133; 0.400236 0.254467 0.791898 0.713791 0.459024]
5×5 Array{Float64,2}:
 0.738962   0.030631  0.0414155  0.92469   0.819403
 0.0795628  0.261736  0.467157   0.172408  0.564304
 0.752273   0.204407  0.0575049  0.778186  0.72415 
 0.551209   0.121949  0.790782   0.376882  0.331133
 0.400236   0.254467  0.791898   0.713791  0.459024

julia> @show 2
2 = 2
2

julia> @show π
π = π = 3.1415926535897...
π = 3.1415926535897...

The last one looks especially silly. Only the last part of the output seems useful to me.

The π example is a bit goofy, but there’s nothing really wrong going on here. @show x always prints x = and then whatever Base.repr(x) returns. It also returns the value of x, which the REPL then happily displays for you. You can see that with macroexpand:

julia> @macroexpand @show x
quote 
    (Base.println)("x = ", (Base.repr)(begin  # show.jl, line 243:
                #2#value = x
            end))
    #2#value
end

Base.repr(π) is "π = 3.1415926535897...", presumably to indicate that this is (a) indeed the pi which is approximately 3.14 and not some other pi but also (b) not a floating-point approximation but a representation of the exact number. It just looks goofy when you do @show pi for the same reason that @show 2 looks odd.

@show is used when you want to show both a value and the name of the variable it’s bound to. If that’s not what you want, then println() is probably the right choice.

1 Like

Thanks. I’ve never found println particularly useful as it dumps everything wholesale:

julia> println(A)
[2.19053 1.17125 0.983764 0.204726 1.48619; 1.17125 1.0 0.0 0.19505 0.14318; 0.983764 0.0 1.0 0.620039 0.817307; 0.204726 0.19505 0.620039 1.0 1.03617; 1.48619 0.14318 0.817307 1.03617 1.17563]

How would one get

julia> friendly_print(A)
5×5 Array{Float64,2}:
 0.738962   0.030631  0.0414155  0.92469   0.819403
 0.0795628  0.261736  0.467157   0.172408  0.564304
 0.752273   0.204407  0.0575049  0.778186  0.72415 
 0.551209   0.121949  0.790782   0.376882  0.331133
 0.400236   0.254467  0.791898   0.713791  0.459024

In the REPL, one can simply type A, but how about in a script?

display(A) seems to do it. I must admit I’m totally missing the point of @show.

@show is not particularly useful at the REPL. It’s meant to be used inside a block of code where just printing a value is less helpful than also showing what variable it’s bound to.

<code here>
@show a
<more code>
@show b

makes it obvious which of the printed values is a and which is b.

4 Likes