Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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

>Solution :

Its called type hinting (or I also heared type anotation).

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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading