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
Jesper Stemann Andersen has marked this topic as resolved.
Santtu said:
Also, you should probably be using the
pyis
function from the PythonCall library instead of==
, to comparex
toNone
(link).
Thanks - this was what I was looking for.
Last updated: Nov 22 2024 at 04:41 UTC