Questions about Python from typing import list

Asked 2 years ago, Updated 2 years ago, 20 views

For #1,

from typing import List

def average(L : list[float]) -> float:
    print("HELLO")

Normal operation. For #2

from typing import List

def average(L : Gist[float]) -> float:
    print("HELLO")

If

Traceback (most recent call last):
  File "<pyshell#86>", line 1, in <module>
    def average(L : Gist[float]) -> float:
NameError: name 'Gist' is not defined 

1. I print out a message, but what I'm curious about is that if the type notation is not set according to the type, an error message appears after L:, and if the type is adjusted and the value is put in the factor, does it not send an error message?

Question number two

from typing import List
def average(L : list[float]) -> float:
    print("HELLO")
 from typing import List

 def average(L : List[float]) -> float:
    print("HELLO")

1. What do you get from import list in these two codes? 2. Do I have to write L:list and L:list separately?

python

2022-09-20 08:42

1 Answers

Python was created in a dynamic language, so there was no type designation in the beginning.

However, as the version went up, the type-hinting function was added, and as it was added, it was developed, so there were some imperfections. The list of typing, Dict, etc. are made and developed in that way. It's a bit vague to import the type and use type.

Types such as list and dict are included in the official Python after typing. Functionally, it tells the type of variable, so it seems to be used for static analysis. The restriction is smaller than other Kangta language.


2022-09-20 08:42

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.