Home > front end >  Python equivalent for typedef
Python equivalent for typedef

Time:10-05

What is the python way to define a (non-class) type like:

typedef Dict[Union[int, str], Set[str]] RecordType

CodePudding user response:

This would simply do it?

from typing import Dict, Union, Set

RecordType = Dict[Union[int, str], Set[str]]


def my_func(rec: RecordType):
    pass


my_func({1: {'2'}})
my_func({1: {2}})

This code will generate a warning from your IDE on the second call to my_func, but not on the first. As @sahasrara62 indicated, more here https://docs.python.org/3/library/stdtypes.html#types-genericalias

  • Related