Python Learning log Python module

Source: Internet
Author: User

Python Introductory Learning Notes Python module

Python module:

I. Understanding of the Python module

1. What is a module

A function is a program that can implement one or more functions. Our modules are extensions of function functions, which are blocks that can implement one or more functions. From the definition can be seen: The function is a program, the module is a program block, that is, functions, modules are used to implement the function, but the module scope is wide, in the module can be reused multiple functions


2. How to import modules

In a Python program, if you want to use a module, you cannot use the functionality of a module directly. Before the module is used, we need to import the specified module before we can use it after importing the module.

>>> Import math>>> math.pi3.141592653589793>>>


3.sys system comes with module

There are some modules in Python that are not defined by us, and are provided by Python's own module. These modules are created with this Python installation, which we call the standard library module. Standard library module is a class of modules, not a module, in the standard library module, there are also a lot of modules, some modules and e-mail related, and some operating system-related, as well as the system functions, we refer to the standard library and system-related modules called SYS module

#实例:

>>> import sys>>> sys.version ' 2.7.10 (default, May, 09:44:00) [MSC v.1500-bit (AMD64)] ' >&G t;> sys.executable ' d:\\python27\\python279\\pythonw.exe ' #当前运行文件的位置 >>> sys.getwindowsversion () Sys.getwindowsversion (major=6, Minor=1, build=7601, platform=2, service_pack= ' service pack

1 ') #返回当前windows系统信息

>>> Sys.modules.keys () #查看已经导入的模块关键字


Second, byte compilation

1. What is the. pyc file for Python

What we call the. pyc file is a file of this type with a suffix of. PYc. We know that because our computers only know the binary machine language, there are two ways we execute the Python module: one is to compile the contents of the module into a binary language, then execute the binary language, and the other is to execute the binary language program inside the corresponding module. The second way is to omit the compile step, so the execution speed is relatively faster. The process of compiling a module into a binary language program is called Byte compilation, which produces a. pyc file corresponding to the compiled module, so the. pyc file is a binary file of the compiled module


2. Differences in byte compilation and compilation

BYTE compilation: The process of compiling modules into binary language programs

Python is an interpreted language, not a compiled language, so some people will say how to compile the byte, in fact, although Python has compiled this process, but it is different from other compiled languages, its compilation is done by the interpreter, After all, each program wants to execute the need to become a binary form, the compiled language refers to the software has a separate compilation module to compile the program, and Python does not, it has an interpreter


Generation of 3..PYC files

The byte compilation process produces a. pyc file that corresponds to the compiled module, that is, the. pyc file is the binary file that the compiled module corresponds to.

There are two types of. PYc produced in the way:

The first type: Import

>>> Import zipfile>>>

Second type: cmd command line

Enter into the Lib file line by entering: Cd:d:\python27\lib, then enter: Python-m compileall xmllib.py


Use of 4..PYC files

In Python, the biggest role of the. pyc file is to speed up the speed of the module (you can run the. pyc file directly, and you do not need to compile). Actually. PYc and advanced features such as anti-compilation


Decompile: Restore a binary file to a source file


Three, From...import detailed

1.Python in the vagina A module method is to use import, but import just imported the module, and did not import a property and method of our module, and we have to import not only a module, but also to import the corresponding function in the module, we can use the From ... Import statement

#实例:

>>> import sys>>> sys.version ' 2.7.10 (default, May, 09:44:00) [MSC v.1500-bit (AMD64)] ' >&G  T;> Versiontraceback (most recent): File "<pyshell#4>", line 1, <module> Versionnameerror: Name ' version ' is not defined>>> from sys import version>>> version

' 2.7.10 (default, May, 09:44:00) [MSC v.1500-bit (AMD64)] '

>>> #直接调用version, no error occurred


2. Using from...import*

In Python it is only a function of importing a module with the From...import statement, and if we want to put all the functions of this module, that is, all the properties and methods, we can use the from...import* statement.

#实例:

