With the old Ziko Python import module _python

Source: Internet
Author: User
Tags instance method stdin in python

Understanding Module

For modules, in some of the previous examples, it has been mentioned, such as once: import random (get random number module). In order to be able to have a clear understanding of the module, first look at what the module, here Select the Official document definition of it:

Copy Code code as follows:

A module is a file containing Python definitions and statements. The file name is the module name with the suffix. Py appended. Within a module, the module ' s name (as a string) is available as the value of the global variable name.

They're all foreign, don't they? No! Or just the point:
• The module is a file containing a Python statement
• The module name is the filename (do not extend. py)

So, where is that import random file?

Use the Magic weapon you've talked about: the Help () function to see:

Copy Code code as follows:

>>> Help (Random)

And then it appears:

Copy Code code as follows:

NAME
Random-random variable generators.

FILE
/usr/local/lib/python2.7/random.py

MODULE DOCS
Http://docs.python.org/library/random

DESCRIPTION
...

It is very obvious here that the file of the random module is:/usr/local/lib/python2.7/ random.py (Note: This address is the address of my computer, it may not be the same as reader, especially if reader is using Windows, it must be different from mine. )

Reader this time may have the question, how does import find that document? How do you write similar documents? Don't worry, these I will come one by one.

Standard library

Looking at the previous example of random, reader may immediately think of a problem: has someone already written a lot of commonly used functions as modules? Then the user simply needs to invoke it in a similar way. It is true, as shown above, that it is not written by a programmer while it is in use, but is installed on the computer when it is installed in Python. Watch that file store the address, you know.

According to the address above, I list the files in/usr/local/lib/python2.7/, which are similar to random modules, because it is a Python installation, it is standard, give them a name "standard module Library", referred to as "standard library".

This figure lists a small portion of the module files that exist in this directory.

Python's standard library (standard library) is an integral part of Python and a powerful tool for Python, making programming easier.

If reader has time, visit frequently: https://docs.python.org/2/library/, here is a list of how all standard libraries are used.

One point, please reader special attention, for the standard library, because the content is too much, I am afraid that can not remember. Also do not have to be able to remember, just need to know that there is such a thing. If you are writing a program, it must be thought that for something, there will be a standard library support it? Then go to Google or the address given above to search.

Example:

Copy Code code as follows:

>>> Import SYS #导入了标准库sys
>>> dir (SYS) #如果不到网页上看, in this way you can view the various methods (functions) provided by this standard library
[' __displayhook__ ', ' __doc__ ', ' __egginsert ', ' __excepthook__ ', ' __name__ ', ' __package__ ', ' __plen ', ' __stderr__ ', ' __ Stdin__ ', ' __stdout__ ', ' _clear_type_cache ', ' _current_frames ', ' _getframe ', ' _mercurial ', ' api_version ', ' argv ', ' Builtin_module_names ', ' byteorder ', ' call_tracing ', ' callstats ', ' copyright ', ' displayhook ', ' dont_write_bytecode ', ' Exc_clear ', ' exc_info ', ' exc_type ', ' excepthook ', ' exec_prefix ', ' executable ', ' exit ', ' flags ', ' float_info ', ' Float_ Repr_style ', ' getcheckinterval ', ' getdefaultencoding ', ' getdlopenflags ', ' getfilesystemencoding ', ' getprofile ', ' Getrecursionlimit ', ' getrefcount ', ' getsizeof ', ' gettrace ', ' hexversion ', ' last_traceback ', ' last_type ', ' Last_value ' , ' Long_info ', ' maxint ', ' maxsize ', ' maxunicode ', ' meta_path ', ' modules ', ' path ', ' path_hooks ', ' path_importer_cache ', ' Platform ', ' prefix ', ' ps1 ', ' ps2 ', ' py3kwarning ', ' setcheckinterval ', ' setdlopenflags ', ' setprofile ', ' Setrecursionlimit ', ' settrace ', ' stderr ', ' stdin ', ' stdout ', ' subversion ', ' verSion ', ' version_info ', ' warnoptions ']
>>> Sys.platform #比如这个
' Linux2 '
>>> sys.version #还有这个
' 2.7.6 (default, Nov 2013, 19:24:16) \N[GCC 4.6.3] '

