Quantcast

Equivalent to the Python's None value for the function's default parameter value

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Equivalent to the Python's None value for the function's default parameter value

Alexandr
Hi all,

I need to specify function parameter by default value None/Nothing like
in Python, so that if it is not initialized during the function call I
do it inside the function.

I read corresponding section in the FAQ:
http://docs.julialang.org/en/release-0.5/manual/faq/#nothingness-and-missing-values


And I still don't understand what is the proper way of doing this.

Should I do it like this?


```
function foo(x1,x2; z = Nullable{Int64}())
    if isnull(z)
        ...
    else
        ...
    end
end
```

or in another way?


--
BR,
Alex
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Equivalent to the Python's None value for the function's default parameter value

Steven G. Johnson


On Friday, November 4, 2016 at 2:37:38 PM UTC-4, Alex wrote:
Hi all,

I need to specify function parameter by default value None/Nothing

You can just use "z=nothing", which is the equivalent of the Python construct, and check "z===nothing".

However, z will be type-unstable, so you might want to put your actual computations into another lower-level function in order to keep good performance.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Equivalent to the Python's None value for the function's default parameter value

Fengyang Wang
In reply to this post by Alexandr
There is no need to do this in Julia; you can simply initialize in the default argument.

function f(x; y=[x, x, x])
   
@show x y
end

then

julia
> f(1)
x
= 1
y
= [1,1,1]

because in Julia, unlike Python, the initialization is done at calltime instead of function definition time.


On Friday, November 4, 2016 at 2:37:38 PM UTC-4, Alex wrote:
Hi all,

I need to specify function parameter by default value None/Nothing like
in Python, so that if it is not initialized during the function call I
do it inside the function.

I read corresponding section in the FAQ:
<a href="http://docs.julialang.org/en/release-0.5/manual/faq/#nothingness-and-missing-values" target="_blank" rel="nofollow" onmousedown="this.href=&#39;http://www.google.com/url?q\x3dhttp%3A%2F%2Fdocs.julialang.org%2Fen%2Frelease-0.5%2Fmanual%2Ffaq%2F%23nothingness-and-missing-values\x26sa\x3dD\x26sntz\x3d1\x26usg\x3dAFQjCNHsR13Oe7pCeUxVm_7QO0txtVlSwg&#39;;return true;" onclick="this.href=&#39;http://www.google.com/url?q\x3dhttp%3A%2F%2Fdocs.julialang.org%2Fen%2Frelease-0.5%2Fmanual%2Ffaq%2F%23nothingness-and-missing-values\x26sa\x3dD\x26sntz\x3d1\x26usg\x3dAFQjCNHsR13Oe7pCeUxVm_7QO0txtVlSwg&#39;;return true;">http://docs.julialang.org/en/release-0.5/manual/faq/#nothingness-and-missing-values


And I still don't understand what is the proper way of doing this.

Should I do it like this?


```
function foo(x1,x2; z = Nullable{Int64}())
    if isnull(z)
        ...
    else
        ...
    end
end
```

or in another way?


--
BR,
Alex
Loading...