>>> Executabletraceback (most recent): File "<pyshell#9>", line 1, in <module> executab Lenameerror:name ' executable ' is not defined>>> #executable是sys的另外一个功能 for viewing current directory >>> from sys import *& gt;>> executable ' D:\\python27\\python279\\pythonw.exe '


Iv. Understanding the __name__ attribute

1. Understanding the Main module

In a Python function, if a function calls another function to complete a function, we call this function the main function, and if a function does not call other functions, we call this function a non-main function. Modules are similar, if a module is used directly, and is not called by others, we call it the main module, if a module is called by others, we call it a non-master module


2. Recognize _name_ properties

#首先我们分别看一下这个模块在不同场景中的__name__的值

Print __name__

#输出结果:

_main_# Main Module


Distinguish whether the main module, see oneself Call or be called by others


Use of the 3.__name__ property

In Python there are main modules and non-master modules, then how to distinguish between the main module and the non-master module, if a module of the _name_ attribute value is _main_, then the module is the main module, and vice versa, we can actually think of _name_ as a variable, this variable is given by the Sye system To determine if a module is a master module

#实例

#其次我们看一下__name__属性的常用情况, used to determine whether to execute or be called

# ' * ' coding=utf-8 ' * ' #用来防止编码错误if __name__== "__main__": #两个下划线 print "It's main" else:print "it ' s not main" #输出结果: >> ;> import lname_it ' s not main>>>


Five, custom module

1. What is the custom module?

Python has many modules, some of which do not need to be defined by the user to write, Python in the installation of their own modules, these modules are the system comes with modules. And some modules are just different from this module, it is necessary for us to define to write the module, called the Custom module


2. Customize the module using the detailed

(1). Just save the Python program (. py) you wrote to the Lib directory.


(2). Writing code

Ctrl+s save it and save it to Lib.

#实例:

To create a program, define a function:

def add (i,j): K=i+j return Kk=add (i,j) Print K

Difficulty: Parameter passing, passing parameters to I,j

To add an initialization value before the function:

I=0j=0def Add (i,j): K=i+j return Kk=add (i,j) Print K

This way, the syntax and logic do not appear to be wrong

Crtl+s

Then start passing parameters:

>>> import add0>>> i=6>>> j=7>>> add.add (i,j) 13>>>

or before the function is declared before saving:

int iin jdef Add (i,j): K=i+j return Kk=add (i,j) Print K

#输出结果:

>>> import add>>> i=6>>> j=7>>> add.add (i,j) 13>>>


Vi. Functions of the Dir

1. Recognize the Dir () function

There are a lot of modules in Python, but sometimes we forget what the function of a module is, and we can use the Dir () function to see a list of the functions of the specified module.


#查看sys模块:

>

>> import sys   #先导入 >>> dir (SYS)      #再查看 [' __ displayhook__ ',  ' __doc__ ',  ' __excepthook__ ',  ', __name__ ',  ' __package__ ',  ' __stderr__ ',   ' __stdin__ ',  ' __stdout__ ',  ' _clear_type_cache ',  ' _current_frames ',  ' _getframe ',   ' _mercurial ',  ' api_version ',  ' argv ',  ' builtin_module_names ',  ' byteorder ',  ' call _tracing ',  ' callstats ',  ' copyright ',  ' displayhook ',  ' dllhandle ',  ' Dont_write_bytecode ',  ' exc_clear ',  ' exc_info ',  ' exc_traceback ' ',  ' exc_type ',  ' exc_value ',  ' Excepthook ',  ' exec_prefix ',  ' executable ',  ' exit ',  ' flags ',  ' float_info ',  ' Float_ Repr_style ',  ' getcheckinterval ',  ' getdefaultencoding ',  ' getfilesystemencoding ',  ' GetProfile ',  ' getrecursionlimit ',  ' getrefcount ',  "getsizeof ',  ' gettrace ',  ' Getwindowsversion ',  ' hexversion ',  ' long_Info ',  ' maxint ',  ' maxsize ',  ' maxunicode ',  ' meta_path ' ',  ' modules ',  ' path ',  ' Path_hooks ',  ' path_importer_cache ',  ' platform ',  "prefix ',  ' py3kwarning ',  ' Setcheckinterval ',  ' setprofile ',  ' setrecursionlimit ',  ', settrace ',  ' stderr ',  ' stdin ',   ' stdout ',  ' Subversion ',  ' version ',  ' version_info ',  ' warnoptions ',  ' winver ']> >> sys.__doc__# can be viewed


