Python differs from Java in that it is a dynamic language: When creating objects, we do not need to declare the types of objects. While this is a great convenience for the country, it is difficult for the IDE to identify these types intelligently and give the appropriate code hints. As the following illustration shows, we load the JSON data into a list db via JSON, but the IDE does not give the appropriate hints when invoking the DB method, which can greatly reduce development efficiency for beginners who are not familiar with Python functions or are unfamiliar with them.
In fact, Pycharm provides a method called the type hint to give the IDE a type hint to get code hints more easily. Reference site: https://www.jetbrains.com/help/pycharm/type-hinting-in-pycharm.html
Here are a few ways to get code hints (note: The following methods are valid for Python 3.7 and above): 1 Add type hints in your own Write method (available under Python 3.7)
When you define a method, you can set the type for the parameter, and if you set the type, you can get the appropriate hints in the following code, such as:
This will allow you to get the hint smoothly.
Also, if the method has a return value, you can set the type for the return of the method, so that when you use the method to get the object, you can get the appropriate hints in the following code, such as:
2 use annotations to indicate the local variable type (available under Python 2.7)
If you encounter the use of a third-party package, and the person who provided the package does not give the complete type hint, and then rewrite their method is obviously unrealistic, you can prompt the IDE by adding annotations after the object. Like what:
The annotation format is:
# type: corresponding type
3 declaring types in DocString (available under Python 2.7)
Indicates that the type of a local variable uses: Type or @type docstrings specify a local variable type
You can specify the type of the property in the DocString of class
Specify return type use: Rtype or @rtype docstrings specify return type
Specifies that the parameter type uses: Type or @type docstrings to specify the parameter type
4 reference https://my.oschina.net/pierrecai/blog/1142711 https://www.jetbrains.com/help/pycharm/ Type-hinting-in-product.html