2021-10-27 09:10

Python is an easy to learn, powerful programming language. It has efficient high-level data structures and a simple but effective approach to object-oriented programming. Pythonrsquo;s elegant syntax and dynamic typing, together with its interpreted nature, make it an ideal language for scripting and rapid application development in many areas on most platforms.

The Python interpreter and the extensive standard library are freely available in source or binary form for all major platforms from the Python Web site,, and may be freely distributed. The same site also contains distributions of and pointers to many free third party Python modules, programs and tools, and additional documentation.

The Python interpreter is easily extended with new functions and data types implemented in C or C (or other languages callable from C). Python is also suitable as an extension language for customizable applications.

This tutorial introduces the reader informally to the basic concepts and features of the Python language and system. It helps to have a Python interpreter handy for hands-on experience, but all examples are self-contained, so the tutorial can be read off-line as well.

For a description of standard objects and modules, see The Python Standard Library. The Python Language Reference gives a more formal definition of the language. To write extensions in C or C , read Extending and Embedding the Python Interpreter and Python/C API Reference Manual. There are also several books covering Python in depth.

This tutorial does not attempt to be comprehensive and cover every single feature, or even every commonly used feature. Instead, it introduces many of Pythonrsquo;s most noteworthy features, and will give you a good idea of the languagersquo;s flavor and style. After reading it, you will be able to read and write Python modules and programs, and you will be ready to learn more about the various Python library modules described in The Python Standard Library.

If you do much work on computers, eventually you find that therersquo;s some task yoursquo;d like to automate. For example, you may wish to perform a search-and-replace over a large number of text files, or rename and rearrange a bunch of photo files in a complicated way. Perhaps yoursquo;d like to write a small custom database, or a specialized GUI application, or a simple game.

If yoursquo;re a professional software developer, you may have to work with several C/C /Java libraries but find the usual write/compile/test/re-compile cycle is too slow. Perhaps yoursquo;re writing a test suite for such a library and find writing the testing code a tedious task. Or maybe yoursquo;ve written a program that could use an extension language, and you donrsquo;t want to design and implement a whole new language for your application.

Python is just the language for you.

You could write a Unix shell script or Windows batch files for some of these tasks, but shell scripts are best at moving around files and changing text data, not well-suited for GUI applications or games. You could write a C/C /Java program, but it can take a lot of development time to get even a first-draft program. Python is simpler to use, available on Windows, Mac OS X, and Unix operating systems, and will help you get the job done more quickly.

Python is simple to use, but it is a real programming language, offering much more structure and support for large programs than shell scripts or batch files can offer. On the other hand, Python also offers much more error checking than C, and, being a very-high-level language, it has high-level data types built in, such as flexible arrays and dictionaries. Because of its more general data types Python is applicable to a much larger problem domain than Awk or even Perl, yet many things are at least as easy in Python as in those languages.

Python allows you to split your program into modules that can be reused in other Python programs. It comes with a large collection of standard modules that you can use as the basis of your programs — or as examples to start learning to program in Python. Some of these modules provide things like file I/O, system calls, sockets, and even interfaces to graphical user interface toolkits like Tk.

Python is an interpreted language, which can save you considerable time during program development because no compilation and linking is necessary. The interpreter can be used interactively, which makes it easy to experiment with features of the language, to write throw-away programs, or to test functions during bottom-up program development. It is also a handy desk calculator.

Python enables programs to be written compactly and readably. Programs written in Python are typically much shorter than equivalent C, C , or Java programs, for several reasons:

  • the high-level data types allow you to express complex operations in a single statement;
  • statement grouping is done by indentation instead of beginning and ending brackets;
  • no variable or argument declarations are necessary.

Python is extensible: if you know how to program in C it is easy to add a new built-in function or module to the interpreter, either to perform critical operations at maximum speed, or to link Python programs to libraries that may only be available in binary form (such as a vendor-specific graphics library). Once you are really hooked, you can link the Python interpreter into an application written in C and use it as an extension or command language for that application.

By the way, the language is named after the BBC show “Monty Pythonrsquo;s Flying Circus” and has nothing to do with reptiles. Making references to Monty Python skits in documentation is not only allowed, it is encouraged!

Now that you are all excited about Python, yoursquo;ll want to examine it in some more detail. Since the best way to learn a language is to use it, t



Python解释器易于扩展,可以使用C或C (或者其他可以通过C调用的语言)扩展新的功能和数据类型。Python也可用于可定制化软件中的扩展程序语言。


有关标准的对象和模块,参阅Python标准库。Python语言参考提供了更正式的语言参考。想要编写C或者C 扩展可以参考扩展和嵌入Python解释器和Python/CAPI参考手册。也有不少书籍深入讲解Python。



如果你是专业的软件开发人员,你可能需要编写一些C/C /Java库,但总觉得通常的开发的流程(编写、编译、测试、再次编译等)太慢了。可能给这样的库写一组测试,就是很麻烦的工作了。或许你写了个软件,可以支持插件扩展语言,但你不想为了自己这一个应用,专门设计和实现一种新语言了。


