如何使用户定义的函数描述(“docstrings”)可供julia REPL使用?
当通过REPL使用如何使用户定义的函数描述(“docstrings”)可供julia REPL使用?,julia,Julia,当通过REPL使用?f或帮助(f) 例如,假设我编写了以下函数 function f(x::Float64, y::Float64) return 2x - y^2 end 如果我将其加载到julia会话中并尝试help(f)我会得到以下结果: julia> help(f) f (generic function with 1 method) 如果我想看到这样的东西呢 julia> help(f) f Compute 2 times x minus y squar
?f
或帮助(f)
例如,假设我编写了以下函数
function f(x::Float64, y::Float64)
return 2x - y^2
end
如果我将其加载到julia会话中并尝试help(f)
我会得到以下结果:
julia> help(f)
f (generic function with 1 method)
如果我想看到这样的东西呢
julia> help(f)
f
Compute 2 times x minus y squared
这里写着“计算2乘以x减去y的平方”。我猜我的问题的答案可以从“描述应该写在哪里?”这个问题的答案中确定
举个例子,如果我想在python中做同样的事情,我可以定义函数并将描述作为docstring:
def f(x, y):
"""
Compute 2 times x minus y squared
"""
return 2 * x - y ** 2
当我从IPython键入help(f)
或f?
时,这将使我的描述立即可用。您可以在Julia版本0.4(2015年10月)及更高版本中使用宏
% julia
_
_ _ _(_)_ | A fresh approach to technical computing
(_) | (_) (_) | Documentation: http://docs.julialang.org
_ _ _| |_ __ _ | Type "?help" for help.
| | | | | | |/ _` | |
| | |_| | | | (_| | | Version 0.4.0 (2015-10-08 06:20 UTC)
_/ |\__'_|_|_|\__'_| | Official http://julialang.org/ release
|__/ | x86_64-apple-darwin13.4.0
julia> @doc """
Compute 2 times x minus y squared.
""" ->
function f(x::Float64, y::Float64)
return 2x - y^2
end
f (generic function with 1 method)
julia> @doc f
Compute 2 times x minus y squared.
编辑:正如@Harrison Grodin所指出的,0.5及以上版本支持缩写语法以及标记、LaTEX和其他一些优点:
"""
Calculate the left Riemann sum[^1] approximating ``\int_a^b f(x) dx = F(b) - F(a).``
[^1]: Thomas G., Finney R. (1996), Calculus and Analytic Geometry, Addison Wesley, ISBN 0-201-53174-7
"""
function rs(a, b, d, f)
end
还有更多细节。在Julia v0.5+(包括最近的Julia版本,如1.2+)中,您可以在函数定义上方编写多行字符串。(不再需要@doc
)
有关正确设置文档字符串格式的更多信息,请参阅官方。我认为您还不能这样做。例如,这将很快发生。见讨论
julia> """
cube(x)
Compute the cube of `x`, ``x^3``.
# Examples
```jldoctest
julia> cube(2)
8
```
"""
function cube(x)
x^3
end
cube
help?> cube
search: Cdouble isexecutable Ac_mul_B Ac_mul_Bc Ac_mul_B! Ac_mul_Bc! cumsum_kbn
cube(x)
Compute the cube of x, x^3.
Examples
≡≡≡≡≡≡≡≡≡≡
julia> cube(2)
8