Import essence, import

Source: Internet
Author: User

Import essence, import

I. Module: it is used to logically organize python code (variables, functions, classes, logic, and implement a function). In essence, it is the python file ending with py.

1.1 import method:

Import module

Import module1, module2

From module import logger

From module import logger as frank

1.2 import essence: Explain the python file again

1.3 import module_name

In fact, it is equivalent to copying all the code of module_name. To use it, use the variables and functions module_name.name, module_name.logger.

1.4 from module_name import name

In fact, it is equivalent to copying the name part of module_name to this part. You can directly reference name = 'Alex 'and directly call the variable name.

 

Ii. Package: logically organize a module, which is essentially a directory (a directory with the _ init _. py file)

1.1 import essence: Execute the _ init _. py file under the package directory

1.2 Import Optimization:

Instance, the following code needs to constantly call the module module_test. If there are 200 such calls, you have to find them back and forth, affecting efficiency, in this case, you can convert "import module_test" to "from module_test import test"

def logger():       module_test.test()    print(' in the logger')def search():    module_test.test()    print("in the search")

 

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.