You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Is your feature request related to a problem? Please describe.
In my application I'd like to pass a Julia callable to a Python library. The Python library checks if the callable is passed (it's an optional argument) with something like:
iff:
# f is here, use itelse:
# do something else
When I try to pass a Julia callable I get an error about __len__ not being implemented. I think this is because, in Python, the truthiness of a class is controlled by the __bool__ method, if present, and the __len__ method if not, and then finally is always True if the class implements neither. In PythonCall, a Julia function is treated as an AnyValue in Python, which doesn't implement __bool__ but does implement __len__. But the AnyValue__len__ eventually calls the Julia length function, which isn't implemented for Julia functions and triggers an error.
I believe in Python a plain function is always considered True.
Here is an example of a Julia REPL session that shows the error:
Describe the solution you'd like
It would be cool if a Julia function/callable could be considered True in Python, similar to Python functions.
Describe alternatives you've considered
I have a workaround where I wrap the Julia function I'm passing to Python in a Julia struct, then implement the Julia length function for that as something like Base.length(::MyStruct) = 1. That works fine, but it would be great if I didn't have to do it. :-)
The text was updated successfully, but these errors were encountered:
Is your feature request related to a problem? Please describe.
In my application I'd like to pass a Julia callable to a Python library. The Python library checks if the callable is passed (it's an optional argument) with something like:
When I try to pass a Julia callable I get an error about
__len__
not being implemented. I think this is because, in Python, the truthiness of aclass
is controlled by the__bool__
method, if present, and the__len__
method if not, and then finally is alwaysTrue
if the class implements neither. In PythonCall, a Julia function is treated as anAnyValue
in Python, which doesn't implement__bool__
but does implement__len__
. But theAnyValue
__len__
eventually calls the Julialength
function, which isn't implemented for Julia functions and triggers an error.I believe in Python a plain function is always considered
True
.Here is an example of a Julia REPL session that shows the error:
Describe the solution you'd like
It would be cool if a Julia function/callable could be considered
True
in Python, similar to Python functions.Describe alternatives you've considered
I have a workaround where I wrap the Julia function I'm passing to Python in a Julia
struct
, then implement the Julialength
function for that as something likeBase.length(::MyStruct) = 1
. That works fine, but it would be great if I didn't have to do it. :-)The text was updated successfully, but these errors were encountered: