📄 index.lxp@lxpwrap=c88_252ehtm.htm
字号:
></LI><LI><P>Built-in regular expression engine that works on both regular and Unicode strings. </P></LI><LI><P>Use of indentation instead of braces <TTCLASS="FUNCTION">begin</TT>/<TTCLASS="FUNCTION">end</TT> pairs to delimit blocks of code. This practically forces readable code.</P></LI></UL><P>Your Python code resides in files, ending with <TTCLASS="FILENAME">.py</TT> suffix. These files can be grouped in modules, in the form of directories with an indexfile called <TTCLASS="FILENAME">__init__.py</TT>, and you can import elements from modules and files in other files. There is one file you use to start your application. It will usually simply import the necessary modules and start the application explicitly in a <TTCLASS="FUNCTION">main (args)</TT> function.</P><P>Maybe the introduction is bit <SPAN><ICLASS="EMPHASIS">early</I></SPAN> to start with actual code examples, but let's have an example of a Python bootstrap script anyway:</P><DIVCLASS="EXAMPLE"></A><P><B>Example 1-1. Bootstrapping a Python application</B></P><PRECLASS="PROGRAMLISTING">#!/usr/bin/env python <IMGSRC="images/callouts/1.gif"HSPACE="0"VSPACE="0"BORDER="0"ALT="(1)"></A>## bootstrap.py#import sys <IMGSRC="images/callouts/2.gif"HSPACE="0"VSPACE="0"BORDER="0"ALT="(2)"></A>from myapp import SomeClass <IMGSRC="images/callouts/3.gif"HSPACE="0"VSPACE="0"BORDER="0"ALT="(3)"></A>def main(args): <IMGSRC="images/callouts/4.gif"HSPACE="0"VSPACE="0"BORDER="0"ALT="(4)"></A> class=SomeClass(args) class.exec_loop()if __name__=="__main__": <IMGSRC="images/callouts/5.gif"HSPACE="0"VSPACE="0"BORDER="0"ALT="(5)"></A> main(sys.argv) </PRE><DIVCLASS="CALLOUTLIST"><DLCOMPACT="COMPACT"><DT><A href="index.lxp@lxpwrap=c88_252ehtm.htm#HASHBANG"><IMGSRC="images/callouts/1.gif"HSPACE="0"VSPACE="0"BORDER="0"ALT="(1)"></A></DT><DD>The so-called ‘hash-bang' trick is useful on Unix systems only. If the first line of any text file starts with #!, then the system will try to execute the application that follows the #! with the rest of the file as input. In this case, the <BCLASS="COMMAND">env</B> utility starts <BCLASS="COMMAND">python</B>, which runs the rest of the script.</DD><DT><A href="index.lxp@lxpwrap=c88_252ehtm.htm#IMPSYS"><IMGSRC="images/callouts/2.gif"HSPACE="0"VSPACE="0"BORDER="0"ALT="(2)"></A></DT><DD>The standard Python module <BCLASS="COMMAND">sys</B> handles tasks like passing on command-line arguments and lots of other things. Here we import the module, so we can pass the command-line arguments to the application. </DD><DT><A href="index.lxp@lxpwrap=c88_252ehtm.htm#IMPMYAPP"><IMGSRC="images/callouts/3.gif"HSPACE="0"VSPACE="0"BORDER="0"ALT="(3)"></A></DT><DD>All application code is in separate modules; the first of these we import here.</DD><DT><A href="index.lxp@lxpwrap=c88_252ehtm.htm#DEFMAIN"><IMGSRC="images/callouts/4.gif"HSPACE="0"VSPACE="0"BORDER="0"ALT="(4)"></A></DT><DD>This is the definition of the main function. By encapsulating this code in a function, it won't get run if this file were imported from another file.</DD><DT><A href="index.lxp@lxpwrap=c88_252ehtm.htm#IFNAME"><IMGSRC="images/callouts/5.gif"HSPACE="0"VSPACE="0"BORDER="0"ALT="(5)"></A></DT><DD>In this line, we check if this is a top-level script, instead of a file imported from another file. This is done by looking at the variable <TTCLASS="VARNAME">__name__</TT>. If this is the toplevel file, then the <TTCLASS="FUNCTION">main(args)</TT> is run.</DD></DL></DIV></DIV><P>Python is, like Java, a language that is compiled to bytecode. Python uses a virtual machine to run the bytecode. This virtual machine is written in C and interprets each byte-code instruction, translates it to real machine code and then runs it. The Python virtual machine differs from the Java virtual machine in that the byte-code instructions are a bit more high-level, and that there are no JIT-compilers that pre-compile chunks of byte-code to native machine code.</P><P>The translation from Python code to byte-code only happens once: Python saves a compiled version of your code in another file with the extension <TTCLASS="FILENAME">.pyc</TT>, or an optimized compiled version of your code that removes assert statements and line-number tracking in a file with the extension <TTCLASS="FILENAME">.pyo</TT>.</P><P>However, that is only done with Python files that are imported from other files: the bootstrap script will be compiled to bytecode every time you run it, but python will create a <TTCLASS="FILENAME">myapp.pyc</TT> from a file <TTCLASS="FILENAME">myapp.py</TT> (which is not shown here).</P><P>Interpreted languages, even byte-code interpreted languages, have a reputation for sluggishness. On the other hand, modern computers have a well-deserved reputation for excessive processing power. The combination means that an application written in a interpreted language can be fast enough for almost any needs.</P><P>Certainly, anyone who has ever tried to use a full-scale Java GUI application will know the exact meaning of the expression ‘slow as frozen treacle'. There are several reasons for the abominable slowness of Java applications, the most important of which is the fact that all Java Swing gui elements are also written in Java. Every pixel is put on screen by Java. Python, on the other hand, makes clever use of available GUI libraries that are coded in C or C++ and thus run as native machine code.</P><P>The ease with which Python can make use of native libraries is one of its strong points. Thanks to this extensibility, you can write the logic of your application in Python, and later rewrite the bottlenecks in C or C++. But even without writing extension libraries, I have never encountered any problem with the performance of a Python application. </P></DIV></DIV><DIVCLASS="NAVFOOTER"><HRALIGN="LEFT"WIDTH="100%"><TABLESUMMARY="Footer navigation table"WIDTH="100%"BORDER="0"CELLPADDING="0"CELLSPACING="0"><TR><TDWIDTH="33%"ALIGN="left"VALIGN="top"><A accesskey="P" href="index.lxp@lxpwrap=x76_252ehtm.htm">Prev</A></TD><TDWIDTH="34%"ALIGN="center"VALIGN="top"><A accesskey="H" href="index.lxp@lxpwrap=book1_252ehtm">Home</A></TD><TDWIDTH="33%"ALIGN="right"VALIGN="top"><A accesskey="N" href="index.lxp@lxpwrap=x179_252ehtm.htm">Next</A></TD></TR><TR><TDWIDTH="33%"ALIGN="left"VALIGN="top">Acknowledgments</TD><TDWIDTH="34%"ALIGN="center"VALIGN="top"> </TD><TDWIDTH="33%"ALIGN="right"VALIGN="top">GUI programming with Python</TD></TR></TABLE></DIV></BODY></HTML> </td> </tr> </table> </td> </tr> </table>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -