# Function parameter speculation in runtime

**URL:** https://discourse.julialang.org/t/function-parameter-speculation-in-runtime/41523
**Category:** General Usage
**Tags:** python, function
**Created:** [June 16, 2020, 2:09pm UTC](https://discourse.julialang.org/t/function-parameter-speculation-in-runtime/41523 "2020-06-16T14:09:05Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![dinskid](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dinskid/32/15264_2.png) [@dinskid](https://discourse.julialang.org/u/dinskid)
#### Post date: [June 16, 2020, 2:09pm UTC](https://discourse.julialang.org/t/function-parameter-speculation-in-runtime/41523/1 "2020-06-16T14:09:05Z")

</div>

I need a way to imitate the functionality of the `inspect` module from python.  
I want to know the list of parameters of a functions, check if there are default values or not, and check if it accepts kwargs or not.

Sample python code

```julia
import inspect
def func(a, b=10, **kwargs):
     pass
allArgs = inspect.getfullargspec(func)

```

**allArgs is an object containing the required information**  
For more information: [inspect — Inspect live objects — Python 3.10.6 documentation](https://docs.python.org/3/library/inspect.html#inspect.getargspec)

I want to achieve the same in Julia. Is there any library to do the same or is there some hack to do the same?

Any help is appreciated. Thanks in advance  
Dinesh Vikram V

---

<div class="post-metadata">

### Author: ![ablaom](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ablaom/32/4889_2.png) [@ablaom](https://discourse.julialang.org/u/ablaom)
#### Post date: [June 22, 2020, 7:38am UTC](https://discourse.julialang.org/t/function-parameter-speculation-in-runtime/41523/2 "2020-06-22T07:38:41Z")

</div>

To get all the possible signatures of a function `foo` you can do `methods(foo)`. This does not tell you default values of key-word arguments. I don’t know how to get those.

```julia
julia> foo(x; y=5) = x+y
foo (generic function with 1 method)

julia> foo(x::AbstractVector, z::Int) = z*x
foo (generic function with 2 methods)

julia> methods(foo)
# 2 methods for generic function "foo":
[1] foo(x::AbstractArray{T,1} where T, z::Int64) in Main at REPL[2]:1
[2] foo(x; y) in Main at REPL[1]:1

```

---

<div class="post-metadata">

### Author: ![Sebastian\_Vollmer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sebastian_vollmer/32/31027_2.png) [@Sebastian\_Vollmer](https://discourse.julialang.org/u/Sebastian_Vollmer)
#### Post date: [June 27, 2020, 8:40am UTC](https://discourse.julialang.org/t/function-parameter-speculation-in-runtime/41523/3 "2020-06-27T08:40:14Z")

</div>

One way would be borrowing from MLJ e.g. [Learning networks](https://alan-turing-institute.github.io/DataScienceTutorials.jl/getting-started/learning-networks-2/index.html#using_the_from_network_macro) - multiple algorithms can be composed with neatly keeping track of all parameters.

One way around this would be to use callable structs - it is very easy to built up nested parameter spaces, see [nested parameters in MLJ](https://github.com/alan-turing-institute/MLJBase.jl/blob/b62fa7b9d15ef6102b29a179e360d49d621d4171/src/parameter_inspection.jl#L24).

@ChrisRackauckas Your feedback would be greatly appreciated. Not sure any of this would be easily compatible with the SciML ecosystem.

---

<div class="post-metadata">

### Author: ![ChrisRackauckas](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chrisrackauckas/32/77_2.png) [@ChrisRackauckas](https://discourse.julialang.org/u/ChrisRackauckas)
#### Post date: [June 27, 2020, 11:50am UTC](https://discourse.julialang.org/t/function-parameter-speculation-in-runtime/41523/4 "2020-06-27T11:50:57Z")

</div>

Callable structs would definitely be a better way to handle this. You should definitely only resort to reading the method table when you really really need to.
