Search code examples
pythonnumpypython-typing

What are the valid TypeVar types in Python 3.x?


Over the past several weeks, I've been taking an online class where we have to solve various problems using a variety of numerical methods. One of these was a Kalman filter, and the YT video I watched included the person using the following syntax:

def __init__(self, var_a: float,
                 var_b: float,
                 var_c: float,
                 var_d: float) -> None:

This seems like it's very useful for an end user to know what type of value to input to get the function started.

I've done a lot of searching on what the other valid types are, and cannot seem to Google the correct term. For example, "float" obviously works, and I know "list" works; however, "array" does not. These types may also be used to specify something other than "None" in what the function returns as well.

Does someone know of a reference, or what term I need to search?


Solution

  • Its called type hinting or type annotation.

    You can use all types available in Python. Built in that is (a.o.):

    • int: Integer
    • float: Floating-point number
    • str: String
    • bool: Boolean
    • dict: Dictionary
    • tuple: Tuple
    • set: Set

    However you can also define types yourself by defining a class. Also many modules give you more types. So for example numpy arrays:

    import numpy as np
    
    def function(a: np.ndarray):
        return a
    

    The TypeVar type is used when you want to indicate that a type is a generic type parameter.