Skip to content
Advertisement

How to define multiple functions in single function declaration?

In Fortran you can do something like this:

JavaScript

Can something like this be done in Python?

I want to write a function like this for float, int and str.

JavaScript

Which I will then pass to the parser:

JavaScript

Advertisement

Answer

The types themselves are first-class objects, so you can pass them as arguments to a single function.

First, define a function that defines the common function, closing over the desired type.

JavaScript

Then use make_type to create the necessary argument for the type argument to `add_argument.

JavaScript
User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement