每天学点python(4)

===========================================================================

===========================================================================

1. import【转】(http://www.cnblogs.com/dajianshi/archive/2012/12/28/2837270.html)

众所周知,python既可以导入包(import package),也可以导入模块(import module),package一般理解为多文件的模块,它是这样定义的,如果一个目录下存在”__init__.py”这个文件,那么python就认 为这个目录下的所有文件同属于一个package(这和java的namespace有点像,但是java可不需要这么个特殊文件),如果没有这个文件, 那么python认为目录下的py文件都是不相干的独立模块。但是在子目录中你可不能这么干,如果子目录中没有”__init__.py”,那么该目录下 的程序算是白写了,根本没有办法引用。当然,也不是绝对的,除非绕个大弯儿:设置当前目录,然后导入,然后重设当前目录。比如,程序需要使用test目录 中的模块,而test目录不是package,你只能这样做:

   os.chdir("test")

   import testfuncs

   os.chdir("..")

最方便的引入,当然是同一个目录的模块(除了及其简单的程序,很少有人会使用这种扁平的目录结构),那是想怎么导入就怎么导入。比如在主程序中想要使用另一个文件模块中的函数,只需要直接导入即可:

import testfuncs  #直接导入模块(也就是不带扩展名的文件名)
testfuncs.nousefunc() #通过模块的名字引用其中的函数
from testfuncs import nousefunc #导入模块中的函数
nousefunc() #直接调用导入的函数

上面只是最简单的情况,如果文件中定义的是class怎么样呢,实际使用也是差不多的,不过要多一次构造class的实例的调用:

import testclass  #直接导入模块(也就是不带扩展名的文件名)
obj=testclass.TestClass(); #实例化类
obj.func1() #通过模块的名字引用其中的class,然后才到类的函数,注意类要实例化
from testclass import TestClass #导入模块中的类
obj=TestClass(); #实例化类
obj.func1() #调用函数

如果都是这种从程序中引入同目录或者子目录的包,那么事情就简单了,但是如果是同为子目录中的模块,要引入兄弟目录中的模块或类,要怎么办呢?例如,如下的目录结构:

图中src目录就是程序的顶层目录,也是包导入的顶层package,pub目录及其子目录是公用程序所在。在这种情况下,最好的方法就是在主程序 中(一般位于应用的源程序的根目录,如图中的start.py),把所有下级的目录都缴入的sys.path中,然后在子目录中的模块中,只要使用完全限 定的包名引入其他子目录中的模块或者类就可以了。然而现实的情况往往不那么尽如人意,比如为公用包写的测试程序需要放在pub/test目录下,测试目标 在pub/data目录下,这是不能寄希望与应用的主程序了,因为此时不会去运行应用程序。这种情况下,启动程序和被引用的包同在一个父目录的子目录中。 此时该怎么办呢,还是老办法,要把父目录(src/pub)和(src/pub/data、src/pub/test)目录都要加入sys.path中, 然后再用

绝对的方式进行引入(import pub.data.datautil / from pub.data.datautil import DataUtil)。每一个需要的模块都要这么干,因此,我特地写了一个函数,来自动的处理这种情况:

import os,sys
import TestClass
import testfuncs;
from TestClass import TestClass;

def _prepareRelativeImport(layer=2):
    """ 为相对引用做准备,以便下层目录中的模块,相对引用[本目录]以及[父目录]和[兄弟目录]中的模块。
    参数layer,表示引入到多少层父目录。默认为2->引入本目录和父目录;3->引入本目录、父目录和祖父目录。
    """
    import sys,os;
    curP=os.path.abspath(os.path.dirname(__file__)); oriP=curP;__package__=curP.rpartition(os.path.sep)[2];
    print('\r\ncurdir=',curP);
    while layer>=0:
        layer-=1;
        if not curP in sys.path:sys.path.append(curP);
        pa=curP.rpartition(os.path.sep);curN=pa[2];pp=pa[0];os.chdir(pp);
        #if '__init__' in ''.join(os.listdir(curP)):__import__(curN);
        curP=pp;
    os.chdir(oriP);

if __name__=='__main__':
    if not '__file__' in dir():__file__=os.path.abspath('.')+os.path.sep+"1.py";
    _prepareRelativeImport(2)
    from TestClass import TestClass;
    from pub.test.TestClass import TestClass;
    from pub.data.CompareOperator import CompareOperators
    print('\r\nTest of RelativeImport done!')

 2. 输入

age = raw_input("How old are you? ")
height = raw_input("How tall are you? ")
weight = raw_input("How much do you weigh? ") print "So, you're %r old, %r tall and %r heavy." % (age, height, weight)
from sys import argv

script, first, second, third = argv

print "The script is called:", script
print "Your first variable is:", first
print "Your second variable is:", second
print "Your third variable is:", third

 执行脚本,输出

$ python ex13.py first 2nd 3rd
The script is called: ex13.py
Your first variable is: first
Your second variable is: 2nd
Your third variable is: 3rd

查看doc

windows:

python -m pydoc raw_input

linux:

pydoc raw_input

 同样也是输入

>>>input("the meaning of life:")
the meaning of life:25
25

3.

print "helloworld!"
raw_input("Press <enter>")

以上代码加入最后一行之后双击这个.py文件可以显示输出结果。。。原来没想到。。。

原文地址:https://www.cnblogs.com/dollarzhaole/p/2837676.html