Automatic hints for Python objects in Pycharm __python

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.