Python modules, module usage, installation, aliases, scope, and other concepts

Source: Internet
Author: User

The so-called module is to put the functions of different functions into different files, which not only facilitates the maintenance of functions, but also facilitates the invocation of functions. In Python, a. py file is a module. On the upper level of the module there is a directory called the package. The purpose of this directory is to avoid the duplicate name of the module.

Once the package is introduced, all modules will not conflict with others as long as the package name in the top layer does not conflict with others. Now, the name of the abc.py module becomes mycompany.abc , similarly, xyz.py the module name becomes mycompany.xyz .

Please note that each package directory will have a __init__.py file that must exist, otherwise Python will use this directory as a normal directory, not a package. __init__.pyit can be an empty file, or it can have Python code, because __init__.py it is a module, and its module name is mycompany .

using ModulesThe first thing you need to do with the module is to import the module you want to use. UseImportKeyword Guide package. Write a standard Python module below.
1 #!/usr/bin/evn python2 #_*_ encoding:utf-8_*_3  4 'This is a test module'5  6 __author__='Six Day'7  8 ImportSYS9  Ten defTest (): Oneargs =SYS.ARGV A     ifLen (args) = = 1: -         Print 'Hello World,code changing world' -     elifLen (args) = = 2: the         Print 'Hello,%s!.'% args[1] -     Else: -         Print 'Too many arguments!' -   + if __name__=='__main__': -Test ()

Lines 1th and 2nd are standard comments, and the 1th line of comments allows this hello.py The file runs directly on the Unix/linux/mac, and the 2nd line of comments indicates that the. py file itself uses standard UTF-8 encoding;

Line 4th is a string representing the document comment of the module, and the first string of any module code is treated as a document comment of the module;

The 6th line uses __author__ variables to write the author in, so that when you open the source code, others will be able to admire your name;

The above is the Python module standard file template, of course, you can also delete all do not write, but, according to the standard is certainly correct.

The real Code section starts at the back.

Finally, notice the two lines of code:

if __name__= ='__main__':    test ()

When we run the module file at the command line hello , the Python interpreter places a special variable __name__ __main__ , and if the module is imported elsewhere, the hello if judgment will fail, so this if Testing allows a module to execute some extra code when it runs through the command line, most commonly by running tests.

Alias

When importing modules, you can also use aliases so that you can select the most appropriate modules at run time based on your current environment. For example, the Python standard library is generally provided StringIO with cStringIO two libraries, the interface and functions of the two libraries are the same, but cStringIO is written in C, faster, so you will often see the wording:

 try  :    Cstringio as Stringio  except  importerror: #   Import failures are captured Importerror  import  Stringio 

This allows you to first import cStringIO . If some platforms are not available cStringIO , you can also downgrade them for use StringIO . cStringIO When importing, import ... as ... aliases are specified StringIO so that subsequent code references StringIO work correctly.

There are simplejson libraries like this, which are independent third-party libraries before Python 2.6 and built from 2.6, so there's a way to do this:

Try :     Import # Python >= 2.6 except importerror:     Import # Python <= 2.5

由于Python是动态语言,函数签名一致接口就一样,因此,无论导入哪个模块后续代码都能正常工作。
ScopeNormal functions and variables are public, and can be referred to directly, such as: abcx123PIN. A similar _xxxAnd __xxxSuch a function or variable is non-public (private) and should not be directly referenced, such as _abc__abcN. A similar __xxx__Such variables are special variables that can be referenced directly, but have special uses, such as the above __author____name__is a special variable, hellomodule-defined document annotations can also be used with special variables __doc__Access, our own variables generally do not use this variable name, the external function does not need to be referenced all defined as private, only the external need to refer to the function is defined as public. installing third-party modules 

In Python, the installation of a third-party module is done through the Setuptools tool.

If you are using Mac or Linux, install Setuptools itself this step can be skipped.

If you are using Windows, please download ez_setup.pyfrom this address first:

Https://pypi.python.org/pypi/setuptools#windows

installation method See the connection belowHttp://www.liaoxuefeng.com/wiki/001374738125095c955c1e6d8bb493182103fac9270762a000/ 0013868200214529634268c5b3b45b3a3ba1cd81a251a3b000 

Python modules, module usage, installation, aliases, scope, and other concepts

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.