How are you type alias?

In some (mostly functional) languages, you can do something like this:

type row = list(datum) 

or

 type row = [datum] 

So that we can build such things:

 type row = [datum] type table = [row] type database = [table] 

Is there a way to do this in Python? You can do this using classes, but Python has some functional aspects, so I was wondering if this could be done easier.

+9
source share
3 answers

Python is dynamically typed. While Lucas R.'s answer is correct for the purpose of hinting like (which, in turn, can be used for static analysis and casting), strictly speaking, you do not need to do anything to do this work. Just create 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, ...] 

The types of hints are really useful, as they can help document how your code behaves, and they can be checked both statically and at runtime. But you have nothing to do if it is inconvenient.

-3
source

With Python 3.5 you can use typing .

Quoting documents, an Alias ​​type is determined by assigning the type to an alias:

 Vector = List[float] 

To learn more about applying types in Python, you can check out PEP: PEP483 and PEP484 .

Python historically used duck typing instead of strong typing and did not have a built-in way to force types before release 3.5.

+29
source

What about something like row = lambda datum: list(datum) ? There is no real introspection of support there, but it is a very simple way of type "aliases", set by the Python application for duck text input. And it is functional! Kinda

-3
source

Source: https://habr.com/ru/post/1233376/


All Articles