# Include\_string vs getfield

**URL:** <https://discourse.julialang.org/t/include-string-vs-getfield/16814>\
**Category:** General Usage\
**Created:** [October 26, 2018, 2:36am UTC](https://discourse.julialang.org/t/include-string-vs-getfield/16814 "2018-10-26T02:36:26Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![LeoK987](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/leok987/32/4880_2.png) [@LeoK987](https://discourse.julialang.org/u/LeoK987)\
**Post date:** [October 26, 2018, 2:36am UTC](https://discourse.julialang.org/t/include-string-vs-getfield/16814/1 "2018-10-26T02:36:27Z")

</div>

I used to use include\_string to call a function with the name defined by the string. But it seems like a hack, doesn’t it? Now getfield appears more sensible, doesn’t it? But I don’t know what behind these two functions. Which is more efficient? Can anyone explain?

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [October 26, 2018, 3:01am UTC](https://discourse.julialang.org/t/include-string-vs-getfield/16814/2 "2018-10-26T03:01:31Z")

</div>

Why are you calling functions addressed by strings in a performance-sensitive context?

---

<div class="post-metadata">

**Author:** ![LeoK987](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/leok987/32/4880_2.png) [@LeoK987](https://discourse.julialang.org/u/LeoK987)\
**Post date:** [October 26, 2018, 3:08am UTC](https://discourse.julialang.org/t/include-string-vs-getfield/16814/3 "2018-10-26T03:08:04Z")

</div>

In various situations.

1. I have a bunch of functions with the same signature, so I parse the script file and compile them into s String Array, and then run them one by one.

2. I get DataFrame files which have the names of these functions.

Are there better ways doing these?

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [October 26, 2018, 3:11am UTC](https://discourse.julialang.org/t/include-string-vs-getfield/16814/4 "2018-10-26T03:11:37Z")

</div>

Could you be more specific? Why do you have a data file with a bunch of function names?

---

<div class="post-metadata">

**Author:** ![LeoK987](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/leok987/32/4880_2.png) [@LeoK987](https://discourse.julialang.org/u/LeoK987)\
**Post date:** [October 26, 2018, 3:36am UTC](https://discourse.julialang.org/t/include-string-vs-getfield/16814/5 "2018-10-26T03:36:02Z")

</div>

1. in a file called myfunc.jl, I have the bunch of functions defined as:

```julia
function f1(x)
    such such ...
end

function f2(x)
    such such ...
end
....

```

This file is include the main script. But the main script also parses this file to compile all the function names into a array, then it calls them one by one.

1. Some outputs including the function names are written in a csv data file for further analysis, where the function name are read in as String, by which the function is called upon.

I hope this clarifies.

---

<div class="post-metadata">

**Author:** ![kristoffer.carlsson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kristoffer.carlsson/32/22_2.png) [@kristoffer.carlsson](https://discourse.julialang.org/u/kristoffer.carlsson)\
**Post date:** [October 26, 2018, 2:12pm UTC](https://discourse.julialang.org/t/include-string-vs-getfield/16814/6 "2018-10-26T14:12:49Z")

</div>

So you want to dynamically call functions that are parsed from a file?

Some options that are likely much faster:

- Make a `Dict{String, Function}`, put your functions in there and index with the string you parse from the file. You can make a macro that adds your function to the dictionary when it is declared, if that is convenient.
- Just switch on the string, e.g.

```julia
func = Symbol(readline(f))
if line == :foo
    foo(a)
elseif line == :bar
    bar(a)
...

```

`include_string` or `getfield` are not very useful for your purpose.

---

<div class="post-metadata">

**Author:** ![LeoK987](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/leok987/32/4880_2.png) [@LeoK987](https://discourse.julialang.org/u/LeoK987)\
**Post date:** [October 26, 2018, 3:07pm UTC](https://discourse.julialang.org/t/include-string-vs-getfield/16814/7 "2018-10-26T15:07:36Z")

</div>

Thanks for the tips.

When I make a Dict, don’t I have to do one of the following?

```julia
Dict("f1", include_string(Main, "f1"))
Dict("f1", getfield(Main, Symbol("f1")))

```

When you said " `include_string` or `getfield` are not very useful for your purpose", do you mean that they are very inefficient?

---

<div class="post-metadata">

**Author:** ![kristoffer.carlsson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kristoffer.carlsson/32/22_2.png) [@kristoffer.carlsson](https://discourse.julialang.org/u/kristoffer.carlsson)\
**Post date:** [October 26, 2018, 3:11pm UTC](https://discourse.julialang.org/t/include-string-vs-getfield/16814/8 "2018-10-26T15:11:53Z")

</div>

> [@LeoK987](#):
>
> Dict(“f1”, include\_string(Main, “f1”))

You do

```julia
Dict("f1" => Main.f1)

```
