Function parameter speculation in runtime

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

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

allArgs is an object containing the required information
For more information: 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

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

1 Like

One way would be borrowing from MLJ e.g. Learning networks - 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.

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

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.

1 Like