Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Define a list with type

Tags:

python

I come from Java, and I want to do some data transfer objects (DTOs) like this:

class ErrorDefinition():
    code = ''
    message = ''
    exception = ''

class ResponseDTO():
    sucess = True
    errors = list() # How do I say it that it is directly of the ErrorDefinition() type, to not import it every time that I'm going to append an error definition?

Or is there a better way to do this?

like image 798
panchicore Avatar asked Dec 17 '09 20:12

panchicore


People also ask

What is list and its type in Python?

Lists are used to store multiple items in a single variable. Lists are one of 4 built-in data types in Python used to store collections of data, the other 3 are Tuple, Set, and Dictionary, all with different qualities and usage.

How do you define a list?

A list can be defined as a collection of values or items of different types. The items in the list are separated with the comma (,) and enclosed with the square brackets [].

Is a list a data type?

List is a collection data type. It allows multiple values to be stored within the same field.

What is a type in Python?

The type() function is used to get the type of an object. Python type() function syntax is: type(object) type(name, bases, dict)


1 Answers

You can type a list as python 3.8.4

https://docs.python.org/3/library/typing.html

from typing import List
vector:List[float] = list()

or

from typing import List
vector:List[float] = []

Python is dynamically typed however https://www.python.org/dev/peps/pep-0484/ Typing makes it easier to read code, easier understand it, make efficient use of your IDE tooling and create higher quality software. I guess that's why it's in the PEP.

like image 135
Shaun Ryan Avatar answered Sep 19 '22 16:09

Shaun Ryan