# How to determine if an object is callable?

**URL:** <https://discourse.julialang.org/t/how-to-determine-if-an-object-is-callable/2897>\
**Category:** General Usage\
**Tags:** question\
**Created:** [March 27, 2017, 8:51am UTC](https://discourse.julialang.org/t/how-to-determine-if-an-object-is-callable/2897 "2017-03-27T08:51:05Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![TsurHerman](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tsurherman/32/1234_2.png) [@TsurHerman](https://discourse.julialang.org/u/TsurHerman)\
**Post date:** [March 27, 2017, 8:51am UTC](https://discourse.julialang.org/t/how-to-determine-if-an-object-is-callable/2897/1 "2017-03-27T08:51:05Z")

</div>

How do I determine if an object is callable?

---

<div class="post-metadata">

**Author:** ![Per](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/per/32/10387_2.png) [@Per](https://discourse.julialang.org/u/Per)\
**Post date:** [March 27, 2017, 9:26am UTC](https://discourse.julialang.org/t/how-to-determine-if-an-object-is-callable/2897/2 "2017-03-27T09:26:59Z")

</div>

> <https://stackoverflow.com/questions/41658692/how-to-determine-if-julia-object-is-callable>

---

<div class="post-metadata">

**Author:** ![tamasgal](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamasgal/32/27946_2.png) [@tamasgal](https://discourse.julialang.org/u/tamasgal)\
**Post date:** [March 27, 2017, 9:37am UTC](https://discourse.julialang.org/t/how-to-determine-if-an-object-is-callable/2897/3 "2017-03-27T09:37:41Z")

</div>

```julia
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

```

---

<div class="post-metadata">

**Author:** ![giordano](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/giordano/32/2166_2.png) [@giordano](https://discourse.julialang.org/u/giordano)\
**Post date:** [March 27, 2017, 10:20am UTC](https://discourse.julialang.org/t/how-to-determine-if-an-object-is-callable/2897/4 "2017-03-27T10:20:32Z")

</div>

`Bool` can convert only 0 and 1 to a boolean:

```julia
julia> callable(sin)
ERROR: InexactError()
Stacktrace:
 [1] convert(::Type{Bool}, ::Int64) at ./bool.jl:7
 [2] callable(::Function) at ./REPL[9]:1

```

---

<div class="post-metadata">

**Author:** ![tamasgal](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamasgal/32/27946_2.png) [@tamasgal](https://discourse.julialang.org/u/tamasgal)\
**Post date:** [March 27, 2017, 12:20pm UTC](https://discourse.julialang.org/t/how-to-determine-if-an-object-is-callable/2897/5 "2017-03-27T12:20:08Z")

</div>

Oh ok, thanks 😉

---

<div class="post-metadata">

**Author:** ![leferrad](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/leferrad/32/6105_2.png) [@leferrad](https://discourse.julialang.org/u/leferrad)\
**Post date:** [June 22, 2020, 9:46pm UTC](https://discourse.julialang.org/t/how-to-determine-if-an-object-is-callable/2897/6 "2020-06-22T21:46:51Z")

</div>

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
julia> sin isa Function
true

```

---

<div class="post-metadata">

**Author:** ![jling](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jling/32/212909_2.png) [@jling](https://discourse.julialang.org/u/jling)\
**Post date:** [June 22, 2020, 9:54pm UTC](https://discourse.julialang.org/t/how-to-determine-if-an-object-is-callable/2897/7 "2020-06-22T21:54:26Z")

</div>

wow dude… the thread is 3 years old

---

<div class="post-metadata">

**Author:** ![yuyichao](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yuyichao/32/20_2.png) [@yuyichao](https://discourse.julialang.org/u/yuyichao)\
**Post date:** [June 22, 2020, 9:59pm UTC](https://discourse.julialang.org/t/how-to-determine-if-an-object-is-callable/2897/8 "2020-06-22T21:59:59Z")

</div>

Also don’t do that.

---

<div class="post-metadata">

**Author:** ![leferrad](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/leferrad/32/6105_2.png) [@leferrad](https://discourse.julialang.org/u/leferrad)\
**Post date:** [June 22, 2020, 11:49pm UTC](https://discourse.julialang.org/t/how-to-determine-if-an-object-is-callable/2897/10 "2020-06-22T23:49:43Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![jling](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jling/32/212909_2.png) [@jling](https://discourse.julialang.org/u/jling)\
**Post date:** [June 22, 2020, 11:52pm UTC](https://discourse.julialang.org/t/how-to-determine-if-an-object-is-callable/2897/11 "2020-06-22T23:52:04Z")

</div>

just make that a function… as suggested

---

<div class="post-metadata">

**Author:** ![leferrad](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/leferrad/32/6105_2.png) [@leferrad](https://discourse.julialang.org/u/leferrad)\
**Post date:** [June 22, 2020, 11:55pm UTC](https://discourse.julialang.org/t/how-to-determine-if-an-object-is-callable/2897/12 "2020-06-22T23:55:36Z")

</div>

Well, of course I assumed that the object is an actual function, so yes…

---

<div class="post-metadata">

**Author:** ![jling](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jling/32/212909_2.png) [@jling](https://discourse.julialang.org/u/jling)\
**Post date:** [June 22, 2020, 11:56pm UTC](https://discourse.julialang.org/t/how-to-determine-if-an-object-is-callable/2897/13 "2020-06-22T23:56:41Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![leferrad](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/leferrad/32/6105_2.png) [@leferrad](https://discourse.julialang.org/u/leferrad)\
**Post date:** [June 23, 2020, 12:45am UTC](https://discourse.julialang.org/t/how-to-determine-if-an-object-is-callable/2897/14 "2020-06-23T00:45:02Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![yuyichao](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yuyichao/32/20_2.png) [@yuyichao](https://discourse.julialang.org/u/yuyichao)\
**Post date:** [June 23, 2020, 1:05am UTC](https://discourse.julialang.org/t/how-to-determine-if-an-object-is-callable/2897/15 "2020-06-23T01:05:13Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![yuyichao](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yuyichao/32/20_2.png) [@yuyichao](https://discourse.julialang.org/u/yuyichao)\
**Post date:** [June 23, 2020, 1:06am UTC](https://discourse.julialang.org/t/how-to-determine-if-an-object-is-callable/2897/16 "2020-06-23T01:06:32Z")

</div>

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.)

---

<div class="post-metadata">

**Author:** ![leferrad](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/leferrad/32/6105_2.png) [@leferrad](https://discourse.julialang.org/u/leferrad)\
**Post date:** [June 23, 2020, 1:23am UTC](https://discourse.julialang.org/t/how-to-determine-if-an-object-is-callable/2897/17 "2020-06-23T01:23:38Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![giordano](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/giordano/32/2166_2.png) [@giordano](https://discourse.julialang.org/u/giordano)\
**Post date:** [June 23, 2020, 1:31am UTC](https://discourse.julialang.org/t/how-to-determine-if-an-object-is-callable/2897/18 "2020-06-23T01:31:44Z")

</div>

Python functions from `PyCall` aren’t `Function`s. Also constructors of Julia structs aren’t `Function`s

---

<div class="post-metadata">

**Author:** ![jling](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jling/32/212909_2.png) [@jling](https://discourse.julialang.org/u/jling)\
**Post date:** [June 23, 2020, 1:34am UTC](https://discourse.julialang.org/t/how-to-determine-if-an-object-is-callable/2897/19 "2020-06-23T01:34:25Z")

</div>

basically,  
 ![image](https://global.discourse-cdn.com/julialang/original/3X/7/a/7a6d4461a27adb617382a0db01735f602537e9b5.png)

---

<div class="post-metadata">

**Author:** ![leferrad](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/leferrad/32/6105_2.png) [@leferrad](https://discourse.julialang.org/u/leferrad)\
**Post date:** [June 23, 2020, 1:35am UTC](https://discourse.julialang.org/t/how-to-determine-if-an-object-is-callable/2897/20 "2020-06-23T01:35:56Z")

</div>

Great, both are good examples! Thanks for the detailed answers! I will take this into account then.

---

<div class="post-metadata">

**Author:** ![yuyichao](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yuyichao/32/20_2.png) [@yuyichao](https://discourse.julialang.org/u/yuyichao)\
**Post date:** [June 23, 2020, 1:51pm UTC](https://discourse.julialang.org/t/how-to-determine-if-an-object-is-callable/2897/21 "2020-06-23T13:51:31Z")

</div>

> [@leferrad](#):
>
> 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.

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.

[Next page](https://discourse.julialang.org/t/how-to-determine-if-an-object-is-callable/2897.md?page=2)
