【问题标题】:What functions are called to display an (Array) variable on the julia REPL?调用什么函数来在 julia REPL 上显示(数组)变量?
【发布时间】:2015-11-04 13:14:18
【问题描述】:

假设我进入:

julia> X = randn(3,4)
3x4 Array{Float64,2}:
 -0.862092   0.78568     0.140078  -0.0409951
 -0.157692   0.0838577   1.38264   -0.296809 
  1.40242   -0.628556   -0.500275   0.258898 

调用了哪些函数来产生给定的输出?

请注意,重载 Base.show 似乎不足以改变这种行为,所以我不确定该去哪里。

julia> Base.show(io::IO, A::Array{Float64, 2}) = println("Humbug")
show (generic function with 120 methods)

julia> X
3x4 Array{Float64,2}:
 -0.862092   0.78568     0.140078  -0.0409951
 -0.157692   0.0838577   1.38264   -0.296809 
  1.40242   -0.628556   -0.500275   0.258898 

是不是我必须更改 Base/array.jl 源代码并重建 julia 才能使这样的更改生效?注意 this 和用户定义类型的区别:

julia> type foo
       x::Float32
       s::ASCIIString
       end

julia> ob = foo(1., "boo")
foo(1.0f0,"boo")

julia> Base.show(io::IO, someob::foo) = print("Humbug!")
show (generic function with 123 methods)

julia> ob
Humbug!

【问题讨论】:

  • Base.show(X) 会给你Humbug。 :)

标签: julia read-eval-print-loop


【解决方案1】:

好吧,你应该重载display()

julia> Base.display(A::Array{Float64, 2}) = println("Humbug")
display (generic function with 11 methods)

julia> X
Humbug

你可以在REPL.jl找到定义。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-07-28
    • 1970-01-01
    • 2016-06-19
    • 1970-01-01
    相关资源
    最近更新 更多