How do you alias a type?

Lara :

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.

Kevin :

Python is dynamically typed. While Łukasz R.'s answer is correct for type hinting purposes (which can in turn be used for static analysis and linting), strictly speaking, you do not need to do anything to make this work. Just construct your lists like this and assign them to variables:

foo_table = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
bar_table = ...
foo_database = [foo_table, bar_table, ...]

Type hints are genuinely useful, because they can help document how your code behaves, and they can be checked both statically and at runtime. But there's nothing forcing you to do so if it is inconvenient.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How do you alias a type iny Python?

How do you SELECT attributes of a custom type via a table alias?

How do you declare a type alias in a f# constructor?

How do you use the Typescript compiler's Typechecker to get the declared (alias) type, rather than the resolved type?

(Play 2.5) How do you define json format for type alias of an Option?

When do you use an interface over a type alias in flow?

How do I make a generic type alias?

How do I type alias a templated struct

How to do pattern matching with alias type

c++ template meta programming: how do you define the type trait "is_xxxxx_v" alias for my custom trait?

How do I unwrap a generic type alias from a union type which makes the type alias more specific?

How do you alias an imported package in a JSP file?

How do you resolve a docker alias using node?

How do you make an alias or function that retains tab completion?

How do you wrap executable commands so that they work in an alias or function?

How do you point API Gateway to a lambda Alias in CDK?

How do you set alias sudo='nocorrect sudo ' correctly?

How do you invoke alias from ruby code for zsh shell?

How do you remove or delete a global alias from Zshell?

How do I create some variable type alias in Java

How do I create a type alias for an array of char with a fixed size?

How to do a type alias reassignment in typescript without intermediate variables?

How do I implement Debug for a struct containing a function type alias?

How do I "generify" a closure type alias in Swift?

How do I initialize a type alias like this in Elm?

How do you define a function type in Haskell?

How do you show the type hierarchy of a value?

How do you import the timestamp type?

How do you type a class decorator in typescript?