How Python creates modules and module imports

Source: Internet
Author: User
The examples in this article describe how Python creates modules and module imports. Share to everyone for your reference. The specific analysis is as follows:

The Python Learning manual reads:

Define the module, just use a text editor, enter some Python code into the text, and then save it with a. py suffix, any such file will be considered a Python module.

For example, the following code is entered into a file and can be thought of as a module:

def Printme (Var):  print Varif __name__ = = ' __main__ ':    printme (1)

Assuming that the input is in the a.py, then import a can be imported into the module.

Then executable A.printme (3), the screen can print out 3:

>>> A.printme (3) 3>>>

A variable that is defined at the top level of a module, automatically becomes the module's properties. For example:

Data=[1,2,3]def Printme (Var):  print Varif __name__ = = ' __main__ ':    printme (1)

The data variable is a property of the module. In fact, Printme is also a property, just a function.

An example of the Introduction module is as follows: (assuming data=[1,2,3] undefined at this time)

>>> Import a>>> A.datatraceback (most recent): File "
 
  
   
  ", line 1, in 
  
   
    
     A.dataattributeerror: ' Module ' object has no attribute ' data ' >>> reload (a)
   
    
     
    >>> A.datatraceback (most recent): File "
    
     
      
     ", line 1, in 
     
      
       
        a.dataattributeerror: ' Module ' object has no attribute ' data ' >>>
     
      
    
     
   
    
  
   
 
  

You can see from the above hint that the data property is undefined, and then define data=[1,2,3 in the a.py file], reload the A module, and output the Data property:

>>> Reload (a)
 
  
   
  >>> a.data[1, 2, 3]>>> 
 
  

The reload function here can reload a module. If you change it in the module code, you need to reload it.

The a.data above is the access to the properties in the module.

The example above is to import a file as a module.

In fact, Python's module import also has a richer content.

In addition to the module name, Python can import the specified directory path. The directory of Python code is called a package. Therefore, this type of import is called a package import. In fact, a package import is a namespace that turns a directory on your computer into Python. A property is a subdirectory or a module file that is contained in a directory.

Look at the following example:

On my desktop there is a AA folder, there is a BB folder, BB inside has a.py this file.

Then place a __init__.py in the AA and BB folders, and then import AA.BB.A on the command line, and then you are ready for module A.

Hopefully this article will help you with Python programming.

  • 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.