# API to retrieve the docstring of a function

**URL:** https://discourse.julialang.org/t/api-to-retrieve-the-docstring-of-a-function/10722
**Category:** General Usage
**Created:** [May 5, 2018, 8:15am UTC](https://discourse.julialang.org/t/api-to-retrieve-the-docstring-of-a-function/10722 "2018-05-05T08:15:20Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![essenciary](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/essenciary/32/210469_2.png) [@essenciary](https://discourse.julialang.org/u/essenciary)
#### Post date: [May 5, 2018, 8:15am UTC](https://discourse.julialang.org/t/api-to-retrieve-the-docstring-of-a-function/10722/1 "2018-05-05T08:15:20Z")

</div>

I would like to programmatically get the docstring associated to a function – is that possible?

On a related note, how can we apply reflection and introspection techniques to functions? In Julia functions are first-class, but they don’t seem to behave like standard objects (as least the way one might expect by comparison with other languages). What kind of beast is this? 👹

```julia
julia> g = (x) -> 2x
(::#23) (generic function with 1 method)

julia> typeof(g)
##23#24

julia> isa(g, Function)
true

julia> f = Function()
ERROR: MethodError: no constructors have been defined for Function

julia> fieldnames(typeof(g))
0-element Array{Symbol,1}

julia> methods(g)
# 1 method for generic function "(::#23)":
(::##23#24)(x) in Main at REPL[8]:1

julia> methods(Function)
# 1 method for generic function "(::Type)":
(::Type{T})(arg) where T in Base at sysimg.jl:77

```

---

<div class="post-metadata">

### Author: ![jonathanBieler](https://avatars.discourse-cdn.com/v4/letter/j/82dd89/32.png) [@jonathanBieler](https://discourse.julialang.org/u/jonathanBieler)
#### Post date: [May 5, 2018, 9:04am UTC](https://discourse.julialang.org/t/api-to-retrieve-the-docstring-of-a-function/10722/2 "2018-05-05T09:04:05Z")

</div>

```julia
Base.doc(Base.Docs.Binding(Base.Docs.current_module(),:rand))

```

You can find this using `macroexpand( :(@doc rand) )`.

Otherwise you often want to look at methods instead of functions:

```julia
julia> m = collect(methods(rand))[1]
rand(rd::RandomDevice, ::Type{T}) where T<:Union{Bool, Int128, Int16, Int32, Int
64, Int8, UInt128, UInt16, UInt32, UInt64, UInt8} in Base.Random at random.jl:40

julia> fieldnames(m)
19-element Array{Symbol,1}:
 :name
 :module
 :file
 :line
 :sig
 :min_world
 :ambig
 :specializations
 :sparam_syms
 :source
 :unspecialized
 :generator
 :roots
 :invokes
 :nargs
 :called
 :isva
 :isstaged
 :pure

```

---

<div class="post-metadata">

### Author: ![essenciary](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/essenciary/32/210469_2.png) [@essenciary](https://discourse.julialang.org/u/essenciary)
#### Post date: [May 5, 2018, 10:12am UTC](https://discourse.julialang.org/t/api-to-retrieve-the-docstring-of-a-function/10722/3 "2018-05-05T10:12:39Z")

</div>

Excellent, thank you!
