# Python \_\_call\_\_ equivalent in Julia

**URL:** <https://discourse.julialang.org/t/python-call-equivalent-in-julia/36905>\
**Category:** New to Julia\
**Tags:** question\
**Created:** [April 2, 2020, 2:43pm UTC](https://discourse.julialang.org/t/python-call-equivalent-in-julia/36905 "2020-04-02T14:43:35Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![HiroIshida](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/hiroishida/32/7424_2.png) [@HiroIshida](https://discourse.julialang.org/u/HiroIshida)\
**Post date:** [April 2, 2020, 2:43pm UTC](https://discourse.julialang.org/t/python-call-equivalent-in-julia/36905/1 "2020-04-02T14:43:35Z")

</div>

In python, one can use an instance as a function by defining ` __call__ ` as following code. I want to do the similar thing in Julia by using a struct instead of a class. Could anyone tell me how to do this?

```julia
class Mul:
    def __init__ (self, a):
        self.a = a
    def __call__ (self, x):
        return self.a * x

if __name__ ==' __main__':
    m = Mul(2)
    m(2) # equals to 4

```

---

<div class="post-metadata">

**Author:** ![mauro3](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mauro3/32/292_2.png) [@mauro3](https://discourse.julialang.org/u/mauro3)\
**Post date:** [April 2, 2020, 2:48pm UTC](https://discourse.julialang.org/t/python-call-equivalent-in-julia/36905/2 "2020-04-02T14:48:39Z")

</div>

Like so:

```julia
julia> struct A
       a
       end

julia> (a::A)(x) = a.a*x

julia> A(2)(2)
4

```

PS: but don’t do OO programming in Julia 😉

---

<div class="post-metadata">

**Author:** ![Mason](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mason/32/2423_2.png) [@Mason](https://discourse.julialang.org/u/Mason)\
**Post date:** [April 2, 2020, 10:16pm UTC](https://discourse.julialang.org/t/python-call-equivalent-in-julia/36905/3 "2020-04-02T22:16:37Z")

</div>

> [@mauro3](#):
>
> PS: but don’t do OO programming in Julia 😉

I know many don’t enjoy semantics nearly so much as I do, but multiple dispatch is considered to be a form of OO programming by many. It’s just not class-based object orientation which seems to have taken over as the one and only form of OO in many people’s minds.

---

<div class="post-metadata">

**Author:** ![frtps](https://avatars.discourse-cdn.com/v4/letter/f/73ab20/32.png) [@frtps](https://discourse.julialang.org/u/frtps)\
**Post date:** [April 3, 2020, 1:13am UTC](https://discourse.julialang.org/t/python-call-equivalent-in-julia/36905/4 "2020-04-03T01:13:01Z")

</div>

Here’s a non-OO alternative:

```julia
make_mul(a) = x-> a*x

mul = make_mul(2)

mul(2)
4

```

---

<div class="post-metadata">

**Author:** ![Mason](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mason/32/2423_2.png) [@Mason](https://discourse.julialang.org/u/Mason)\
**Post date:** [April 3, 2020, 1:35am UTC](https://discourse.julialang.org/t/python-call-equivalent-in-julia/36905/5 "2020-04-03T01:35:44Z")

</div>

I disagree that this is less OO than what @mauro3 showed (regardless of what the term OO actually means). The closure you show in your code is implemented as a `struct` storing `a`. The only real difference between the two examples is that yours is one line shorter but produces intermediate outputs which might be less legible and shouldn’t be dispatched on.

```julia
julia> dump(mul)
#13 (function of type var"#13#14"{Int64})
  a: Int64 2

```

* * *

edited to make the tone seem less aggressive (did not mean to be aggressive)

---

<div class="post-metadata">

**Author:** ![frtps](https://avatars.discourse-cdn.com/v4/letter/f/73ab20/32.png) [@frtps](https://discourse.julialang.org/u/frtps)\
**Post date:** [April 3, 2020, 1:39am UTC](https://discourse.julialang.org/t/python-call-equivalent-in-julia/36905/6 "2020-04-03T01:39:29Z")

</div>

Fair enough. I was just showing an alternative, if it is actually OO I’m not going to argue.
