Документ взят из кэша поисковой машины. Адрес оригинального документа : http://www.mrao.cam.ac.uk/~bn204/oof/bojanspython.html
Дата изменения: Mon Apr 4 13:47:52 2016
Дата индексирования: Sun Apr 10 06:23:49 2016
Кодировка: ISO8859-5

Поисковые слова: m 63
Explanation of my Python scripts and how to use them — Bojan Nikolic web pages (r. 329)

Explanation of my Python scripts and how to use themТЖ

Design for use within PythonТЖ

If you try to run most of my scripts by typing something like:

python <name of some script>

you will more likely than not find that absolutely nothing happens. The reason is that essentially all of the are designed to only provide functions and commands that do something useful, but they do not actually start these commands.

This applies even to top-level scripts which analyse whole data set – quite often they provide an analyse() or some such command, but they do not actually invoke it.

The reason for such design is that the scripts are designed for interactive use within Python. What you are supposed to do is to start python interpreter, load up the script using the execfile command, e.g.,

execfile("<name of script.py>")

and then continue in Python to use the commands defined by the script, maybe for example calling a analyse() function.

Getting helpТЖ

The standard Python command for getting help is help (no surprise there!). It will give the list of parameters that a function takes and any documentation that it has. Use this command first to find out about something that you do not know about.

Besides help, the other useful command is dir which lists all of the member variables and functions that an object has. This is a potentially powerful way of discovering everything that you can do with an object.

Difference between import and execfileТЖ

Generally I do not recommend my top-level scripts are imported-ed, but rather they should be execfile-d. These commands are not the same!

import is designed for loading modules and if you import the same file twice it will only by read in once. This is an intentional design feature (to improve performance of large Python application) and means import is only appropriate for loading slowly changing modules. If you really need to update a module loaded with import then you should use the reload() function.