pythontypesgenetic-programming

How to get the parameters' type and return type of a function?


I'm trying to implement strong type genetic programming in python.

Is there something like these sample?

def funcA(a,b):
  return a + b
return_type(funcA)

output: <class 'Integer'>

and

def funcA(a,b):
  return a + b
parameter_type(funcA)

output: [<class 'Integer'>,<class 'Integer'>]

update:

I'm trying to generate python's expression and avoiding something cannot be evaluated like this:

funcA(20, funcA(True, "text"))

Solution

  • Python 3 introduces function annotations. By themselves they don't do anything, but you can write your own enforcement:

    def strict(fun):
        # inspect annotations and check types on call
    
    @strict
    def funcA(a: int, b: int) -> int:
        return a + b