# Quick debugging using println?

**URL:** <https://discourse.julialang.org/t/quick-debugging-using-println/51398>\
**Category:** New to Julia\
**Tags:** question\
**Created:** [December 7, 2020, 1:08pm UTC](https://discourse.julialang.org/t/quick-debugging-using-println/51398 "2020-12-07T13:08:40Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![ZettEff](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/zetteff/32/31976_2.png) [@ZettEff](https://discourse.julialang.org/u/ZettEff)\
**Post date:** [December 7, 2020, 1:08pm UTC](https://discourse.julialang.org/t/quick-debugging-using-println/51398/1 "2020-12-07T13:08:41Z")

</div>

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:

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

```

Thanks!

---

<div class="post-metadata">

**Author:** ![ctkelley](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ctkelley/32/10684_2.png) [@ctkelley](https://discourse.julialang.org/u/ctkelley)\
**Post date:** [December 7, 2020, 1:15pm UTC](https://discourse.julialang.org/t/quick-debugging-using-println/51398/2 "2020-12-07T13:15:37Z")

</div>

How’s this?

```julia
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

```

---

<div class="post-metadata">

**Author:** ![nilshg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nilshg/32/2283_2.png) [@nilshg](https://discourse.julialang.org/u/nilshg)\
**Post date:** [December 7, 2020, 1:17pm UTC](https://discourse.julialang.org/t/quick-debugging-using-println/51398/3 "2020-12-07T13:17:05Z")

</div>

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](https://global.discourse-cdn.com/julialang/original/3X/5/3/53478a5d0e9fa4fc15e463f7b83c7f313128d2e6.png)

---

<div class="post-metadata">

**Author:** ![ctkelley](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ctkelley/32/10684_2.png) [@ctkelley](https://discourse.julialang.org/u/ctkelley)\
**Post date:** [December 7, 2020, 1:18pm UTC](https://discourse.julialang.org/t/quick-debugging-using-println/51398/4 "2020-12-07T13:18:41Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![ZettEff](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/zetteff/32/31976_2.png) [@ZettEff](https://discourse.julialang.org/u/ZettEff)\
**Post date:** [December 7, 2020, 1:19pm UTC](https://discourse.julialang.org/t/quick-debugging-using-println/51398/5 "2020-12-07T13:19:25Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![mcabbott](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mcabbott/32/6603_2.png) [@mcabbott](https://discourse.julialang.org/u/mcabbott)\
**Post date:** [December 7, 2020, 1:31pm UTC](https://discourse.julialang.org/t/quick-debugging-using-println/51398/6 "2020-12-07T13:31:17Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![ZettEff](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/zetteff/32/31976_2.png) [@ZettEff](https://discourse.julialang.org/u/ZettEff)\
**Post date:** [December 7, 2020, 1:47pm UTC](https://discourse.julialang.org/t/quick-debugging-using-println/51398/7 "2020-12-07T13:47:55Z")

</div>

Thanks. This was it!

---

<div class="post-metadata">

**Author:** ![johnh](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/johnh/32/3615_2.png) [@johnh](https://discourse.julialang.org/u/johnh)\
**Post date:** [December 7, 2020, 1:50pm UTC](https://discourse.julialang.org/t/quick-debugging-using-println/51398/8 "2020-12-07T13:50:22Z")

</div>

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`](https://docs.python.org/3/using/cmdline.html#cmdoption-o) option. See also the [`assert`](https://docs.python.org/3/reference/simple_stmts.html#assert) statement.

---

<div class="post-metadata">

**Author:** ![johnh](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/johnh/32/3615_2.png) [@johnh](https://discourse.julialang.org/u/johnh)\
**Post date:** [December 7, 2020, 1:51pm UTC](https://discourse.julialang.org/t/quick-debugging-using-println/51398/9 "2020-12-07T13:51:52Z")

</div>

For Julia have a look at this thread please

> [@Equivalent of Python if \_\_debug\_\_](https://discourse.julialang.org/t/equivalent-of-python-if-debug/25715):
>
> 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 if \_\_debug\_\_: 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.

---

<div class="post-metadata">

**Author:** ![yha](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yha/32/3502_2.png) [@yha](https://discourse.julialang.org/u/yha)\
**Post date:** [December 7, 2020, 5:34pm UTC](https://discourse.julialang.org/t/quick-debugging-using-println/51398/10 "2020-12-07T17:34:27Z")

</div>

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

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

julia> f(1);
val = 1

```

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [December 8, 2020, 2:55pm UTC](https://discourse.julialang.org/t/quick-debugging-using-println/51398/11 "2020-12-08T14:55:23Z")

</div>

Also see the goodies in

[https://github.com/timholy/DebuggingUtilities.jl](https://github.com/timholy/DebuggingUtilities.jl)

---

<div class="post-metadata">

**Author:** ![ZettEff](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/zetteff/32/31976_2.png) [@ZettEff](https://discourse.julialang.org/u/ZettEff)\
**Post date:** [December 8, 2020, 9:31pm UTC](https://discourse.julialang.org/t/quick-debugging-using-println/51398/12 "2020-12-08T21:31:06Z")

</div>

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