I have noticed that using
println within a function (for quick debugging) doesn’t work in Jupyter Notebook. Nothing is printed in the output cell.
So what is an idiomatic way in Julia to match the functionality of the following python code:
print("Value is", value) # <-- this
# do stuff
# return stuff
julia> function pval(val)
println("Value is ",val)
pval (generic function with 1 method)
Value is 1.00000e+00
Value is hello
It seemed to me the quesion was partiularly about usage in a Jupyter notebook - that said, what you suggest is of course perfectly fine in that context as well:
Yup, Tested it in a notebook while you were writing your response.
@ZettEff, remember to run the code cell which defines the function before calling the function.
Hm…this is what I tried, of course. It doesn’t work. Good to know it should work. Feel kind of like an idiot now and do understand why I could not find anything googling the issue…
I will inspect why my case is different (i.e. what I have done wrong…).
What’s unfortunately easy to do is to edit a function and change its signature to be something less specific, and re-run the cell. This then doesn’t overwrite the old definition, I think that’s a common way to get surprising results, at least for me! Running
methods(pval) should print out all currently defined methods.
In Python I like to use the
debug flag you set the flag to true at runtime. You do not then need to clean up your code and comment out the printlns
if __debug__: print variablename
This constant is true if Python was not started with an
option. See also the
For Julia have a look at this thread please
In Python there is a feature for simple print style debugging to be switched on or off.
If you run a Python script with the -O option then
print variable, variable, variable
Is there an equivalent in Julia? I know you could set a constant to be true or false, bt that involves editing the code to switch on/off the debug lines.
Incidentally, you might also like the
@show macro for print debugging:
julia> f(val) = @show val
f (generic function with 1 method)
val = 1
Thanks to everyone for your help and suggestions. Great community, great language!