Today I started learning Julia.
I've tried the following on Juno, running Julia 0.3.8
VERSION < v"0.4-" && import Docile
using Docile
#doc doc"""
Hello Hello ***world***
""" ->
function hello()
print ("Not bad")
end
With juno, the ctrl+D key-combo just yields the default:
hello (generic function with 1 method)
The same happens on the terminal with ?
I've tried out other varieties, for example #doc """ blah blah """ -> ..... or even #doc "blah blah" -> ......
I am not sure how to troubleshoot this.
edit
ok, wrapping the function in a module which is then imported makes the docstring work in the repl but the ctrl+d key combination in juno does still not work as before.
Since you have posted this question, docstrings have become standard throughout Julia Base and ?function will return the docstring in both Juno and the REPL.
Related
In python to get the documentation of a function, we can type (for example) help(len).
How to do the same to get a function's documentation in Julia?
In Julia you can use a question mark followed by a function name, i.e. ?functionname, to get information about a function.
If you are using the REPL, the question mark will switch your julia> prompt to a help?> prompt - similar to how ] triggers the pkg> REPL mode. Check out the documentation for more information.
In Jupyter notebooks (IJulia) you just type ?println and there is no visible REPL mode change.
Example:
help?> println # I typed ?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"
Note that this approach isn't restricted to functions. It works for all objects that have some docstrings attached to them:
help?> Sys.CPU_THREADS # docstring of a constant
Sys.CPU_THREADS
The number of logical CPU cores available in the system, i.e. the number of threads that the CPU can run concurrently. Note that this is not necessarily the number of CPU cores, for example, in the presence of hyper-threading (https://en.wikipedia.org/wiki/Hyper-threading).
See Hwloc.jl or CpuId.jl for extended information, including number of physical cores.
When I compile a cell with cython, it seems Jupyter forgets the compiled function in the next cell. This seems to me to be not right. What is going wrong?
I am using version 5.0.0 of the notebook, and
Python 3.6.1 |Anaconda custom (x86_64)| (default, May 11 2017, 13:04:09)
[GCC 4.2.1 Compatible Apple LLVM 6.0 (clang-600.0.57)]
Here is a MWE that produces the problem:
Cell 1:
%load_ext Cython
Cell 2:
%%cython
cdef int foo():
return 3
print(foo())
This produces:
3
In the next cell, I have
print(foo())
This produces:
---------------------------------------------------------------------------
NameError Traceback (most recent call last)
<ipython-input-3-9701608cebc0> in <module>()
----> 1 print(foo())
NameError: name 'foo' is not defined
I guess it's because you didn't defined your foo function as available in python (with cpdef) but only only give it a C signature (with cdef) so it can only be called from cython code.
In the cell 2 you can call it because you are still using cython code but in your cell 3 you are back in pure python and the function isn't available. There is various way to get the result from the foo function in python :
%%cython
# Not reachable in pure python:
cdef int foo():
return 3
# Python visible function signature:
cpdef int foo2():
return 3
# Or a wrapper around the cython function:
def foo3():
return foo()
You can now try to call foo2() or foo3() in your python code.
See one of the relevant part of the documentation if you haven't see it.
I would like to retrieve the path of the currently running Julia interpreter from Julia. In Python, this can be achieved with sys.executable.
Base.julia_cmd() is probably what you need. It returns the full command line that was used to invoke the current julia process, with the default options spelled out. Base.julia_exename() returns the name of the executable.
julia> Base.julia_cmd()
/Users/aviks/dev/julia/julia5/usr/bin/julia -Cnative -J/usr/lib/julia/sys.dylib --compile=yes --depwarn=yes
julia> Base.julia_exename()
"julia"
If you just want the location of the julia executable, try one of these:
julia> julia_bin_exe = joinpath(Base.Sys.BINDIR,Base.julia_exename())
"/home/mkitti/src/julia/usr/bin/julia"
julia> Base.julia_cmd()
`/home/mkitti/src/julia/usr/bin/julia -Cnative -J/home/mkitti/src/julia/usr/lib/julia/sys.so -g1`
julia> typeof(Base.julia_cmd())
Cmd
julia> Base.julia_cmd()[1]
"/home/mkitti/src/julia/usr/bin/julia"
julia> julia_bin_exe == Base.julia_cmd()[1]
true
Every time I call a constructor for a custom-type, the show method for that type runs. I cannot work out why. Reproducible example follows:
I have a module:
module ctbTestModule1
import Base.show
export MyType1
type MyType1
function MyType1()
new()
end
end
function show(io::IO, a::MyType1)
println("hello world")
end
end
I open up a fresh julia session in the REPL and type:
using ctbTestModule1
z = MyType1()
The following prints to the console when I run the line z = MyType1():
hello world
How is the show method being called here? It certainly doesn't appear to be called in the inner constructor...
The REPL (Read-evaluate-print loop) evaluates and prints every statement.
What you describe is the usual behavior.
Run z = 1 in your REPL and the printed output will be 1.
Likewise, your z is a MyType1 which is displayed as hello world.
If you want to suppress the output in the REPL finish your statement with a semicolon ;:
z = MyType1();
So, to answer your question: Yes, if you create an instance of a type in the REPL, the result is shown by calling the show() function of that type.
This is just a convenience but I think useful. Note that IPython allows a pure quit as does Matlab. Thus it would be reasonble in Julia to allow aliasing.
Thanks for any ideas as to how to do this.
Quitting in Julia
If you are using Julia from the command line then ctrl-d works. But if your intention is to quit by typing a command this is not possible exactly the way you want it because typing quit in the REPL already has a meaning which is return the value associated with quit, which is the function quit.
julia> quit
quit (generic function with 1 method)
julia> typeof(quit)
Function
Also Python
But that's not rare, for example Python has similar behavior.
>>> quit
Use quit() or Ctrl-D (i.e. EOF) to exit
Using a macro
Using \q might be nice in the Julia REPL like in postgres REPL, but unfortunately \ also already has a meaning. However, if you were seeking a simple way to do this, how about a macro
julia> macro q() quit() end
julia> #q
Causes Julia to Quit
If you place the macro definition in a .juliarc.jl file, it will be available every time you run the interpreter.
As waTeim notes, when you type quit into the REPL, it simply shows the function itself… and there's no way to change this behavior. You cannot execute a function without calling it, and there are a limited number of ways to call functions in Julia's syntax.
What you can do, however, is change how the Functions are displayed. This is extremely hacky and is not guaranteed to work, but if you want this behavior badly enough, here's what you can do: hack this behavior into the display method.
julia> function Base.writemime(io::IO, ::MIME"text/plain", f::Function)
f == quit && quit()
if isgeneric(f)
n = length(f.env)
m = n==1 ? "method" : "methods"
print(io, "$(f.env.name) (generic function with $n $m)")
else
show(io, f)
end
end
Warning: Method definition writemime(IO,MIME{symbol("text/plain")},Function) in module Base at replutil.jl:5 overwritten in module Main at none:2.
writemime (generic function with 34 methods)
julia> print # other functions still display normally
print (generic function with 22 methods)
julia> quit # but when quit is displayed, it actually quits!
$
Unfortunately there's no type more specific than ::Function, so you must completely overwrite the writemime(::IO,::MIME"text/plain",::Function) definition, copying its implementation.
Also note that this is pretty unexpected and somewhat dangerous. Some library may actually end up trying to display the function quit… causing you to lose your work from that session.
Related to Quitting in Julia
I was searching for something simple. This question hasn't been updated since 2017, as I try to learn Julia now, and spend some time googling for something simple and similar to python. Here, what I found:
You can use:
exit()
Note
I use julia 1.53