page125.html

来自「Data Structures And Algorithms With Obje」· HTML 代码 · 共 49 行

HTML
49
字号
<HTML><HEAD><TITLE>The Container Class __str__ Method</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="tex2html2649" HREF="page126.html"><IMG WIDTH=37 HEIGHT=24 ALIGN=BOTTOM ALT="next" SRC="../icons/next_motif.gif"></A> <A NAME="tex2html2647" HREF="page123.html"><IMG WIDTH=26 HEIGHT=24 ALIGN=BOTTOM ALT="up" SRC="../icons/up_motif.gif"></A> <A NAME="tex2html2643" HREF="page124.html"><IMG WIDTH=63 HEIGHT=24 ALIGN=BOTTOM ALT="previous" SRC="../icons/previous_motif.gif"></A>  <A NAME="tex2html2651" HREF="page611.html"><IMG WIDTH=43 HEIGHT=24 ALIGN=BOTTOM ALT="index" SRC="../icons/index_motif.gif"></A> <BR><HR><H3><A NAME="SECTION005252000000000000000">The <tt>Container</tt> Class <tt>__str__</tt> Method</A></H3><P>One of the methods defined in the Python <tt>object</tt> classis the <tt>__str__</tt> method.Consequently, every Python object supports the <tt>__str__</tt> method.The <tt>__str__</tt> method is required to return a stringthat represents the object ``textually.''It is typically invoked in situationswhere it is necessary to print out a human-readable representation of an object.<P>Program&nbsp;<A HREF="page125.html#progcontainerd"><IMG  ALIGN=BOTTOM ALT="gif" SRC="../icons/cross_ref_motif.gif"></A> defines the <tt>__str__</tt> methodof the abstract <tt>Container</tt> class.This method is provided to simplify the implementation of classes derivedfrom the <tt>Container</tt> class.The default behavior is to print out the name of the classand then to print each of the elements in the container,by using the <tt>accept</tt> method together with a visitor.<P><P><A NAME="4975">&#160;</A><A NAME="progcontainerd">&#160;</A> <IMG WIDTH=575 HEIGHT=447 ALIGN=BOTTOM ALT="program4725" SRC="img652.gif"  ><BR><STRONG>Program:</STRONG> <tt>Container</tt> class <tt>__str__</tt> method.<BR><P><P>The <tt>StrVisitor</tt> is a visitor.It uses uses its <tt>_string</tt> instance attributeto accumulate the textual representations of the objects it visits.(It also makes sure to put in commas as required).<P>The final result returned by the <tt>Container</tt> class<tt>__str__</tt> method consists of the name of the container class,followed by a comma-separated list of the contents of that containerenclosed in braces <code>{</code> and <code>}</code>.<P><HR><A NAME="tex2html2649" HREF="page126.html"><IMG WIDTH=37 HEIGHT=24 ALIGN=BOTTOM ALT="next" SRC="../icons/next_motif.gif"></A> <A NAME="tex2html2647" HREF="page123.html"><IMG WIDTH=26 HEIGHT=24 ALIGN=BOTTOM ALT="up" SRC="../icons/up_motif.gif"></A> <A NAME="tex2html2643" HREF="page124.html"><IMG WIDTH=63 HEIGHT=24 ALIGN=BOTTOM ALT="previous" SRC="../icons/previous_motif.gif"></A>  <A NAME="tex2html2651" 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 &#169; 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 + =
减小字号Ctrl + -
显示快捷键?