⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 222.html

📁 Python Ebook Python&XML
💻 HTML
📖 第 1 页 / 共 4 页
字号:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=iso-8859-1">
<META NAME="Robots" content="INDEX,NOFOLLOW">
<META HTTP-EQUIV="Pragma" CONTENT="no-cache">
<TITLE>Safari | Python Developer's Handbook -&gt; Development Strategy</TITLE>
<LINK REL="stylesheet" HREF="oreillyi/oreillyN.css">
</HEAD>
<BODY bgcolor="white" text="black" link="#990000" vlink="#990000" alink="#990000" leftmargin="0" topmargin="0" marginwidth="0" marginheight="0">

<table width="100%" cellpadding=5 cellspacing=0 border=0 class="navtopbg"><tr><td><font size="1"><p class="navtitle"><a href="8.html" class="navtitle">Web Development</a> &gt; <a href="0672319942.html" class="navtitle">Python Developer's Handbook</a> &gt; <a href="219.html" class="navtitle">16. Development Environment</a> &gt; <span class="nonavtitle">Development Strategy</span></p></font></td><td align="right" valign="top" nowrap><font size="1"><a href="main.asp?list" class="safnavoff">See All Titles</a></font></td></tr></table>
<TABLE width=100% bgcolor=white border=0 cellspacing=0 cellpadding=5><TR><TD>
<TABLE border=0 width="100%" cellspacing=0 cellpadding=0><TR><td align=left width="15%" class="headingsubbarbg"><a href="221.html" title="Building Python Applications"><font size="1">&lt;&nbsp;BACK</font></a></td><td align=center width="70%" class="headingsubbarbg"><font size="1"><a href="popanote.asp?pubui=oreilly&bookname=0672319942&snode=222" target="_blank" title="Make a public or private annnotation">Make Note</a> | <a href="222.html" title="Use a Safari bookmark to remember this section">Bookmark</a></font></td><td align=right width="15%" class="headingsubbarbg"><a href="223.html" title="Integrated Development Environments"><font size="1">CONTINUE&nbsp;&gt;</font></a></td></TR></TABLE>
<a href="6%2F1%2F2002+6%3A16%3A27+PM.html" TABINDEX="-1"><img src=images/spacer.gif border=0 width=1 height=1></a><font color=white size=1>152015024128143245168232148039196038240039088173205162105045222217074025151062064201046</font><a href="read2.asp?bookname=0672319942&snode=222&now=6%2F1%2F2002+6%3A16%3A27+PM" TABINDEX="-1"><img src=images/spacer.gif border=0 width=1 height=1></a><br>
<FONT>
				<h3>Development Strategy</h3>
				<p>Writing a program is something very easy, but writing a good and optimized program requires some level of experience. A good way to start is to learn all the nuances of the language, which in our case involves learning Python. You should know a little bit of everything, and this book helps you learn most of them, including classes, modules, functions, exception handling, dynamic typing, GUI, operator overloading, indentation, and so forth.</p>

				<P>Of course, you must know many other important items too.</P>

				<P>Nowadays, the most important development efforts are focusing on the Internet. Python offers the basic necessary tools that you might need for your  Web projects. Python can be used either for Web-based interface projects or to generate entire back-end frameworks, using tools such as Zope.</P>

				<p>Note that by extending Grail, the Web browser written in Python, you can embed your Python application directly on it and distribute a browser to your clients that carries specific and customized interfaces.</p>

				<p>Even if you don't use Grail, you can use any browser to provide GUI interfaces for your applications. Have you ever considered delivering information and products through the Web? If so, you can do it using Python.<a NAME="idx1073749407"></a>
					<a naME="idx1073749408"></A>
					<A name="idx1073749409"></a>
					<a name="idx1073749410"></a>
					<a name="idx1073749411"></a>
					<a naMe="idx1073749412"></a>
					<A namE="idx1073749413"></a>
					<a naMe="idx1073749414"></a>
				</p>

				<P>Python is a perfect language for project prototyping. Python's design allows you to make changes very quickly. Later you can decide whether you will re-implement the code using a compiled language, or stick to Python and continue the development effort using the prototype as a startup. Remember that after spending some time creating a prototype, you probably have a huge amount of code that you do not want to throw away.</P>

				<P>
					<A name="idx1073749415"></A>Prototyping with Python is very easy. You can, for example, wrap your code in a function inside a module and use a development environment, such as Pythonwin or IDLE, to run the script. To test this application, you just need to save it and execute it梫ery simple. No intermediate stages are necessary.</P>

				<P>Python testing mechanisms also allow you to forge command-line arguments. You can  <A name="idx1073749416"></A>
					<A NAme="idx1073749417"></a>
					<a NAME="idx1073749418"></a>test your command-line scripts by first setting their expected arguments to predefined values using the built-in variable <tt class="monofont">sys.argv.</tt>
				</p>

				<p>Along the development stage, you will soon see that Python can be easily used to code entire applications, without discarding the prototyped code.</p>

				<p>If speed is a requirement, you can use a compiled language in the back-end side of your application to support the high-demand operations. Python, in this case, can be used as the front end of the application, leaving the hard work to the other language. This kind of implementation allows you to create black boxes of code, which get called by Python, and Python doesn't necessarily need to know what is happening behind the scenes because only the external interface of the compiled language needs to be exposed.</p>

				<p>But whenever possible, select just Python. It is good to remember that supporting a scripting language is much easier than supporting a compiled language. The usage of a scripting language makes tasks such as debug the application, fix bugs, and add enhancements look very simple. Because we are not using a compiled language, we don't need to spend time compiling and linking the files. Updating client sites with the latest version of the application is also very easy because we just need to send the file that carries the changed Python module.</p>

				<p>As you can see, a lot of thinking is involved in the process of preparing yourself to handle a Python development. Next, we will see some ideas about how to optimize your code, and how to write a program with style. Both are very important things that you must have in mind, not only when using Python, but also when writing in any other language.<a nAme="idx1073749419"></A>
					<a naMe="idx1073749420"></a>
					<a nAme="idx1073749421"></a>
					<A NAMe="idx1073749422"></a>
					<a nAME="idx1073749423"></A>
					<a namE="idx1073749424"></A>
					<A Name="idx1073749425"></a>
					<A NAMe="idx1073749426"></a>
				</p>

				
					<h4>







Optimizing the Code</h4>
					<p>To prevent your program from running very slowly, you might consider following some basic Python optimization rules. By designing your application from the start with these guidelines in mind, you will certainly be satisfied with the final overall performance that you will get.</p>

					<p>My goal in this section is to provide ways to generate acceptable performance in your Python routines. Note that I don't cover everything, but a good set of basic concepts is covered.</p>

					<p>Many things can be done to reduce the processing time of your application. Remember that you have an interpreter being called every time you execute a Python script. Consequently, you need to work on your code in order to compensate that somehow. The fact that it is an interpreted language is a big concern, but by reducing the number of statements that get parsed, you can also reduce the interpreter overhead.</p>

					<p>By the way, the Python interpreter has a <a name="idx1073749427"></a>
						<a nAme="idx1073749428"></A>
						<a naMe="idx1073749429"></a>command-line option (<tt ClasS="monofont">-O,</TT> which stands for <I>optimize</i>) that enables you to execute your code in such a way that some of the bytecode operations are not executed. Basically, it is used to remove the comments in the bytecode that give the line number where exceptions occur, and does not compile in the doc strings and a few other things. This flag does not give that much speed increase, and it makes things harder to debug.<a naME="idx1073749430"></A>
						<A name="idx1073749431"></A>
						<A NAme="idx1073749432"></a>
						<a NAME="idx1073749433"></a>
						<a name="idx1073749434"></a>
						<a name="idx1073749435"></a>
						<a name="idx1073749436"></a>
						<a NamE="idx1073749437"></a>
						<a nAme="idx1073749438"></a>
						<a Name="idx1073749439"></A>
						<A NAme="idx1073749440"></a>
						<a NAME="idx1073749441"></a>
						<a naME="idx1073749442"></A>
						<A name="idx1073749443"></A>
						<A NAme="idx1073749444"></a>
						<a name="idx1073749445"></a>
					</p>

					<p>Some useful optimization hints are as follows:</p>

					<ul>
<li>
							<p>
								<a naMe="idx1073749446"></a>Variables桪epending on how your variables are defined, the interpreter spends more or less time trying to figure out their values. Python deals with dynamic scope rules when trying to resolve variable names. After it finds a variable in the code, it first tries to discover if the variable is a local variable by looking at the local namespace dictionary. If it finds the variable, it grabs the variable's value. Otherwise, it searches in the global namespace dictionary, and if necessary, in the built-in namespace dictionary. As you can see, local variable lookups are pretty much faster than other types. Consequently, the access to their values is faster too. Also, local variable lookups are much faster because they correspond to indexing into an array, whereas global variable lookups correspond to hash table lookups. A good optimization hint might be that if you are using a global variable a lot in a function, assigning its value to a local variable can help a lot.<A namE="idx1073749447"></a>
							</p>

						</li>
