Get the App
SLTechnology News&Howtos  ›  Development  › 

How to introduce import and from... Import

Shulou Source: shulou.com Published: 2022-06-03 05:31:58 09月16日 Update

This article introduces you how to introduce import and from... import, the content is very detailed, interested friends can refer to reference, I hope to help you.

Generally import and from... import... Import module.

Take the file code in spam.py below as an example.

import module name

import Three things happen when you import a module for the first time:

Create a module namespace based on the module

Execute the file corresponding to the module, and throw the names generated during execution into the module namespace.

Get a module name in the current execution file

Duplicate imports of modules create good results directly before drinking, and do not execute the module's files repeatedly, i.e. duplicate imports occur: spam=spam= memory address of the module namespace

from module name import specific function

from... import... Three things happen when you import a module for the first time:

Create a module namespace based on the module

Execute the file corresponding to the module, and throw the names generated during execution into the namespace of the module in the namespace of the currently executed file.

Get a name in the module, which directly points to a name in the module, meaning that you can use it directly without adding any prefix.

Advantages: No prefix, code is more streamlined

Disadvantages: Easy to conflict with names in the namespace in the current execution file

mport and from... import... the similarities and differences

Same points:

Both execute the file corresponding to the module, both generate the namespace of the module, and when calling the function, you need to go to the definition to find the scope relationship, regardless of the calling location.

Difference:

import requires prefix;from... import... No prefix required

About how to introduce import and from... import to share here, I hope the above content can be of some help to everyone, you can learn more knowledge. If you think the article is good, you can share it so that more people can see it.

Tags: Module file space name name prefix no code content function more process middle class help different good same different advantage location Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno NVidia Shulou Technology Shulou Information Apple OPPO Reno