Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What are the problems with Python dynamic types

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly introduces the relevant knowledge of "what are the problems of Python dynamic types". The editor shows you the operation process through actual cases, and the operation method is simple, fast and practical. I hope this article "what are the problems of Python dynamic types" can help you solve the problem.

Python as a dynamic language, the code is concise and flexible, leaving aside the running efficiency, but some problems are also true, such as:

1. The smart prompt of IDE is more chicken. For example, a string has a startswith method, and you can easily ignore the "s" in the middle. If you don't have the help of IDE, you have to check a document. (in fact, PyCharm is already very smart, even if there is no type declaration. )

2. Most errors can only be found when the program is running, and only simple syntax errors can be found in the compilation process.

3. The API call depends on the documentation. Although we can use docstring, your docstring may not be updated synchronously after the code is updated.

These problems are more prominent in large-scale projects, especially in multi-person cooperation projects. Therefore, it is particularly important to follow the code specification, Code Review, if you can standardize the code from the syntax level is undoubtedly the most cost-effective, so there is a type hint (Type Hints) in Python3.5, that is, PEP484. When defining a function, you can specify the return value type of the function and the type of parameter.

Previously defined a function that could receive any type of data:

Def greeting (name):

Return "Hello" + name

Greeting ("bob")

Greeting (1)

There is no error before the program runs, although we know that strings and numbers do not support the "+" operation.

In Python3.5, it is written in Type Hint as follows:

Def greeting (name: str)-> str:

Return 'Hello' + name

The above is the writing of static types. There are more ": str" and "- > str". The former is used to indicate the type of name, and the latter refers to the type of value returned by the function. In the process of writing code, IDE can also prompt us to write it incorrectly:

In addition to IDE, we have a more powerful static type checking tool called mypy, which is also implemented by Guido, the father of Python.

# install mypy first

Pip install mypy

$mypy test.py

Test.py:4: error: Argument 1 to "greeting"

Has incompatible type "int"; expected "str"

With the type hint, Python has a better effect in code invocation, refactoring, and even static analysis, which not only reduces the burden of type checking during development, but more importantly, because of the type hint, there is a unified reference standard for all kinds of intelligent hints, refactoring and other functions that can not be done well in Python integration development tools in the past.

In a sense, the type hint is only an auxiliary function. Although we add the data type hint, for the Python interpreter, it will directly ignore the type hint information, even if the type is wrong, it will not prevent the program from running.

As for the type declaration of a variable, it can be explained by type annotation in PEP484, that is, the type of the variable is described in the way of annotation, for example:

From typing import List

X = [] # type: List [Employee]

Y = [1,2] # type: List [int]

Y.append ("a")

In the proposal of Python3.6, that is, PEP526, further optimizations are made for variable annotations to make type declarations part of the syntax, which makes them more readable than comments, for example:

My_var: int # variables declared as integers

My_var = 5 # pass type check

Other_var: int ='a'# assign a string to an integer type variable, the inspector will report an error, but there will be no problem with the interpreter running

Print (other_var)

Run mypy

Mypy xx.py # run Type Checker

Xx.py:3: error: Incompatible types in assignment (expression has type "str", variable has type "int")

Python test.py # run the interpreter

With powerful type hints, do you dare to say that refactoring code is a crematorium? However, I think dynamic languages are becoming more and more bloated, such as:

T = TypeVar ('T')

S = TypeVar ('S')

Class Foo (General [T]):

Def method (self, x: t, y: s)-> S:

# Body

This makes a simple and elegant language as bloated as Java, except that type hints are only an optional operation in Python and are completely invisible to the interpreter. And this kind of writing is not the mainstream practice of Python, you can take a look at the source code of the mainstream framework, rarely use this feature.

This is the end of the content about "what are the problems with Python dynamic types". Thank you for reading. If you want to know more about the industry, you can follow the industry information channel. The editor will update different knowledge points for you every day.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

  • Jenkins+Jmeter+Ant generates test report

    © 2024 shulou.com SLNews company. All rights reserved.

    12
    Report