<Li>
							<p>
								<a NAME="idx1073749448"></a>Modules梂ithin a single script, you just need to import an external module once. Therefore, it is not necessary to have multiple <tt cLASS="monofont">import</tt> statements inside your code. Actually, you should avoid trying to re-import modules on your program. As a rule of thumb, put all the <tt CLASs="monofont">import</tt> statements in the very first part of your program header. However, calling <tT CLAss="monofont">import</tt> on a module multiple times is not really a problem because it is just a dictionary lookup.</p>

							<p>In cases where you have to do a lot of referencing to particular attributes of an external module, you should consider copying those elements to a single variable (when that's possible, of course) before starting to code梕specially, if the references are made inside a loop.<a name="idx1073749449"></a>
								<a name="idx1073749450"></a>
								<a nAme="idx1073749451"></A>
								<a naMe="idx1073749452"></a>
								<a nAme="idx1073749453"></a>
								<A NAMe="idx1073749454"></a>
								<a nAME="idx1073749455"></A>
								<a namE="idx1073749456"></A>
								<A Name="idx1073749457"></a>
								<A NAMe="idx1073749458"></a>
								<a name="idx1073749459"></a>
								<a name="idx1073749460"></a>
								<a name="idx1073749461"></a>
								<A naMe="idx1073749462"></a>
								<a Name="idx1073749463"></a>
								<A namE="idx1073749464"></A>
								<A Name="idx1073749465"></a>
							</P>

							<P>Whenever you <A Name="idx1073749466"></a>import a module, the interpreter looks for a byte-compiled version of the module. In case it doesn't find any, it automatically bytecompiles the module and generates a .<TT CLass="monofont">pyc</tT> file. So, the next time you try to import the module, the byte-compiled file will be there. As you can feel, <TT Class="monofont">.pyc</tt> files are executed much faster than regular <tt class="monofont">.py</tt> files because they have already being interpreted by the interpreter prior to the execution. The suggestion here is to use byte-compiled modules the more you can. The Python code executes at the same speed no matter if there is a <tt claSs="monofont">.pyc</tT> file or not. The only difference is that if there is a byte-compiled file, startup will be a bit quicker. The actual running speed of the code is no different.<a naMe="idx1073749467"></a>
							</p>

						</lI>
<li>
							<p>
								<A NAMe="idx1073749468"></a>Strings桿se format strings whenever you need to concatenate strings with other variables. Check out the next concatenation forms.</p>

							<pRE>
								
name = "Andre"
print "Hello " + name
print "Hello %s" % (name)

							</PRe>

							<p>Be sure that the second <tt CLASs="monofont">print</tt> statement is more optimized than the first one. The parentheses on the third line are not necessary. Another option would be</p>

							<PRE>
								
print "Hello", name

							</Pre>

						</li>
<li>
							<p>
								<a name="idx1073749469"></a>Tkinter桝void creating unnecessary instances of widgets. If you are not planning to manipulate the attributes of a widget after it has been created, stick to direct calls to the class. In a GUI app, this won't affect the running speed that much梛ust the startup time.</p>

							<p>There is no reason to say</p>

							<pre>
								
mybutton = Button(root, text="Close")
mybutton.pack(side=right)

							</pRe>

							<p>when you can simply use</P>

							<pre>
								
mybutton = Button(root, text="Close").pack(side=right) 

							</Pre>

							<p>Now, the interpreter has one less variable to handle.</p>

							<P>I open a parenthesis here to let you know that if you are testing a Tkinter application using IDLE, you need to comment your <a naME="idx1073749470"></A>
								<A name="idx1073749471"></A>
								<TT Class="monofont">mainloop()</TT> command. That's because IDLE is already running inside a Tkinter mainloop, and calling another one might freeze your entire environment.</P>

						</Li>
<li>
							<p>
								<A NAMe="idx1073749472"></a>Loops梇ou can optimize a lot of things in your loops in order to make them run smoothly. In a short list, I can tell you the following:</p>

							<ul>
<li>
									<p>You should use built-in functions in your inner loop instead of using functions written in Python. By using built-in functions that support list manipulation (such as <tt class="monofont">map(),</tt>
										<tt cLasS="monofont">reduce(),</tt> and <tT clasS="monofont">filter()</tt>) instead of straight loops, you can move some of the loop overhead to the C code. Passing built-in functions to <tT CLAss="monofont">map,</tt>
										<TT CLass="monofont">reduce,</tT> or <TT Class="monofont">filter</TT> gives even better performance.</P>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -