Python学习笔记(二)- 你如何运行程序(How You Run Programs)

1.你如何启动交互式解释器会话(interactive intepreter session)?
答:你可以启动一个交互式会话在Windows7,更早些的时候,通过点击你的开始按钮,选择“所有程序”选项,点击Python一栏,然后选择“Python(命令行)”菜单选项。当然,你也可以通过在你的系统控制窗口(在Windows中是命令提示符窗口)中键入Python(作为一条系统指令行),在Windows和其他平台上取得相同效果。另一种选择是启动IDLE,因为它的主要Python Shell窗口是一个交互式会话。如果你还没有设置你的系统变量(system's PATH)来寻找Python(这取决于你的平台和Python),你可能需要用cd指令到你Python安装的地方,或者键入完整的目录路径取代仅有的Python指令(e.g.在WIndows使用C:\Python33\python,除非你正在使用3.3启动器)。


2.为了启动一个脚本(script)文件,你在哪里键入系统命令行?
答:你可以在你的平台提供的任何系统控制台中键入系统命令行:在Windows是命令行提示符窗口;在Unix,Linux和Mac OS X上是终端仿真程序(xterm)或者终端窗口(termainal window);等等。您可以在系统的提示符下输入,而不是在Python交互式解释器的“>>>”提示符下输入——小心不要搞混这些提示符。

 

3.说出四种或者更多种运行保存在脚本文件里代码的方法。
答:在脚本文件(实际上是模块,module)中的代码可以在

(1)系统命令行中运行

(2)文件图标点击

(3)import 和 reloads

(4)exec内建函数,exec(open('script1.py').read())

(5)IDE的GUI选项如IDLE的RUN→RUN Module菜单选项。

        在Unix中,他们可以用“#!”技巧运行为可执行文件,而且有些平台支持更多专业的启动技巧(e.g.,拖拽和释放)。另外,有些文本编辑器有独一无二的方法来运行Python代码,有些Python程序是作为独立的“冻结二进制”可执行文件(“frozen binary”executables)提供的,而且,一些系统在内嵌模式下使用Python代码,Python代码可以在像C,C++,或者Java语言编写的封闭系统中自动地运行。后一种技术通常用于提供用户自定义层。

 

4.列出两个与在Windows上单击文件图标相关的缺陷。
答:打印就退出程序的脚本造成,在你看见输出之前,输出文件立即消失,这就是为什么input技巧可以派上用场;你脚本产生的错误消息也在输出窗口出现,但是在你检查它之前就消失了,这是系统指令行和IDE(如IDLE)对大多数开发都是更好的原因之一。

 

5.为什么你可能需要重载(reload)一个模块(module)?
答:默认情况下,Python每一个进程只导入同一个模块一次,所以如果你已经改变了它的源代码然后想要在不停止和重启Python下运行最新版本的代码,你将要不得不重载(reload)它。在你能够重载(reload)模块之前,你必须至少导入模块一次。从系统shell命令行、通过图标单击或通过IDE(如IDLE)运行代码文件通常不会造成问题,因为这些启动方案通常每次运行源代码文件的当前版本。

 

6.你如何在IDLE中运行一个脚本?
答:在要运行的文件的文本编辑窗口中,选择窗口的Run→Run Modules菜单选项。它将窗口里的源代码作为顶层运行

运行文件脚本,并将其输出显示在交互式python shell窗口。

 

7.说出使用IDLE有关的缺陷。
答:IDLE仍然可以被某些类型的程序挂起,特别是执行多线程的GUI程序(这是本书范围之外的高级技术)。此外,IDLE还有一些可用性特性,一旦离开IDLE的GUI,这些特性就会烧掉(burn)你:例如,当你运行一个文件时,一个脚本的变量会自动导入到IDLE的交互作用域(scope)中,工作目录也会改变,但是python本身一般不会采取这样的步骤。

 

8.命名空间/名称空间(namespace)是什么,然后它与模块(module)文件有什么关系?
答:命名空间只是一个变量包【即名称(names)】。在Python中,它采用的形式是一个具有属性的对象。每个模块文件自动是一个命名空间,即一个变量包,反映了文件的顶层所做的赋值。命名空间有助于避免Python程序中的命名冲突:因为每个模块文件都是一个独立的命名空间,所以文件必须显式导入其他文件才能使用它们的名称。

 

标注:转载《Learning Python 5th Edition》[奥莱理]

1. How can you start an interactive interpreter session?
2. Where do you type a system command line to launch a script file?
3. Name four or more ways to run the code saved in a script file.
4. Name two pitfalls related to clicking file icons on Windows.
5. Why might you need to reload a module?
6. How do you run a script from within IDLE?
7. Name two pitfalls related to using IDLE.
8. What is a namespace, and how does it relate to module files?

1. You can start an interactive session on Windows 7 and earlier by clicking your Start button, picking the All Programs option, clicking the Python entry, and selecting the “Python (command line)” menu option. You can also achieve the same effect
on Windows and other platforms by typing python as a system command line in your system’s console window (a Command Prompt window on Windows). Another alternative is to launch IDLE, as its main Python shell window is an interactive session. Depending on your platform and Python, if you have not set your system’s PATH variable to find Python, you may need to cd to where Python is installed, or type its full directory path instead of just python (e.g., C:\Python33\python on Windows, unless you’re using the 3.3 launcher).

2. You type system command lines in whatever your platform provides as a system console: a Command Prompt window on Windows; an xterm or terminal window on Unix, Linux, and Mac OS X; and so on. You type this at the system’s prompt, not at the Python interactive interpreter’s “>>>” prompt—be careful not to confuse these prompts.

3. Code in a script (really, module) file can be run with system command lines, file icon clicks, imports and reloads, the exec built-in function, and IDE GUI selections such as IDLE’s Run→Run Module menu option. On Unix, they can also be run as
executables with the #! trick, and some platforms support more specialized launching techniques (e.g., drag and drop). In addition, some text editors have unique ways to run Python code, some Python programs are provided as standalone “frozen binary” executables, and some systems use Python code in embedded mode, where it is run automatically by an enclosing program written in a language like C, C++, or Java. The latter technique is usually done to provide a user customization layer.

4. Scripts that print and then exit cause the output file to disappear immediately, before you can view the output (which is why the input trick comes in handy); error messages generated by your script also appear in an output window that
closes before you can examine its contents (which is one reason that system command lines and IDEs such as IDLE are better for most development).

5. Python imports (loads) a module only once per process, by default, so if you’ve changed its source code and want to run the new version without stopping and restarting Python, you’ll have to reload it. You must import a module at least once before you can reload it. Running files of code from a system shell command line, via an icon click, or via an IDE such as IDLE generally makes this a nonissue, as those launch schemes usually run the current version of the source code file each time.

6. Within the text edit window of the file you wish to run, select the window’s Run→Run Module menu option. This runs the window’s source code as a top-level script file and displays its output back in the interactive Python shell window.

7. IDLE can still be hung by some types of programs—especially GUI programs that perform multithreading (an advanced technique beyond this book’s scope). Also, IDLE has some usability features that can burn you once you leave the IDLE GUI:
a script’s variables are automatically imported to the interactive scope in IDLE and working directories are changed when you run a file, for instance, but Python itself does not take such steps in general.

8. A namespace is just a package of variables (i.e., names). It takes the form of an object with attributes in Python. Each module file is automatically a namespace—that is, a package of variables reflecting the assignments made at the top level of
the file. Namespaces help avoid name collisions in Python programs: because each module file is a self-contained namespace, files must explicitly import other files in order to use their names.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值