>>> Help (Sys.stdin) #这是查看某个模块方法具体内容的方式

Standard library, often used in programming. Here is not to repeat. As long as reader can know where to find, how to find the required standard library.

Write your own module

Reader may prefer to "do-it-yourself, plenty of clothing" (although it is not necessarily a good enough), in some of the necessary time, you really have to write some of the modules themselves. So how do you write a module?

The module is a. py file, so as long as you write some statements to a. py file, it's a module. There's nothing too much to be secretive about.

A file is created under a directory named: mmmm.py, as shown in the following figure, and then edited for the contents of this file. Save when you are done editing.

The code is the contents of the file:

Copy Code code as follows:

#!/usr/bin/env python
#coding: Utf-8

web = "Https://qiwsir.github.io"

def my_name (name):
Print Name

Class Pythoner:
def __init__ (Self,lang):
Self.lang = Lang
def programmer (Self):
Print "Python programmer language is:", Self.lang

The diagram is the directory where the file is located, and the Python interactive mode is turned on in this directory (I am under Ubuntu, Reader is the other operating system, note the path, if you encounter problems, you can temporarily shelve, see below).

As you can see from the diagram, this file is present in the current directory: mmmm.py

In interactive mode, follow the mode of operation on the standard library module:

Copy Code code as follows:

>>> Import MMMM
>>> dir (MMMM)
[' __builtins__ ', ' __doc__ ', ' __file__ ', ' __name__ ', ' __package__ ', ' my_name ', ' Pythoner ', ' web ']
>>> mmmm.__doc__ #这个是空的, exactly, because I've never written any document description
>>> mmmm.__name__ #名字
' MMMM '
>>> mmmm.__file__ #文件
' mmmm.py '

Then look at the back: My_name,pythoner,web, I wrote in the content.

Copy Code code as follows:

>>> Mmmm.web
' Https://qiwsir.github.io '

The Web is the module mmmm a variable created by the assignment statement, where it is programmed to mmmm properties, can be accessed through the dot operation, in fact, not only this type of assignment, other through Def,class, etc., can be used as the MMMM module properties.

Copy Code code as follows:

>>> Mmmm.my_name
<function My_name at 0xb74ceb54>
>>> Mmmm.pythoner
<class Mmmm.pythoner at 0xb73e6bcc>

Of course, as with standard libraries, you can use Help () to look at the specifics of these attributes:

Copy Code code as follows:

>>> Help (Mmmm.my_name)

Help on function my_name in module MMMM:

My_name (name)

>>> Help (Mmmm.pythoner)

Help on Class Pythoner in module mmmm:

Class Pythoner
| Methods defined here:
|
| __init__ (self, Lang)
|
| Programmer (Self)

How to call it? This allows:

Copy Code code as follows:

>>> mmmm.my_name ("Qiwsir")
Qiwsir

When calling functions in a module, use the name of the module (import mmmm) + dot + function (Note that the function is followed by parentheses, if there are parameters, the parentheses inside with the parameters), that is, Module_name.funciton (*args)

Copy Code code as follows:

>>> py = Mmmm.pythoner ("C + +")
>>> Py.programmer ()
Python Programmer Language Is:c++

The above two lines show the method of invoking the class in the module and the instance method of the class using the bound methods. Compared with the past, it seems that there is a mmmm in front.

If you feel this mmmm is more troublesome, you can use from, specifically this:

Copy Code code as follows:

>>> from MMMM import *
>>> my_name (' Qiwsir ')
Qiwsir
>>> Web
' Https://qiwsir.github.io '
>>> py = Pythoner ("C + +")
>>> Py.programmer ()
Python Programmer Language Is:c++

Don't always write so mmmm this time. Two ways, which is better? There is no verdict. Reader in the later practice of experience, when to use what way.

The above uses the from MMMM import, where the symbol means to import all of them in, and in this way, you can import only part of it, as follows:

Copy Code code as follows:

>>> from mmmm import my_name #如果看官前面运行了上述操作, you need to turn off interactive mode,
#再重启 to see the following procedure
>>> my_name ("Qiwsir")
Qiwsir
>>> Web #没有import这个, so there is an error.
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
Nameerror:name ' web ' is not defined

This is the basic Import module method. Reader the doubt, but also to save. And listen to let's.

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.