Issue
In some (mostly functional) languages you can do something like this:
type row = list(datum)
or
type row = [datum]
So that we can build things like this:
type row = [datum]
type table = [row]
type database = [table]
Is there a way to do this in Python? You could do it using classes, but Python has quite some functional aspects so I was wondering if it could be done an easier way.
Solution
Since Python 3.5 you may use typing module.
Quoting docs, A type alias is defined by assigning the type to the alias:
# Python 3.5-3.8
from typing import List
Vector = List[float]
# Python 3.9+
Vector = list[float] # No import needed, lower case l
To learn more about enforcing types in Python you may want to get familiar with PEPs: PEP483 and PEP484.
Python historically was using duck-typing instead of strong typing and hadn't built-in way of declaring types before 3.5 release.
Answered By - Ćukasz Rogalski
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.