The concept of modules and packages in Python __python

Source: Internet
Author: User
Tags in python
the concept of modules and packages in Python Table of Contents 1 Modules (module) and Packages (Package) 1.1 Namespaces (Namespace) 1.2 Modules (module) 1.3 Packs (Package)1 Modules (module) and Packages (Package) 1.1 Namespaces (Namespace)

Because everything in Python is object, and you want to understand module and package, you must first understand the concept of namespace. The so-called namespace, refers to the visible range of identifiers. For Python, common Namespace are mainly the following: Build-in Namespace (built-in namespace) global Namespace (global namespace) local Namespace (partial namespace)

With the concept of namespaces, you can effectively solve the problem of a function or the name of a variable. The same function name or variable name is allowed in different namespaces. They do not interact with each other, for example, in namespace A and B, there is a variable named Var, and assigning a value to A.var does not change the value of B.var. 1.2 Modules (module)

A module in Python corresponds to a. py file. All of the functions defined, or variables, belong to this module. This module is equivalent to a global namespace for all functions. And each function has its own local namespace.

# test.py ' ' This is the ' is '
Test module ' ' Age
= 0

def printhello ():
  print "Hello, world!"

if __name__ = = ' __main__ ':
   Printhello ()

We can use this module as the alias for Test with T.

>>> Import test as T
>>> t.age
0
>>> t.age = 1
1
>>> T.printhello ()
Hello, world!
>>> Print t.__doc__ This are only
a test module

Of course, we can also use this:

>>> form Test Import *
>>> age
0
>>> Printhello ()

You must have noticed the ' __main__ ' conditional judgment, because each module has a __name__ attribute, only when the module is run directly, the __name__ attribute equals __main__. When it is import, you can completely when it does not exist. 1.3 Packs (Package)

The so-called package is a collection of module, also a bunch of. py file. You can create a package by creating a new folder named Test to create a new empty __init__.py file under the test folder. Create a new next py file under the test folder named a.py

# a.py
def SayHello ():
  print "Hello, world!"

>>> import test.a as T
>>> T.sayhello ()
Hello, world!

Package provides a good way to manage module, which can effectively reduce the naming conflict of module and maintain a good tree structure.

HTML generated by Org-mode 6.31a in Emacs 23

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.