Quick debugging using println?

Hi there,

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:

def myfunc(value):
    print("Value is", value) # <-- this
    # do stuff
    # return stuff

Thanks!

How’s this?

julia> function pval(val)
       println("Value is ",val)
       end
pval (generic function with 1 method)

julia> pval(1.0)
Value is 1.00000e+00

julia> pval("hello")
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:

image

1 Like

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…).

Thanks

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.

6 Likes

Thanks. This was it!

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
For example
if __debug__: print variablename

__debug__

This constant is true if Python was not started with an -O option. See also the assert statement.

For Julia have a look at this thread please

Incidentally, you might also like the @show macro for print debugging:

julia> f(val) = @show val
f (generic function with 1 method)

julia> f(1);
val = 1
1 Like

Also see the goodies in

https://github.com/timholy/DebuggingUtilities.jl

1 Like

Thanks to everyone for your help and suggestions. Great community, great language!