How to handle a Py value which can be None (with PythonCall)?
The following seems quite verbose (and it fails if x
is an ndarray)
using PythonCall
function handle_optional_value(x::Py)
if pyconvert(Bool, x == pybuiltins.None)
return nothing
end
# something else
end
I do not think that is verbose at all. There is a difference between being succinct and being so truncated as to be illegible. There seems to be an undue respect towards the latter, in many cases.
Also, you should probably be using the pyis
function from the PythonCall library instead of ==
, to compare x
to None
(link).
function then(f, x::Py)
if pyconvert(Bool, x == pybuiltins.None)
return nothing
end
f(x)
end
then(y) do x
x + 1
end
Last updated: Oct 02 2023 at 04:34 UTC