对于这些任务,你也可以写Unix脚本或者Windows批处理完成,但是shell脚本最擅长移动文件和替换文本,并不适合GUI界面或者游戏开发。你可以写一个C/C /Java程序,但是可能第一版本的草稿都要很长的开发时间。Python的使用则更加简单,可以在Windows,MacOSX,以及Unix操作系统上使用,而且可以帮你更快地完成工作。




Python程序的书写是紧凑而易读的。Python代码通常比同样功能的C,C ,Java代码要短很多,原因列举如下:

  • 高级数据类型允许在一个表达式中表示复杂的操作;
  • 代码块的划分是按照缩进而不是成对的花括号;
  • 不需要预先定义变量或参数。





















  • 加速器模块:这些模块是完全独立的,并且只是为了比CPython中运行的等效纯Python代码运行得更快。理想情况下,如果加速版本在给定系统上不可用,加速器模块将始终具有纯Python等效用作后备。CPython标准库广泛使用加速器模块。
  • 包装器模块:创建这些模块是为了将现有的C接口暴露给Python代码。它们可以直接暴露底层C接口,或者暴露更多“Pythonic”API,利用Python语言功能使API更易于使用。CPython标准库广泛使用包装器模块。
  • 低级系统访问:创建这些模块是为了访问CPython运行时,操作系统或底层硬件的低级功能。通过特定于平台的代码,扩展模块可以实现纯Python代码中不可能实现的功能。许多CPython标准库模块都是用C语言编写的,以便访问未在语言级别公开的解释器内部。



使用二进制扩展的主要缺点是它使得后续软件分发更加困难。使用Python的一个优点是它主要是跨平台的,用于编写扩展模块的语言(通常是C或C ,但实际上任何可以绑定到CPythonCAPI的语言)通常都要求为其创建自定义二进制文件不同的平台。


  • 要求最终用户能够从源代码构建它们,或者有人为常见平台发布预构建的二进制文件
  • 可能与CPython参考解释器的不同构建不兼容
  • 通常无法与PyPy,IronPython或Jython等替代解释器一起正常工作
  • 如果手动编码,通过要求维护者不仅熟悉Python,而且熟悉用于创建二进制扩展的语言,以及CPythonCAPI的详细信息,使维护更加困难。
  • 如果提供纯Python回退实现,则要求在两个位置实现更改,并在测试套件中引入额外的复杂性以确保始终执行两个版本,从而使维护更加困难。



  • 寻找现有的优化替代品。CPython标准库包含许多优化的数据结构和算法(特别是在内置collections和itertools模块中)。PythonPackageIndex还提供了其他选择。有时,适当选择标准库或第三方模块可以避免创建自己的加速器模块。
  • 对于长时间运行的应用程序,JIT编译的PyPy解释器可以提供标准CPython运行时的合适替代方案。采用PyPy的主要障碍通常是依赖于其他二进制扩展模块-而PyPy模拟CPythonCAPI,依赖于此的模块会导致PyPyJIT出现问题,而仿真层经常会暴露CPython扩展模块中的潜在缺陷目前容忍(经常围绕引用计数错误-具有一个实时引用的对象而不是两个通常不会破坏任何东西,但是没有引用而不是一个是主要问题)。
  • Cython是一个成熟的静态编译器,可以将大多数Python代码编译为C扩展模块。初始编译提供了一些速度提升(通过绕过CPython解释器层),Cython的可选静态类型功能可以为速度提升提供额外的机会。使用Cython仍然具有增加分发生成的应用程序的复杂性的缺点,但是具有减少Python程序员进入门槛的优点(相对于其他语言,如C或C )。
  • Numba是一个更新的工具,由科学Python社区的成员创建,旨在利用LLVM允许在运行时选择性地将Python应用程序的部分编译为本机机器代码。它要求LLVM在运行代码的系统上可用,但可以显着提高速度,特别是对于适合矢量化的操作。



  • 除了用于创建加速器模块之外,Cython还可用于创建包装器模块。它仍然涉及手工包装接口,因此可能不是包装大型API的好选择。
  • cffi是一些由PyPy开发人员创建的项目,它使已经熟悉Python和C的开发人员可以直接将他们的C模块暴露给Python应用程序。它还使基于其头文件包装C模块变得相对简单,即使您自己不了解C也是如此。


  • SWIG是一个包装器接口生成器,它允许各种编程语言(包括Python)与C和C 代码进行交互。
  • ctypes当标头信息不可用时,标准库的模块虽然可用于访问C级接口,但是它只能在CABI级别运行,因此在实际导出的接口之间没有自动一致性检查。库和Python代码中声明的库。相比之下,上述替代方案都能够在CAPI级别上运行,使用C头文件来确保被包装的库导出的接口与Python包装器模块所期望的接口之间的一致性。虽然cffi可以直接在CABI级别运行,但它会遇
原文和译文剩余内容已隐藏,您需要先支付 30元 才能查看原文和译文全部内容!立即支付