AI教程网 - 未来以来,拥抱AI;新手入门,从AI教程网开始......

零基础学Python:标准库(2)

零基础学Python AI君 156℃

python标准库内容非常多,有人专门为此写过一本书。在本教程中,由于我的原因,不会将标准库进行完整的详细介绍,但是,我根据自己的理解和喜好,选几个呈现出来,一来显示标准库之强大功能,二来演示如何理解和使用标准库。

sys

这是一个跟python解释器关系密切的标准库,上一节中我们使用过sys.path.append()

>>> import sys
>>> print sys.__doc__

显示了sys的基本文档,看第一句话,概括了本模块的基本特点。

This module provides access to some objects used or maintained by the
interpreter and to functions that interact strongly with the interpreter.

在诸多sys函数和变量中,选择常用的(应该说是我觉得常用的)来说明。

sys.argv

sys.argv是变量,专门用来向python解释器传递参数,所以名曰“命令行参数”。

先解释什么是命令行参数。

$ python --version
Python 2.7.6

这里的--version就是命令行参数。如果你使用python --help可以看到更多:

$ python --help
usage: python [option] ... [-c cmd | -m mod | file | -] [arg] ...
Options and arguments (and corresponding environment variables):
-B     : don't write .py[co] files on import; also PYTHONDONTWRITEBYTECODE=x
-c cmd : program passed in as string (terminates option list)
-d     : debug output from parser; also PYTHONDEBUG=x
-E     : ignore PYTHON* environment variables (such as PYTHONPATH)
-h     : print this help message and exit (also --help)
-i     : inspect interactively after running script; forces a prompt even
         if stdin does not appear to be a terminal; also PYTHONINSPECT=x
-m mod : run library module as a script (terminates option list)
-O     : optimize generated bytecode slightly; also PYTHONOPTIMIZE=x
-OO    : remove doc-strings in addition to the -O optimizations
-R     : use a pseudo-random salt to make hash() values of various types be
         unpredictable between separate invocations of the interpreter, as
         a defense against denial-of-service attacks

只选择了部分内容摆在这里。所看到的如-B, -h之流,都是参数,比如python -h,其功能同上。那么-h也是命令行参数。

sys.arg在python中的作用就是这样。通过它可以向解释器传递命令行参数。比如:

#!/usr/bin/env python
# coding=utf-8

import sys

print "The file name: ", sys.argv[0]
print "The number of argument", len(sys.argv)
print "The argument is: ", str(sys.argv)

将上述代码保存,文件名是22101.py(这名称取的,多么数字化)。然后如此做:

$ python 22101.py
The file name:  22101.py
The number of argument 1
The argument is:  ['22101.py']

将结果和前面的代码做个对照。

  • $ python 22101.py中,“22101.py”是要运行的文件名,同时也是命令行参数,是前面的python这个指令的参数。其地位与python -h中的参数-h是等同的。
  • sys.argv[0]是第一个参数,就是上面提到的22101.py,即文件名。

如果我们这样来试试,看看结果:

$ python 22101.py beginner master www.itdiffer.com
The file name:  22101.py
The number of argument 4
The argument is:  ['22101.py', 'beginner', 'master', 'www.itdiffer.com']

如果在这里,用sys.arg[1]得到的就是beginner,依次类推。

sys.exit()

这是一个方法,意思是退出当前的程序。

Help on built-in function exit in module sys:

exit(...)
    exit([status])

    Exit the interpreter by raising SystemExit(status).
    If the status is omitted or None, it defaults to zero (i.e., success).
    If the status is an integer, it will be used as the system exit status.
    If it is another kind of object, it will be printed and the system
    exit status will be one (i.e., failure).

从文档信息中可知,如果用sys.exit()退出程序,会返回SystemExit异常。这里先告知读者,还有另外一退出方式,是os._exit(),这两个有所区别。后者会在后面介绍。

