📄 page419.html
字号:
<HTML><HEAD><TITLE>Reuse</TITLE></HEAD><BODY bgcolor="#FFFFFF"> <a href="../index.html" target="_top"><img src="../icons/usins.gif" alt="Logo" align=right></a><b>Data Structures and Algorithms with Object-Oriented Design Patterns in Python</b><br><A NAME="tex2html6009" HREF="page420.html"><IMG WIDTH=37 HEIGHT=24 ALIGN=BOTTOM ALT="next" SRC="../icons/next_motif.gif"></A> <A NAME="tex2html6007" HREF="page417.html"><IMG WIDTH=26 HEIGHT=24 ALIGN=BOTTOM ALT="up" SRC="../icons/up_motif.gif"></A> <A NAME="tex2html6001" HREF="page418.html"><IMG WIDTH=63 HEIGHT=24 ALIGN=BOTTOM ALT="previous" SRC="../icons/previous_motif.gif"></A> <A NAME="tex2html6011" HREF="page611.html"><IMG WIDTH=43 HEIGHT=24 ALIGN=BOTTOM ALT="index" SRC="../icons/index_motif.gif"></A> <BR><HR><H4><A NAME="SECTION0013110200000000000000">Reuse</A></H4><P>Sometimes, a Python program will create many objects which are used only once.For example, a program may create an object in the body of a loopthat is used to hold ``temporary'' information that is only requiredfor the particular iteration of the loop in which it is created.Consider the following:<PRE>for i in xrange(1000000): obj = SomeClass(i) print obj</PRE>This creates a million instancesof the <tt>SomeClass</tt> class and prints them out.If the <tt>SomeClass</tt> class has a propery, say <tt>value</tt>,we can reuse an a single object instance like this:<PRE>obj = SomeClass()for i in xrange(1000000): obj.value = i print obj</PRE>Clearly, by reusing a single object instance,we have dramatically reduced the amount of garbage produced.<P><HR><A NAME="tex2html6009" HREF="page420.html"><IMG WIDTH=37 HEIGHT=24 ALIGN=BOTTOM ALT="next" SRC="../icons/next_motif.gif"></A> <A NAME="tex2html6007" HREF="page417.html"><IMG WIDTH=26 HEIGHT=24 ALIGN=BOTTOM ALT="up" SRC="../icons/up_motif.gif"></A> <A NAME="tex2html6001" HREF="page418.html"><IMG WIDTH=63 HEIGHT=24 ALIGN=BOTTOM ALT="previous" SRC="../icons/previous_motif.gif"></A> <A NAME="tex2html6011" HREF="page611.html"><IMG WIDTH=43 HEIGHT=24 ALIGN=BOTTOM ALT="index" SRC="../icons/index_motif.gif"></A> <P><ADDRESS><img src="../icons/bruno.gif" alt="Bruno" align=right><a href="../copyright.html">Copyright © 2003</a> by <a href="../signature.html">Bruno R. Preiss, P.Eng.</a> All rights reserved.</ADDRESS></BODY></HTML>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -