How do I determine if an object is callable?
v0.6.0-pre.alpha.179> callable(o) = !isempty(methods(o))
callable (generic function with 1 method)
v0.6.0-pre.alpha.179> is_not_callable = 1
1
v0.6.0-pre.alpha.179> is_callable(x) = 1
is_callable (generic function with 1 method)
v0.6.0-pre.alpha.179> callable(is_not_callable)
false
v0.6.0-pre.alpha.179> callable(is_callable)
true
Bool
can convert only 0 and 1 to a boolean:
julia> callable(sin)
ERROR: InexactError()
Stacktrace:
[1] convert(::Type{Bool}, ::Int64) at ./bool.jl:7
[2] callable(::Function) at ./REPL[9]:1
Oh ok, thanks
In the case you just want to check if an object is a function, you can use isa Function
(not sure if it is the best way)
julia> sin isa Function
true
wow dude… the thread is 3 years old
Also don’t do that.
Actually, I had the same question recently so I posted what I’m currently doing to check if it is OK. What is the best solution then?
EDIT: I feel it is awkward to do !isempty(methods(o))
everytime I want to know if o is callable.
just make that a function… as suggested
Well, of course I assumed that the object is an actual function, so yes…
then that would be the answer to your question, no? if you read that stackoverflow post, the first answer is iscallable(f) = !isempty(methods(f))
– make your own function
I don’t agree with that posted answer since it is pretty complex to do something as simple as asking f isa Function
as I remarked. Also, I don’t consider useful defining your own callable(f)
method to only ask if a variable f
is a function or not, having the method ìsa
as I mentioned. That’s why I continued this old thread to add my thoughts about a possibly better way to achieve that goal.
If someone thinks the posted way is better for some reason (e.g. performance, style, etc), I would like to hear your thoughts. In the meantime, thanks for your replies on this.
No that’s by no mean a better method. It’s something that you should not do. And object being callable has absolutely nothing to do with whether it’s a function or not. If you frequently see yourself wanting to know whether something is callable or not you are definitely doing something wrong.
Also, it’s not that the iscallable
function defined is something you should do either. You generally shouldn’t care about it but it at least gives the correct answer. OTOH, isa Function
just gives the wrong answer. (It has both false positive and false negative.)
I think it could make sense your point about design, in terms of the convenience of asking if a variable is callable or not in a program. But if you are in a situation where you do need to do that (e.g. you receive a script where types are not handled) and you cannot redefine everything, you should have a way to solve that simple thing. So I found useful to post the best way to do it
Also, could you mention examples about false negatives and false positives of isa Function
? I cannot find possible cases for that.
Python functions from PyCall
aren’t Function
s. Also constructors of Julia structs aren’t Function
s
basically,
Great, both are good examples! Thanks for the detailed answers! I will take this into account then.
No the point is not about recieving something you don’t know. The point is that you shouldn’t need to know. You shouldn’t make you decision based on whether something is callable or not.