#!/usr/bin/env python
# coding=utf-8

import sys

for i in range(10):
    if i == 5:
        sys.exit()
    else:
        print i

这段程序的运行结果就是:

$ python 22102.py
0
1
2
3
4

需要提醒读者注意的是,在函数中,用到return,这个的含义是终止当前的函数,并返回相应值(如果有,如果没有就是None)。但是sys.exit()的含义是退出当前程序,并发起SystemExit异常。这就是两者的区别了。

如果使用sys.exit(0)表示正常退出。如果读者要测试,需要在某个地方退出的时候有一个有意义的提示,可以用sys.exit("I wet out at here."),那么字符串信息就被打印出来。

sys.path

sys.path已经不陌生了,前面用过。它可以查找模块所在的目录,以列表的形式显示出来。如果用append()方法,就能够向这个列表增加新的模块目录。如前所演示。不在赘述。不理解的读者可以往前复习。

sys.stdin, sys.stdout, sys.stderr

这三个放到一起,因为他们的变量都是类文件流对象,分别表示标准UNIX概念中的标准输入、标准输出和标准错误。与python功能对照,sys.stdin获得输入(用raw_input()输入的通过它获得,python3.x中是imput()),sys.stdout负责输出了。

还记得print()吧,在这个学习过程中,用的很多。它的本质就是sys.stdout.write(object + '\n')

>>> for i in range(3):
...     print i
... 
0
1
2

>>> import sys
>>> for i in range(3):
...     sys.stdout.write(str(i))
... 
012>>>

造成上面输出结果在表象上如此差异,原因就是那个'\n'的有无。

>>> for i in range(3):
...     sys.stdout.write(str(i) + '\n')
... 
0
1
2

从这看出,两者是完全等效的。如果仅仅止于此,意义不大。关键是通过sys.stdout能够做到将输出内容从“控制台”转到“文件”,称之为重定向。这样也许控制台看不到(很多时候这个不重要),但是文件中已经有了输出内容。比如:

>>> f = open("stdout.md", "w")
>>> sys.stdout = f
>>> print "Learn Python: From Beginner to Master"
>>> f.close()

sys.stdout = f之后,就意味着将输出目的地转到了打开(建立)的文件中,如果使用print(),即将内容输出到这个文件中,在控制台并无显现。

打开文件看看便知:

$ cat stdout.md
Learn Python: From Beginner to Master

这是标准输出。另外两个,输入和错误,也类似。读者可以自行测试。

关于对文件的操作,虽然前面这这里都涉及到一些。但是,远远不足,后面我会专门讲授对某些特殊但常用的文件读写操作。

copy

《字典(2)》中曾经对copy做了讲授,这里再次提出,即是复习,又是凑数,以显得我考虑到了这个常用模块,还有:

>>> import copy
>>> copy.__all__
['Error', 'copy', 'deepcopy']

这个模块中常用的就是copy和deepcopy。

为了具体说明,看这样一个例子:

#!/usr/bin/env python
# coding=utf-8

import copy

class MyCopy(object):
    def __init__(self, value):
        self.value = value

    def __repr__(self):
        return str(self.value)

foo = MyCopy(7)

a = ["foo", foo]
b = a[:]
c = list(a)
d = copy.copy(a)
e = copy.deepcopy(a)

a.append("abc")
foo.value = 17

print "original: %r\n slice: %r\n list(): %r\n copy(): %r\n deepcopy(): %r\n" % (a,b,c,d,e)

保存并运行:

$ python 22103.py 
original: ['foo', 17, 'abc']
 slice: ['foo', 17]
 list(): ['foo', 17]
 copy(): ['foo', 17]
 deepcopy(): ['foo', 7]

读者可以对照结果和程序,就能理解各种拷贝的实现方法和含义了。


转载请注明:www.ainoob.cn » 零基础学Python:标准库(2)

喜欢 (0)or分享 (0)