2.dir function extensions in a detailed

#实例:

>>> d=[]>>> dir (d) [' __add__ ',  ' __class__ ',  ' __contains__ ',  ' __ delattr__ ',  ' __delitem__ ',  ' __delslice__ ',  "__doc__ ',  ' __eq__ '",  ' __format__ ',  ' _ _ge__ ',  ' __getattribute__ ',  ' __getitem__ ',  ', __getslice__ ',  ' __gt__ ',  ' __hash__ ',   ' __iadd__ ',  ' __imul__ ',  ' __init__ ',  ' __iter__ ',  ' __le__ ',  ' __len__ ',  ' __lt __ ',  ' __mul__ ',  ' __ne__ ',  ' __new__ ',  ' __reduce__ ',  ', ' __reduce_ex__ ',,  ' __repr__ ',   ' __reversed__ ',  ' __rmul__ ',  ' __setattr__ ',  ' __setitem__ ',  ' __setslice__ ',  ' __ sizeof__ ',  ' __str__ ',  ' __subclasshook__ ',  ' ' Append ',  ' count ',  ' extend ',  ' index ',   ' Insert ',  ' Pop ',  ' remove ',  ' reverse ',  ' sort ']>>> c=[' a ', ' B ']>> > dir (c) [' __add__ ',  ' __class__ ',  ' __contains__ ',  ' __delattr__ ',  ' __delitem__ ',   ' __delslice__ ',  ' __doc__ ',  ' __eq__ ',  ' __format__ ',  "__ge__ ',  ' __getattribute__ ',  ' __getitem__ ',  ' __ getslice__ ',  ' __gt__ ',  ' __hash__ ',  ' __iadd__ ',,  ' __imul__ ',  ' __init__ ',  ' __iter__ ',  ' __le__ ',  ' __len__ ',  ' __lt__ ',  ', ' __mul__ ',  ' __ne__ ',  ' __new__ ' ',  ' __reduce __ ',  ' __reduce_ex__ ',  ' __repr__ ',  ' __reversed__ ',  ' __rmul__ ',  ', __setattr__ ',  ' __ setitem__ ',  ' __setslice__ ',  ' __sizeof__ ',  ', __str__ ',  ' __subclasshook__ ',  ' append ',   ' count ',  ' extend ',  ' index ',  ' Insert ',  ' Pop ',  ' remove ',  ' reverse ',  ' Sort ']>>>  #返回值一样, dir returns only attributes, does not differentiate data >>> e= () >>> dir (e) [' __add__ ',  ' __class__ ',  ' __contains__ ',  ' __delattr__ ',  ' __doc__ ', ',  ' __eq__ ',  ' __format__ ',  ' __ge__ ',  ' __getattribute__ ',  ' __getitem__ ',  "__getnewargs__ ',  ' __getslice__ ',  ' __gt__ ',  ' __hash__ ',  ' __init__ ',  ' __iter__ ',  ' __le__ ',  ' __len__ ',  ' __lt__ ' ",  ' __mul__ ',  ' __ ne__ ',  ' __new__ ',  ' __reduce__ ',  "__reduce_ex__ ',  ' __repr__ ',  ' __rmul__ ',  ' __ setattr__ ',  ' __sizeof__ ',  ' __str__ ',  ' "__subclasshook__ ',  ' count ',  ' index ']>> >
















This article is from the "8626774" blog, please be sure to keep this source http://8636774.blog.51cto.com/8626774/1677733

Python Learning log Python module

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.