0

I didn't find how to make a generic function in Python (not classes)

For instance, in .net I can have something like this:

static T methodName<T>(string v)
{
    # foo
    # bar
}

In Python it would be something like:

from typing import TypeVar, Generic

T = TypeVar('T')

def get_info_from_toml[T](section: str) -> T:
    pass

As I'm using mypy to check types, it makes my code coherent to the checks and avoids the need for arbitrary casts.

Is it possible to have something similar to it in Python?

Rodrigo Farias Rezino
  • 2,687
  • 3
  • 33
  • 60
  • 1
    Are you asking how to define a function in python? You can use `def {func_name} (params): # do something` – BanjoMan Aug 09 '22 at 07:51
  • Can you describe the functions you want to implement in python with more specific examples? Python doesn't have generics, but that doesn't mean it can't do what generics can. – Mechanic Pig Aug 09 '22 at 07:52
  • You understand that Python is a dynamically typed language - there are no need for generics, unless you are asking about *type hinting*. It isn't clear exactly what you need – juanpa.arrivillaga Aug 09 '22 at 08:00

0 Answers0