﻿<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/"><channel><title>C++博客-fenglin-文章分类-Python</title><link>http://www.cppblog.com/fenglin/category/14809.html</link><description>创新、创意、挑战</description><language>zh-cn</language><lastBuildDate>Fri, 10 Sep 2010 19:38:21 GMT</lastBuildDate><pubDate>Fri, 10 Sep 2010 19:38:21 GMT</pubDate><ttl>60</ttl><item><title>django框架安装与配置</title><link>http://www.cppblog.com/fenglin/articles/126082.html</link><dc:creator>风林</dc:creator><author>风林</author><pubDate>Tue, 07 Sep 2010 04:16:00 GMT</pubDate><guid>http://www.cppblog.com/fenglin/articles/126082.html</guid><wfw:comment>http://www.cppblog.com/fenglin/comments/126082.html</wfw:comment><comments>http://www.cppblog.com/fenglin/articles/126082.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/fenglin/comments/commentRss/126082.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/fenglin/services/trackbacks/126082.html</trackback:ping><description><![CDATA[django是一个开源框架项目，详情请见官网http://www.djangoproject.com<img src ="http://www.cppblog.com/fenglin/aggbug/126082.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/fenglin/" target="_blank">风林</a> 2010-09-07 12:16 <a href="http://www.cppblog.com/fenglin/articles/126082.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>学python两天半时间写的成果</title><link>http://www.cppblog.com/fenglin/articles/125516.html</link><dc:creator>风林</dc:creator><author>风林</author><pubDate>Wed, 01 Sep 2010 05:11:00 GMT</pubDate><guid>http://www.cppblog.com/fenglin/articles/125516.html</guid><wfw:comment>http://www.cppblog.com/fenglin/comments/125516.html</wfw:comment><comments>http://www.cppblog.com/fenglin/articles/125516.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/fenglin/comments/commentRss/125516.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/fenglin/services/trackbacks/125516.html</trackback:ping><description><![CDATA[<div style="BORDER-BOTTOM: #cccccc 1px solid; BORDER-LEFT: #cccccc 1px solid; PADDING-BOTTOM: 4px; BACKGROUND-COLOR: #eeeeee; PADDING-LEFT: 4px; WIDTH: 98%; PADDING-RIGHT: 5px; FONT-SIZE: 13px; BORDER-TOP: #cccccc 1px solid; BORDER-RIGHT: #cccccc 1px solid; PADDING-TOP: 4px"><span style="COLOR: #008000">#</span><span style="COLOR: #008000">!/usr/bin/python</span><span style="COLOR: #008000"><br>#</span><span style="COLOR: #008000">Filename:myAddressBook.py</span><span style="COLOR: #008000"><br></span><span style="COLOR: #000000"><br></span><span style="COLOR: #0000ff">import</span><span style="COLOR: #000000">&nbsp;os<br></span><span style="COLOR: #008000">#</span><span style="COLOR: #008000">used&nbsp;cPlickle&nbsp;store</span><span style="COLOR: #008000"><br></span><span style="COLOR: #0000ff">import</span><span style="COLOR: #000000">&nbsp;cPickle&nbsp;as&nbsp;p<br><br></span><span style="COLOR: #008000">#</span><span style="COLOR: #008000">#######class&nbsp;Person##############</span><span style="COLOR: #008000"><br></span><span style="COLOR: #0000ff">class</span><span style="COLOR: #000000">&nbsp;Person:<br>&nbsp;&nbsp;</span><span style="COLOR: #0000ff">def</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #800080">__init__</span><span style="COLOR: #000000">(self,name,email):<br>&nbsp;&nbsp;&nbsp;&nbsp;self.name&nbsp;</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">&nbsp;name<br>&nbsp;&nbsp;&nbsp;&nbsp;self.email&nbsp;</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">&nbsp;email<br><br>&nbsp;&nbsp;</span><span style="COLOR: #0000ff">def</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #800080">__del__</span><span style="COLOR: #000000">(self):<br>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">pass</span><span style="COLOR: #000000"><br></span><span style="COLOR: #008000">#</span><span style="COLOR: #008000">###############################</span><span style="COLOR: #008000"><br></span><span style="COLOR: #000000"><br></span><span style="COLOR: #008000">#</span><span style="COLOR: #008000">######class&nbsp;AddressBook#######</span><span style="COLOR: #008000"><br></span><span style="COLOR: #0000ff">class</span><span style="COLOR: #000000">&nbsp;AddressBook:<br>&nbsp;&nbsp;</span><span style="COLOR: #008000">#</span><span style="COLOR: #008000">the&nbsp;long&nbsp;name&nbsp;of&nbsp;data&nbsp;file</span><span style="COLOR: #008000"><br></span><span style="COLOR: #000000">&nbsp;&nbsp;addressFile&nbsp;</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #800000">'</span><span style="COLOR: #800000">Address.data</span><span style="COLOR: #800000">'</span><span style="COLOR: #000000"><br>&nbsp;&nbsp;<br>&nbsp;&nbsp;</span><span style="COLOR: #008000">#</span><span style="COLOR: #008000">dictionary</span><span style="COLOR: #008000"><br></span><span style="COLOR: #000000">&nbsp;&nbsp;addressList&nbsp;</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">{}<br>&nbsp;<br>&nbsp;&nbsp;<br>&nbsp;&nbsp;</span><span style="COLOR: #0000ff">def</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #800080">__init__</span><span style="COLOR: #000000">(self):<br>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #008000">#</span><span style="COLOR: #008000">If&nbsp;the&nbsp;data&nbsp;file&nbsp;of&nbsp;addressbook&nbsp;is&nbsp;not&nbsp;exists,then&nbsp;create&nbsp;it.&nbsp;</span><span style="COLOR: #008000"><br></span><span style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">if</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #0000ff">not</span><span style="COLOR: #000000">&nbsp;os.path.isfile(AddressBook.addressFile):&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;f&nbsp;</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">&nbsp;file(AddressBook.addressFile,</span><span style="COLOR: #800000">'</span><span style="COLOR: #800000">w</span><span style="COLOR: #800000">'</span><span style="COLOR: #000000">)<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;p.dump(AddressBook.addressList,f)<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;f.close()<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">print</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #800000">'</span><span style="COLOR: #800000">Created&nbsp;file&nbsp;successfull!</span><span style="COLOR: #800000">'</span><span style="COLOR: #000000"><br>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">else</span><span style="COLOR: #000000">:<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #008000">#</span><span style="COLOR: #008000">If&nbsp;the&nbsp;data&nbsp;file&nbsp;of&nbsp;addressbook&nbsp;is&nbsp;exists,then&nbsp;list&nbsp;all.</span><span style="COLOR: #008000"><br></span><span style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;f&nbsp;</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">&nbsp;file(AddressBook.addressFile)<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;AddressBook.addressList&nbsp;</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">&nbsp;p.load(f)<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">print</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #800000">'</span><span style="COLOR: #800000">Current&nbsp;address&nbsp;list:</span><span style="COLOR: #800000">'</span><span style="COLOR: #000000"><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;f.close()<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;self.List()</span><span style="COLOR: #008000">#</span><span style="COLOR: #008000">call&nbsp;List&nbsp;function</span><span style="COLOR: #008000"><br></span><span style="COLOR: #000000">&nbsp;&nbsp;<br>&nbsp;&nbsp;</span><span style="COLOR: #008000">#</span><span style="COLOR: #008000">When&nbsp;destroy&nbsp;object,save&nbsp;data&nbsp;to&nbsp;file.</span><span style="COLOR: #008000"><br></span><span style="COLOR: #000000">&nbsp;&nbsp;</span><span style="COLOR: #0000ff">def</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #800080">__del__</span><span style="COLOR: #000000">(self):<br>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">try</span><span style="COLOR: #000000">:<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">try</span><span style="COLOR: #000000">:&nbsp;&nbsp;&nbsp;&nbsp;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;f&nbsp;</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">&nbsp;file(AddressBook.addressFile,</span><span style="COLOR: #800000">'</span><span style="COLOR: #800000">w</span><span style="COLOR: #800000">'</span><span style="COLOR: #000000">)<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;p.dump(AddressBook.addressList,f)&nbsp;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">print</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #800000">'</span><span style="COLOR: #800000">Save&nbsp;data&nbsp;successfull!</span><span style="COLOR: #800000">'</span><span style="COLOR: #000000">&nbsp;&nbsp;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">except</span><span style="COLOR: #000000">:<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">print</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #800000">'</span><span style="COLOR: #800000">Save&nbsp;data&nbsp;fail!</span><span style="COLOR: #800000">'</span><span style="COLOR: #000000"><br>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">finally</span><span style="COLOR: #000000">:<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;f.close()<br>&nbsp;&nbsp;<br>&nbsp;&nbsp;</span><span style="COLOR: #008000">#</span><span style="COLOR: #008000">purpose:Add&nbsp;new&nbsp;record</span><span style="COLOR: #008000"><br></span><span style="COLOR: #000000">&nbsp;&nbsp;</span><span style="COLOR: #0000ff">def</span><span style="COLOR: #000000">&nbsp;Add(self,pepole):<br>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">if</span><span style="COLOR: #000000">&nbsp;len(pepole.name)</span><span style="COLOR: #000000">==</span><span style="COLOR: #000000">0:<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">print</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #800000">'</span><span style="COLOR: #800000">The&nbsp;length&nbsp;of&nbsp;name&nbsp;must&nbsp;longer&nbsp;than&nbsp;zero.</span><span style="COLOR: #800000">'</span><span style="COLOR: #000000"><br>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">elif</span><span style="COLOR: #000000">&nbsp;len(pepole.email)</span><span style="COLOR: #000000">==</span><span style="COLOR: #000000">0:<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">print</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #800000">'</span><span style="COLOR: #800000">The&nbsp;length&nbsp;of&nbsp;emial&nbsp;must&nbsp;longer&nbsp;than&nbsp;zero.</span><span style="COLOR: #800000">'</span><span style="COLOR: #000000"><br>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">else</span><span style="COLOR: #000000">:<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;pr&nbsp;</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">&nbsp;Person(pepole.name,pepole.email)<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;AddressBook.addressList[pepole.name]&nbsp;</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">&nbsp;pr<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">print</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #800000">'</span><span style="COLOR: #800000">Add&nbsp;new&nbsp;person&nbsp;successfull!</span><span style="COLOR: #800000">'</span><span style="COLOR: #000000"><br><br>&nbsp;&nbsp;</span><span style="COLOR: #008000">#</span><span style="COLOR: #008000">purpose:Delete&nbsp;a&nbsp;record&nbsp;by&nbsp;name</span><span style="COLOR: #008000"><br></span><span style="COLOR: #000000">&nbsp;&nbsp;</span><span style="COLOR: #0000ff">def</span><span style="COLOR: #000000">&nbsp;Del(self,name):<br>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">if</span><span style="COLOR: #000000">&nbsp;AddressBook.addressList.has_key(name):<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">del</span><span style="COLOR: #000000">&nbsp;AddressBook.addressList[name]<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">print</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #800000">'</span><span style="COLOR: #800000">Deleted&nbsp;%s&nbsp;successfull!</span><span style="COLOR: #800000">'</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #000000">%</span><span style="COLOR: #000000">&nbsp;name<br>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">else</span><span style="COLOR: #000000">:<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">print</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #800000">'</span><span style="COLOR: #800000">The&nbsp;name&nbsp;is&nbsp;not&nbsp;exists.</span><span style="COLOR: #800000">'</span><span style="COLOR: #000000"><br>&nbsp;&nbsp;<br>&nbsp;&nbsp;</span><span style="COLOR: #008000">#</span><span style="COLOR: #008000">purpose:Modify&nbsp;a&nbsp;record&nbsp;by&nbsp;name</span><span style="COLOR: #008000"><br></span><span style="COLOR: #000000">&nbsp;&nbsp;</span><span style="COLOR: #0000ff">def</span><span style="COLOR: #000000">&nbsp;Edit(self,name,people):<br>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">if</span><span style="COLOR: #000000">&nbsp;AddressBook.addressList.has_key(name):<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;AddressBook.addressList[name]&nbsp;</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">&nbsp;people<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">print</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #800000">'</span><span style="COLOR: #800000">Edit&nbsp;%s\</span><span style="COLOR: #800000">'</span><span style="COLOR: #000000">s&nbsp;information&nbsp;successfull!</span><span style="COLOR: #800000">'</span><span style="COLOR: #800000">&nbsp;%&nbsp;name</span><span style="COLOR: #800000"><br></span><span style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">else</span><span style="COLOR: #000000">:<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">print</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #800000">'</span><span style="COLOR: #800000">The&nbsp;name&nbsp;is&nbsp;not&nbsp;exists.</span><span style="COLOR: #800000">'</span><span style="COLOR: #000000"><br><br>&nbsp;&nbsp;</span><span style="COLOR: #008000">#</span><span style="COLOR: #008000">purpose:Find&nbsp;a&nbsp;record&nbsp;by&nbsp;name</span><span style="COLOR: #008000"><br></span><span style="COLOR: #000000">&nbsp;&nbsp;</span><span style="COLOR: #0000ff">def</span><span style="COLOR: #000000">&nbsp;Find(self,name):<br>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">if</span><span style="COLOR: #000000">&nbsp;AddressBook.addressList.has_key(name):<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">print</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #800000">'</span><span style="COLOR: #800000">%s\</span><span style="COLOR: #800000">'</span><span style="COLOR: #000000">s&nbsp;information&nbsp;</span><span style="COLOR: #0000ff">is</span><span style="COLOR: #000000">&nbsp;:\nName:</span><span style="COLOR: #000000">%</span><span style="COLOR: #000000">s\nEmail:</span><span style="COLOR: #000000">%</span><span style="COLOR: #000000">s</span><span style="COLOR: #800000">'</span><span style="COLOR: #800000">\</span><span style="COLOR: #800000"><br></span><span style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #000000">%</span><span style="COLOR: #000000">&nbsp;(name,\<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;AddressBook.addressList[name].name,\<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;AddressBook.addressList[name].email)<br>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">else</span><span style="COLOR: #000000">:<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">print</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #800000">'</span><span style="COLOR: #800000">The&nbsp;name&nbsp;is&nbsp;not&nbsp;exists.</span><span style="COLOR: #800000">'</span><span style="COLOR: #000000"><br>&nbsp;&nbsp;<br>&nbsp;&nbsp;</span><span style="COLOR: #008000">#</span><span style="COLOR: #008000">purpose:List&nbsp;all&nbsp;record</span><span style="COLOR: #008000"><br></span><span style="COLOR: #000000">&nbsp;&nbsp;</span><span style="COLOR: #0000ff">def</span><span style="COLOR: #000000">&nbsp;List(self):<br>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">print</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #800000">'</span><span style="COLOR: #800000">\n</span><span style="COLOR: #800000">'</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #000000">+</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #800000">'</span><span style="COLOR: #800000">*</span><span style="COLOR: #800000">'</span><span style="COLOR: #000000">*</span><span style="COLOR: #000000">10</span><span style="COLOR: #000000">&nbsp;&nbsp;</span><span style="COLOR: #000000">+</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #800000">'</span><span style="COLOR: #800000">Address&nbsp;List</span><span style="COLOR: #800000">'</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #000000">+</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #800000">'</span><span style="COLOR: #800000">*</span><span style="COLOR: #800000">'</span><span style="COLOR: #000000">*</span><span style="COLOR: #000000">10</span><span style="COLOR: #000000">&nbsp;&nbsp;</span><span style="COLOR: #000000">+</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #800000">'</span><span style="COLOR: #800000">\n</span><span style="COLOR: #800000">'</span><span style="COLOR: #000000"><br>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">for</span><span style="COLOR: #000000">&nbsp;name,people&nbsp;</span><span style="COLOR: #0000ff">in</span><span style="COLOR: #000000">&nbsp;AddressBook.addressList.items():<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">print</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #800000">'</span><span style="COLOR: #800000">%s\</span><span style="COLOR: #800000">'</span><span style="COLOR: #000000">s&nbsp;information&nbsp;</span><span style="COLOR: #0000ff">is</span><span style="COLOR: #000000">&nbsp;:\nName:</span><span style="COLOR: #000000">%</span><span style="COLOR: #000000">s\nEmail:</span><span style="COLOR: #000000">%</span><span style="COLOR: #000000">s</span><span style="COLOR: #800000">'</span><span style="COLOR: #800000">&nbsp;%&nbsp;(name,people.name,people.email)</span><span style="COLOR: #800000"><br></span><span style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">print</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #800000">'</span><span style="COLOR: #800000">-</span><span style="COLOR: #800000">'</span><span style="COLOR: #000000">*</span><span style="COLOR: #000000">30</span><span style="COLOR: #000000"><br>&nbsp;&nbsp;&nbsp;&nbsp;<br></span><span style="COLOR: #008000">#</span><span style="COLOR: #008000">###############main####################################&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #008000"><br></span><span style="COLOR: #000000">os.system(</span><span style="COLOR: #800000">'</span><span style="COLOR: #800000">clear</span><span style="COLOR: #800000">'</span><span style="COLOR: #000000">)<br>ad&nbsp;</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">&nbsp;AddressBook()<br></span><span style="COLOR: #0000ff">while</span><span style="COLOR: #000000">&nbsp;True:<br>&nbsp;&nbsp;</span><span style="COLOR: #0000ff">print</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #800000">'</span><span style="COLOR: #800000">\n</span><span style="COLOR: #800000">'</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #000000">+</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #800000">'</span><span style="COLOR: #800000">*</span><span style="COLOR: #800000">'</span><span style="COLOR: #000000">*</span><span style="COLOR: #000000">10</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #000000">+</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #800000">'</span><span style="COLOR: #800000">Main&nbsp;menu</span><span style="COLOR: #800000">'</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #000000">+</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #800000">'</span><span style="COLOR: #800000">*</span><span style="COLOR: #800000">'</span><span style="COLOR: #000000">*</span><span style="COLOR: #000000">10</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #000000">+</span><span style="COLOR: #800000">'''</span><span style="COLOR: #800000"><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;1.Add&nbsp;new&nbsp;address<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;2.Delete&nbsp;an&nbsp;address&nbsp;by&nbsp;name<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;3.Edit&nbsp;an&nbsp;address&nbsp;by&nbsp;name<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;4.Find&nbsp;an&nbsp;address&nbsp;by&nbsp;name&nbsp;&nbsp;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;5.List&nbsp;all&nbsp;record<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;6.Exit</span><span style="COLOR: #800000">'''</span><span style="COLOR: #000000">+</span><span style="COLOR: #000000">\<br>&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #800000">'</span><span style="COLOR: #800000">\n</span><span style="COLOR: #800000">'</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #000000">+</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #800000">'</span><span style="COLOR: #800000">*</span><span style="COLOR: #800000">'</span><span style="COLOR: #000000">*</span><span style="COLOR: #000000">30</span><span style="COLOR: #000000"><br>&nbsp;&nbsp;</span><span style="COLOR: #0000ff">try</span><span style="COLOR: #000000">:<br>&nbsp;&nbsp;&nbsp;&nbsp;choice&nbsp;</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">&nbsp;int(raw_input(</span><span style="COLOR: #800000">'</span><span style="COLOR: #800000">Please&nbsp;choice:</span><span style="COLOR: #800000">'</span><span style="COLOR: #000000">))<br>&nbsp;&nbsp;</span><span style="COLOR: #0000ff">except</span><span style="COLOR: #000000">:<br>&nbsp;&nbsp;&nbsp;&nbsp;os.system(</span><span style="COLOR: #800000">'</span><span style="COLOR: #800000">clear</span><span style="COLOR: #800000">'</span><span style="COLOR: #000000">)<br>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">print</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #800000">'</span><span style="COLOR: #800000">Error:Please&nbsp;enter&nbsp;a&nbsp;number&nbsp;from&nbsp;1&nbsp;to&nbsp;6,enter&nbsp;again.</span><span style="COLOR: #800000">'</span><span style="COLOR: #000000"><br>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">continue</span><span style="COLOR: #000000"><br><br>&nbsp;&nbsp;</span><span style="COLOR: #0000ff">if</span><span style="COLOR: #000000">&nbsp;choice&nbsp;</span><span style="COLOR: #000000">==</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">:<br>&nbsp;&nbsp;&nbsp;&nbsp;name&nbsp;</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">&nbsp;raw_input(</span><span style="COLOR: #800000">'</span><span style="COLOR: #800000">Please&nbsp;input&nbsp;name:</span><span style="COLOR: #800000">'</span><span style="COLOR: #000000">)<br>&nbsp;&nbsp;&nbsp;&nbsp;email&nbsp;</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">&nbsp;raw_input(</span><span style="COLOR: #800000">'</span><span style="COLOR: #800000">Please&nbsp;input&nbsp;email:</span><span style="COLOR: #800000">'</span><span style="COLOR: #000000">)<br>&nbsp;&nbsp;&nbsp;&nbsp;people&nbsp;</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">&nbsp;Person(name,email)<br>&nbsp;&nbsp;&nbsp;&nbsp;ad.Add(people)<br>&nbsp;&nbsp;</span><span style="COLOR: #0000ff">elif</span><span style="COLOR: #000000">&nbsp;choice&nbsp;</span><span style="COLOR: #000000">==</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #000000">2</span><span style="COLOR: #000000">:<br>&nbsp;&nbsp;&nbsp;&nbsp;name&nbsp;</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">&nbsp;raw_input(</span><span style="COLOR: #800000">'</span><span style="COLOR: #800000">Please&nbsp;input&nbsp;name:</span><span style="COLOR: #800000">'</span><span style="COLOR: #000000">)<br>&nbsp;&nbsp;&nbsp;&nbsp;ad.Del(name)<br>&nbsp;&nbsp;</span><span style="COLOR: #0000ff">elif</span><span style="COLOR: #000000">&nbsp;choice&nbsp;</span><span style="COLOR: #000000">==</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #000000">3</span><span style="COLOR: #000000">:<br>&nbsp;&nbsp;&nbsp;&nbsp;name&nbsp;</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">&nbsp;raw_input(</span><span style="COLOR: #800000">'</span><span style="COLOR: #800000">Please&nbsp;input&nbsp;name:</span><span style="COLOR: #800000">'</span><span style="COLOR: #000000">)<br>&nbsp;&nbsp;&nbsp;&nbsp;newemail&nbsp;</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">&nbsp;raw_input(</span><span style="COLOR: #800000">'</span><span style="COLOR: #800000">Please&nbsp;input&nbsp;new&nbsp;email:</span><span style="COLOR: #800000">'</span><span style="COLOR: #000000">)<br>&nbsp;&nbsp;&nbsp;&nbsp;people&nbsp;</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">&nbsp;Person(name,newemail)<br>&nbsp;&nbsp;&nbsp;&nbsp;ad.Edit(name,people)<br>&nbsp;&nbsp;</span><span style="COLOR: #0000ff">elif</span><span style="COLOR: #000000">&nbsp;choice&nbsp;</span><span style="COLOR: #000000">==</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #000000">4</span><span style="COLOR: #000000">:<br>&nbsp;&nbsp;&nbsp;&nbsp;name&nbsp;</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">&nbsp;raw_input(</span><span style="COLOR: #800000">'</span><span style="COLOR: #800000">Please&nbsp;input&nbsp;name:</span><span style="COLOR: #800000">'</span><span style="COLOR: #000000">)<br>&nbsp;&nbsp;&nbsp;&nbsp;ad.Find(name)<br>&nbsp;&nbsp;</span><span style="COLOR: #0000ff">elif</span><span style="COLOR: #000000">&nbsp;choice&nbsp;</span><span style="COLOR: #000000">==</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #000000">5</span><span style="COLOR: #000000">:<br>&nbsp;&nbsp;&nbsp;&nbsp;ad.List()<br>&nbsp;&nbsp;</span><span style="COLOR: #0000ff">elif</span><span style="COLOR: #000000">&nbsp;choice&nbsp;</span><span style="COLOR: #000000">==</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #000000">6</span><span style="COLOR: #000000">:<br>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">break</span><span style="COLOR: #000000"><br>&nbsp;&nbsp;</span><span style="COLOR: #0000ff">else</span><span style="COLOR: #000000">:<br>&nbsp;&nbsp;&nbsp;&nbsp;os.system(</span><span style="COLOR: #800000">'</span><span style="COLOR: #800000">clear</span><span style="COLOR: #800000">'</span><span style="COLOR: #000000">)&nbsp;&nbsp;&nbsp;&nbsp;<br>&nbsp;&nbsp;&nbsp;&nbsp;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span></div>
<br>
<img src ="http://www.cppblog.com/fenglin/aggbug/125516.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/fenglin/" target="_blank">风林</a> 2010-09-01 13:11 <a href="http://www.cppblog.com/fenglin/articles/125516.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>repr函数</title><link>http://www.cppblog.com/fenglin/articles/125483.html</link><dc:creator>风林</dc:creator><author>风林</author><pubDate>Wed, 01 Sep 2010 00:44:00 GMT</pubDate><guid>http://www.cppblog.com/fenglin/articles/125483.html</guid><wfw:comment>http://www.cppblog.com/fenglin/comments/125483.html</wfw:comment><comments>http://www.cppblog.com/fenglin/articles/125483.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/fenglin/comments/commentRss/125483.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/fenglin/services/trackbacks/125483.html</trackback:ping><description><![CDATA[<strong><u><span style="COLOR: red">repr函数用来取得对象的规范字符串表示。</span><span style="COLOR: red">反引号（也称转换符）可以完成相同的功能。</span></u></strong>注<br>意，在大多数时候有eval(repr(object)) == object。<br>&gt;&gt;&gt; i = []<br>&gt;&gt;&gt; i.append('item')<br>&gt;&gt;&gt; `i`<br>"['item']"<br>&gt;&gt;&gt; repr(i)<br>"['item']"<br>&nbsp;<br>基本上，repr函数和反引号用来获取对象的可打印的表示形式。你可以通过定义类的__repr__<br>方法来控制你的对象在被repr函数调用的时候返回的内容。
<img src ="http://www.cppblog.com/fenglin/aggbug/125483.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/fenglin/" target="_blank">风林</a> 2010-09-01 08:44 <a href="http://www.cppblog.com/fenglin/articles/125483.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>assert语句</title><link>http://www.cppblog.com/fenglin/articles/125482.html</link><dc:creator>风林</dc:creator><author>风林</author><pubDate>Wed, 01 Sep 2010 00:39:00 GMT</pubDate><guid>http://www.cppblog.com/fenglin/articles/125482.html</guid><wfw:comment>http://www.cppblog.com/fenglin/comments/125482.html</wfw:comment><comments>http://www.cppblog.com/fenglin/articles/125482.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/fenglin/comments/commentRss/125482.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/fenglin/services/trackbacks/125482.html</trackback:ping><description><![CDATA[<u style="COLOR: red"><strong>assert语句用来声明某个条件是真的。<br></strong></u>例如，如果你非常确信某个你使用的列表中至少有一个<br>元素，而你想要检验这一点，并且在它非真的时候引发一个错误，那么assert语句是应用在这<br>种情形下的理想语句。当assert语句失败的时候，会引发一个AssertionError。<br>&gt;&gt;&gt; mylist = ['item']<br>&gt;&gt;&gt; assert len(mylist) &gt;= 1<br>&gt;&gt;&gt; mylist.pop()<br>'item'<br>&gt;&gt;&gt; assert len(mylist) &gt;= 1<br>Traceback (most recent call last):<br>&nbsp; File "&lt;stdin&gt;", line 1, in ?<br>AssertionError 
<img src ="http://www.cppblog.com/fenglin/aggbug/125482.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/fenglin/" target="_blank">风林</a> 2010-09-01 08:39 <a href="http://www.cppblog.com/fenglin/articles/125482.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>exec和eval语句</title><link>http://www.cppblog.com/fenglin/articles/125481.html</link><dc:creator>风林</dc:creator><author>风林</author><pubDate>Wed, 01 Sep 2010 00:36:00 GMT</pubDate><guid>http://www.cppblog.com/fenglin/articles/125481.html</guid><wfw:comment>http://www.cppblog.com/fenglin/comments/125481.html</wfw:comment><comments>http://www.cppblog.com/fenglin/articles/125481.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/fenglin/comments/commentRss/125481.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/fenglin/services/trackbacks/125481.html</trackback:ping><description><![CDATA[<strong><u style="COLOR: red">exec语句用来执行储存在字符串或文件中的Python语句。</u></strong>例如，我们可以在运行时生成一个包<br>含Python代码的字符串，然后使用exec语句执行这些语句。下面是一个简单的例子。<br>&gt;&gt;&gt; exec 'print "Hello World"'<br>Hello World <br><br><span style="COLOR: red"><u><strong>eval语句用来计算存储在字符串中的有效Python表达式。</strong></u></span>下面是一个简单的例子。<br>&gt;&gt;&gt; eval('2*3')
<img src ="http://www.cppblog.com/fenglin/aggbug/125481.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/fenglin/" target="_blank">风林</a> 2010-09-01 08:36 <a href="http://www.cppblog.com/fenglin/articles/125481.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>lambda形式</title><link>http://www.cppblog.com/fenglin/articles/125480.html</link><dc:creator>风林</dc:creator><author>风林</author><pubDate>Wed, 01 Sep 2010 00:33:00 GMT</pubDate><guid>http://www.cppblog.com/fenglin/articles/125480.html</guid><wfw:comment>http://www.cppblog.com/fenglin/comments/125480.html</wfw:comment><comments>http://www.cppblog.com/fenglin/articles/125480.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/fenglin/comments/commentRss/125480.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/fenglin/services/trackbacks/125480.html</trackback:ping><description><![CDATA[lambda语句被用来创建新的函数对象，并且在运行时返回它们。<br>
<div style="BORDER-BOTTOM: #cccccc 1px solid; BORDER-LEFT: #cccccc 1px solid; PADDING-BOTTOM: 4px; BACKGROUND-COLOR: #eeeeee; PADDING-LEFT: 4px; WIDTH: 98%; PADDING-RIGHT: 5px; FONT-SIZE: 13px; WORD-BREAK: break-all; BORDER-TOP: #cccccc 1px solid; BORDER-RIGHT: #cccccc 1px solid; PADDING-TOP: 4px"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif"><span style="COLOR: #008000">#</span><span style="COLOR: #008000">!/usr/bin/python</span><span style="COLOR: #008000"><br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif">#</span><span style="COLOR: #008000">&nbsp;Filename:&nbsp;lambda.py</span><span style="COLOR: #008000"><br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif"></span><span style="COLOR: #0000ff">def</span><span style="COLOR: #000000">&nbsp;make_repeater(n):<br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">return</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #0000ff">lambda</span><span style="COLOR: #000000">&nbsp;s:&nbsp;s</span><span style="COLOR: #000000">*</span><span style="COLOR: #000000">n<br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif">twice&nbsp;</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">&nbsp;make_repeater(</span><span style="COLOR: #000000">2</span><span style="COLOR: #000000">)<br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif"></span><span style="COLOR: #0000ff">print</span><span style="COLOR: #000000">&nbsp;twice(</span><span style="COLOR: #800000">'</span><span style="COLOR: #800000">word</span><span style="COLOR: #800000">'</span><span style="COLOR: #000000">)<br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif"></span><span style="COLOR: #0000ff">print</span><span style="COLOR: #000000">&nbsp;twice(</span><span style="COLOR: #000000">5</span><span style="COLOR: #000000">)&nbsp;</span></div>
<strong>输出<br></strong>$ python lambda.py<br>wordword<br>10<br>&nbsp;<br><strong>它如何工作</strong><br>这里，我们使用了make_repeater函数在运行时创建新的函数对象，并且返回它。lambda语句用<br>来创建函数对象。本质上，lambda需要一个参数，后面仅跟单个表达式作为函数体，而表达式<br>的值被这个新建的函数返回。<span style="COLOR: red">注意，即便是print语句也不能用在lambda形式中，只能使用表达<br>式。</span>
<img src ="http://www.cppblog.com/fenglin/aggbug/125480.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/fenglin/" target="_blank">风林</a> 2010-09-01 08:33 <a href="http://www.cppblog.com/fenglin/articles/125480.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>在函数中接收元组和列表</title><link>http://www.cppblog.com/fenglin/articles/125479.html</link><dc:creator>风林</dc:creator><author>风林</author><pubDate>Wed, 01 Sep 2010 00:28:00 GMT</pubDate><guid>http://www.cppblog.com/fenglin/articles/125479.html</guid><wfw:comment>http://www.cppblog.com/fenglin/comments/125479.html</wfw:comment><comments>http://www.cppblog.com/fenglin/articles/125479.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/fenglin/comments/commentRss/125479.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/fenglin/services/trackbacks/125479.html</trackback:ping><description><![CDATA[当要使函数接收元组或字典形式的参数的时候，有一种特殊的方法，它分别<span style="COLOR: red">使用*和**前缀</span>。<br>这种方法在函数需要获取可变数量的参数的时候特别有用。<br>&gt;&gt;&gt; def powersum(power, *args):<br>...&nbsp;&nbsp;&nbsp;&nbsp; '''Return the sum of each argument raised to specified power.'''<br>...&nbsp;&nbsp;&nbsp;&nbsp; total = 0<br>...&nbsp;&nbsp;&nbsp;&nbsp; for i in args:<br>...&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; total += pow(i, power)<br>...&nbsp;&nbsp;&nbsp;&nbsp; return total<br>...<br>&gt;&gt;&gt; powersum(2, 3, 4)<br>25<br>&gt;&gt;&gt; powersum(2, 10)<br>100 <br><span style="COLOR: red">由于在args变量前有*前缀，所有多余的函数参数都会作为一个元组存储在args中。如果使用的<br>是**前缀，多余的参数则会被认为是一个字典的键/值对。</span>
<img src ="http://www.cppblog.com/fenglin/aggbug/125479.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/fenglin/" target="_blank">风林</a> 2010-09-01 08:28 <a href="http://www.cppblog.com/fenglin/articles/125479.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>使用列表综合</title><link>http://www.cppblog.com/fenglin/articles/125478.html</link><dc:creator>风林</dc:creator><author>风林</author><pubDate>Wed, 01 Sep 2010 00:24:00 GMT</pubDate><guid>http://www.cppblog.com/fenglin/articles/125478.html</guid><wfw:comment>http://www.cppblog.com/fenglin/comments/125478.html</wfw:comment><comments>http://www.cppblog.com/fenglin/articles/125478.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/fenglin/comments/commentRss/125478.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/fenglin/services/trackbacks/125478.html</trackback:ping><description><![CDATA[<div style="BORDER-BOTTOM: #cccccc 1px solid; BORDER-LEFT: #cccccc 1px solid; PADDING-BOTTOM: 4px; BACKGROUND-COLOR: #eeeeee; PADDING-LEFT: 4px; WIDTH: 98%; PADDING-RIGHT: 5px; FONT-SIZE: 13px; WORD-BREAK: break-all; BORDER-TOP: #cccccc 1px solid; BORDER-RIGHT: #cccccc 1px solid; PADDING-TOP: 4px"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif"><span style="COLOR: #008000">#</span><span style="COLOR: #008000">!/usr/bin/python</span><span style="COLOR: #008000"><br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif">#</span><span style="COLOR: #008000">&nbsp;Filename:&nbsp;list_comprehension.py</span><span style="COLOR: #008000"><br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif"></span><span style="COLOR: #000000">listone&nbsp;</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">&nbsp;[</span><span style="COLOR: #000000">2</span><span style="COLOR: #000000">,&nbsp;</span><span style="COLOR: #000000">3</span><span style="COLOR: #000000">,&nbsp;</span><span style="COLOR: #000000">4</span><span style="COLOR: #000000">]<br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif">listtwo&nbsp;</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">&nbsp;[</span><span style="COLOR: #000000">2</span><span style="COLOR: #000000">*</span><span style="COLOR: #000000">i&nbsp;</span><span style="COLOR: #0000ff">for</span><span style="COLOR: #000000">&nbsp;i&nbsp;</span><span style="COLOR: #0000ff">in</span><span style="COLOR: #000000">&nbsp;listone&nbsp;</span><span style="COLOR: #0000ff">if</span><span style="COLOR: #000000">&nbsp;i&nbsp;</span><span style="COLOR: #000000">&gt;</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #000000">2</span><span style="COLOR: #000000">]<br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif"></span><span style="COLOR: #0000ff">print</span><span style="COLOR: #000000">&nbsp;listtwo&nbsp;<br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif"></span></div>
<strong>输出</strong><br>$ python list_comprehension.py<br>[6, 8]<br><br><strong>它如何工作</strong><br>这里我们为满足条件（if i &gt; 2）的数指定了一个操作（2*i），从而导出一个新的列表。注意原<br>来的列表并没有发生变化。在很多时候，我们都是使用循环来处理列表中的每一个元素，而使<br>用列表综合可以用一种更加精确、简洁、清楚的方法完成相同的工作。
<img src ="http://www.cppblog.com/fenglin/aggbug/125478.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/fenglin/" target="_blank">风林</a> 2010-09-01 08:24 <a href="http://www.cppblog.com/fenglin/articles/125478.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>vim快捷键总图</title><link>http://www.cppblog.com/fenglin/articles/125433.html</link><dc:creator>风林</dc:creator><author>风林</author><pubDate>Tue, 31 Aug 2010 10:21:00 GMT</pubDate><guid>http://www.cppblog.com/fenglin/articles/125433.html</guid><wfw:comment>http://www.cppblog.com/fenglin/comments/125433.html</wfw:comment><comments>http://www.cppblog.com/fenglin/articles/125433.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/fenglin/comments/commentRss/125433.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/fenglin/services/trackbacks/125433.html</trackback:ping><description><![CDATA[<img border=0 src="http://www.cppblog.com/images/cppblog_com/fenglin/vivim_vim.png">
<img src ="http://www.cppblog.com/fenglin/aggbug/125433.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/fenglin/" target="_blank">风林</a> 2010-08-31 18:21 <a href="http://www.cppblog.com/fenglin/articles/125433.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>特殊的方法</title><link>http://www.cppblog.com/fenglin/articles/125431.html</link><dc:creator>风林</dc:creator><author>风林</author><pubDate>Tue, 31 Aug 2010 10:09:00 GMT</pubDate><guid>http://www.cppblog.com/fenglin/articles/125431.html</guid><wfw:comment>http://www.cppblog.com/fenglin/comments/125431.html</wfw:comment><comments>http://www.cppblog.com/fenglin/articles/125431.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/fenglin/comments/commentRss/125431.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/fenglin/services/trackbacks/125431.html</trackback:ping><description><![CDATA[表15.1 一些特殊的方法<br>名称 说明<br>__init__(self,...) 这个方法在新建对象恰好要被返回使用之前被调用。<br>__del__(self) 恰好在对象要被删除之前调用。<br>__str__(self) 在我们对对象使用print语句或是使用str()的时候调用。<br>__lt__(self,other)当使用 小于 运算符（&lt;）的时候调用。类似地，对于所有的运算符<br>（+，&gt;等等）都有特殊的方法。<br>__getitem__(self,key)使用x[key]索引操作符的时候调用。<br>__len__(self) 对序列对象使用内建的len()函数的时候调用。
<img src ="http://www.cppblog.com/fenglin/aggbug/125431.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/fenglin/" target="_blank">风林</a> 2010-08-31 18:09 <a href="http://www.cppblog.com/fenglin/articles/125431.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>sys模块</title><link>http://www.cppblog.com/fenglin/articles/125429.html</link><dc:creator>风林</dc:creator><author>风林</author><pubDate>Tue, 31 Aug 2010 10:06:00 GMT</pubDate><guid>http://www.cppblog.com/fenglin/articles/125429.html</guid><wfw:comment>http://www.cppblog.com/fenglin/comments/125429.html</wfw:comment><comments>http://www.cppblog.com/fenglin/articles/125429.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/fenglin/comments/commentRss/125429.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/fenglin/services/trackbacks/125429.html</trackback:ping><description><![CDATA[<div style="BORDER-BOTTOM: #cccccc 1px solid; BORDER-LEFT: #cccccc 1px solid; PADDING-BOTTOM: 4px; BACKGROUND-COLOR: #eeeeee; PADDING-LEFT: 4px; WIDTH: 98%; PADDING-RIGHT: 5px; FONT-SIZE: 13px; WORD-BREAK: break-all; BORDER-TOP: #cccccc 1px solid; BORDER-RIGHT: #cccccc 1px solid; PADDING-TOP: 4px"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif"><span style="COLOR: #008000">#</span><span style="COLOR: #008000">!/usr/bin/python</span><span style="COLOR: #008000"><br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif">#</span><span style="COLOR: #008000">&nbsp;Filename:&nbsp;cat.py</span><span style="COLOR: #008000"><br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif"></span><span style="COLOR: #0000ff">import</span><span style="COLOR: #000000">&nbsp;sys<br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif"></span><span style="COLOR: #0000ff">def</span><span style="COLOR: #000000">&nbsp;readfile(filename):<br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #800000">'''</span><span style="COLOR: #800000">Print&nbsp;a&nbsp;file&nbsp;to&nbsp;the&nbsp;standard&nbsp;output.</span><span style="COLOR: #800000">'''</span><span style="COLOR: #000000"><br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif">&nbsp;&nbsp;&nbsp;&nbsp;f&nbsp;</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">&nbsp;file(filename)<br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">while</span><span style="COLOR: #000000">&nbsp;True:<br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;line&nbsp;</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">&nbsp;f.readline()<br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">if</span><span style="COLOR: #000000">&nbsp;len(line)&nbsp;</span><span style="COLOR: #000000">==</span><span style="COLOR: #000000">&nbsp;0:<br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">break</span><span style="COLOR: #000000"><br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">print</span><span style="COLOR: #000000">&nbsp;line,&nbsp;</span><span style="COLOR: #008000">#</span><span style="COLOR: #008000">&nbsp;notice&nbsp;comma</span><span style="COLOR: #008000"><br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif"></span><span style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;f.close()<br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif"></span><span style="COLOR: #008000">#</span><span style="COLOR: #008000">&nbsp;Script&nbsp;starts&nbsp;from&nbsp;here</span><span style="COLOR: #008000"><br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif"></span><span style="COLOR: #0000ff">if</span><span style="COLOR: #000000">&nbsp;len(sys.argv)&nbsp;</span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #000000">2</span><span style="COLOR: #000000">:<br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">print</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #800000">'</span><span style="COLOR: #800000">No&nbsp;action&nbsp;specified.</span><span style="COLOR: #800000">'</span><span style="COLOR: #000000"><br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif">&nbsp;&nbsp;&nbsp;&nbsp;sys.exit()<br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif"></span><span style="COLOR: #0000ff">if</span><span style="COLOR: #000000">&nbsp;sys.argv[</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">].startswith(</span><span style="COLOR: #800000">'</span><span style="COLOR: #800000">--</span><span style="COLOR: #800000">'</span><span style="COLOR: #000000">):<br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif">&nbsp;&nbsp;&nbsp;&nbsp;option&nbsp;</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">&nbsp;sys.argv[</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">][</span><span style="COLOR: #000000">2</span><span style="COLOR: #000000">:]<br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #008000">#</span><span style="COLOR: #008000">&nbsp;fetch&nbsp;sys.argv[1]&nbsp;but&nbsp;without&nbsp;the&nbsp;first&nbsp;two&nbsp;characters</span><span style="COLOR: #008000"><br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif"></span><span style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">if</span><span style="COLOR: #000000">&nbsp;option&nbsp;</span><span style="COLOR: #000000">==</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #800000">'</span><span style="COLOR: #800000">version</span><span style="COLOR: #800000">'</span><span style="COLOR: #000000">:<br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">print</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #800000">'</span><span style="COLOR: #800000">Version&nbsp;1.2</span><span style="COLOR: #800000">'</span><span style="COLOR: #000000"><br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">elif</span><span style="COLOR: #000000">&nbsp;option&nbsp;</span><span style="COLOR: #000000">==</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #800000">'</span><span style="COLOR: #800000">help</span><span style="COLOR: #800000">'</span><span style="COLOR: #000000">:<br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">print</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #800000">'''</span><span style="COLOR: #800000">\<br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif">This&nbsp;program&nbsp;prints&nbsp;files&nbsp;to&nbsp;the&nbsp;standard&nbsp;output.<br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif">Any&nbsp;number&nbsp;of&nbsp;files&nbsp;can&nbsp;be&nbsp;specified.<br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif">Options&nbsp;include:<br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif">&nbsp;&nbsp;--version&nbsp;:&nbsp;Prints&nbsp;the&nbsp;version&nbsp;number<br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif">&nbsp;&nbsp;--help&nbsp;&nbsp;&nbsp;&nbsp;:&nbsp;Display&nbsp;this&nbsp;help</span><span style="COLOR: #800000">'''</span><span style="COLOR: #000000"><br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">else</span><span style="COLOR: #000000">:<br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">print</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #800000">'</span><span style="COLOR: #800000">Unknown&nbsp;option.</span><span style="COLOR: #800000">'</span><span style="COLOR: #000000"><br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif">&nbsp;&nbsp;&nbsp;&nbsp;sys.exit()<br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif"></span><span style="COLOR: #0000ff">else</span><span style="COLOR: #000000">:<br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">for</span><span style="COLOR: #000000">&nbsp;filename&nbsp;</span><span style="COLOR: #0000ff">in</span><span style="COLOR: #000000">&nbsp;sys.argv[</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">:]:<br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;readfile(filename)</span></div>
<strong>输出</strong><br>$ python cat.py<br>No action specified.<br>$ python cat.py --help<br>This program prints files to the standard output.<br>Any number of files can be specified.<br>Options include:<br>&nbsp; --version : Prints the version number<br>&nbsp; --help&nbsp;&nbsp;&nbsp; : Display this help<br>$ python cat.py --version<br>Version 1.2<br>$ python cat.py --nonsense<br>Unknown option.<br>$ python cat.py poem.txt<br>Programming is fun<br>When the work is done<br>if you wanna make your work also fun:<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; use Python!
<img src ="http://www.cppblog.com/fenglin/aggbug/125429.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/fenglin/" target="_blank">风林</a> 2010-08-31 18:06 <a href="http://www.cppblog.com/fenglin/articles/125429.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>os模块</title><link>http://www.cppblog.com/fenglin/articles/125428.html</link><dc:creator>风林</dc:creator><author>风林</author><pubDate>Tue, 31 Aug 2010 10:04:00 GMT</pubDate><guid>http://www.cppblog.com/fenglin/articles/125428.html</guid><wfw:comment>http://www.cppblog.com/fenglin/comments/125428.html</wfw:comment><comments>http://www.cppblog.com/fenglin/articles/125428.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/fenglin/comments/commentRss/125428.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/fenglin/services/trackbacks/125428.html</trackback:ping><description><![CDATA[这个模块包含普遍的操作系统功能。如果你希望你的程序能够与平台无关的话，这个模块是尤<br>为重要的。即它允许一个程序在编写后不需要任何改动，也不会发生任何问题，就可以在<br>Linux和Windows下运行。一个例子就是使用os.sep可以取代操作系统特定的路径分割符。<br>下面列出了一些在os模块中比较有用的部分。它们中的大多数都简单明了。<br>●&nbsp;&nbsp;&nbsp;&nbsp; os.name字符串指示你正在使用的平台。比如对于Windows，它是'nt'，而对于Linux/Unix<br>用户，它是'posix'。<br>●&nbsp;&nbsp;&nbsp;&nbsp; os.getcwd()函数得到当前工作目录，即当前Python脚本工作的目录路径。<br>●&nbsp;&nbsp;&nbsp;&nbsp; os.getenv()和os.putenv()函数分别用来读取和设置环境变量。<br>●&nbsp;&nbsp;&nbsp;&nbsp; os.listdir()返回指定目录下的所有文件和目录名。<br>●&nbsp;&nbsp;&nbsp;&nbsp; os.remove()函数用来删除一个文件。<br>●&nbsp;&nbsp;&nbsp;&nbsp; os.system()函数用来运行shell命令。<br>●&nbsp;&nbsp;&nbsp;&nbsp; os.linesep字符串给出当前平台使用的行终止符。例如，Windows使用'\r\n'，Linux使<br>用'\n'而Mac使用'\r'。<br>●&nbsp;&nbsp;&nbsp;&nbsp; os.path.split()函数返回一个路径的目录名和文件名。<br>&gt;&gt;&gt; os.path.split('/home/swaroop/byte/code/poem.txt')<br>('/home/swaroop/byte/code', 'poem.txt') <br>●&nbsp;&nbsp;&nbsp;&nbsp; os.path.isfile()和os.path.isdir()函数分别检验给出的路径是一个文件还是目录。类似地，os.<br>path.exists()函数用来检验给出的路径是否真地存在。
<img src ="http://www.cppblog.com/fenglin/aggbug/125428.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/fenglin/" target="_blank">风林</a> 2010-08-31 18:04 <a href="http://www.cppblog.com/fenglin/articles/125428.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>try..finally</title><link>http://www.cppblog.com/fenglin/articles/125423.html</link><dc:creator>风林</dc:creator><author>风林</author><pubDate>Tue, 31 Aug 2010 09:19:00 GMT</pubDate><guid>http://www.cppblog.com/fenglin/articles/125423.html</guid><wfw:comment>http://www.cppblog.com/fenglin/comments/125423.html</wfw:comment><comments>http://www.cppblog.com/fenglin/articles/125423.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/fenglin/comments/commentRss/125423.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/fenglin/services/trackbacks/125423.html</trackback:ping><description><![CDATA[假如你在读一个文件的时候，希望在无论异常发生与否的情况下都关闭文件，该怎么做呢？这<br>可以使用finally块来完成。注意，在一个try块下，你可以同时使用except从句和finally块。如果<br>你要同时使用它们的话，需要把一个嵌入另外一个。<br>
<div style="BORDER-BOTTOM: #cccccc 1px solid; BORDER-LEFT: #cccccc 1px solid; PADDING-BOTTOM: 4px; BACKGROUND-COLOR: #eeeeee; PADDING-LEFT: 4px; WIDTH: 98%; PADDING-RIGHT: 5px; FONT-SIZE: 13px; WORD-BREAK: break-all; BORDER-TOP: #cccccc 1px solid; BORDER-RIGHT: #cccccc 1px solid; PADDING-TOP: 4px"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif"><span style="COLOR: #008000">#</span><span style="COLOR: #008000">!/usr/bin/python</span><span style="COLOR: #008000"><br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif">#</span><span style="COLOR: #008000">&nbsp;Filename:&nbsp;finally.py</span><span style="COLOR: #008000"><br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif"></span><span style="COLOR: #0000ff">import</span><span style="COLOR: #000000">&nbsp;time<br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif"></span><span style="COLOR: #0000ff">try</span><span style="COLOR: #000000">:<br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif">&nbsp;&nbsp;&nbsp;&nbsp;f&nbsp;</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">&nbsp;file(</span><span style="COLOR: #800000">'</span><span style="COLOR: #800000">poem.txt</span><span style="COLOR: #800000">'</span><span style="COLOR: #000000">)<br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">while</span><span style="COLOR: #000000">&nbsp;True:&nbsp;</span><span style="COLOR: #008000">#</span><span style="COLOR: #008000">&nbsp;our&nbsp;usual&nbsp;file-reading&nbsp;idiom</span><span style="COLOR: #008000"><br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif"></span><span style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;line&nbsp;</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">&nbsp;f.readline()<br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">if</span><span style="COLOR: #000000">&nbsp;len(line)&nbsp;</span><span style="COLOR: #000000">==</span><span style="COLOR: #000000">&nbsp;0:<br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">break</span><span style="COLOR: #000000"><br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;time.sleep(</span><span style="COLOR: #000000">2</span><span style="COLOR: #000000">)<br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">print</span><span style="COLOR: #000000">&nbsp;line,<br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif"></span><span style="COLOR: #0000ff">finally</span><span style="COLOR: #000000">:<br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif">&nbsp;&nbsp;&nbsp;&nbsp;f.close()<br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">print</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #800000">'</span><span style="COLOR: #800000">Cleaning&nbsp;up<img src="http://www.cppblog.com/Images/dot.gif">closed&nbsp;the&nbsp;file</span><span style="COLOR: #800000">'</span><span style="COLOR: #000000">&nbsp;</span></div>
<strong>输出</strong><br>$ python finally.py<br>Programming is fun<br>When the work is done<br>Cleaning up...closed the file<br>Traceback (most recent call last):<br>&nbsp; File "finally.py", line 12, in ?<br>&nbsp;&nbsp;&nbsp; time.sleep(2)<br>KeyboardInterrupt <br><br><strong>它如何工作</strong><br>我们进行通常的读文件工作，但是我有意在每打印一行之前用time.sleep方法暂停2秒钟。这样<br>做的原因是让程序运行得慢一些（Python由于其本质通常运行得很快）。在程序运行的时候，<br>按Ctrl-c中断/取消程序。<br>我们可以观察到KeyboardInterrupt异常被触发，程序退出。但是在程序退出之前，finally从句仍<br>然被执行，把文件关闭
<img src ="http://www.cppblog.com/fenglin/aggbug/125423.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/fenglin/" target="_blank">风林</a> 2010-08-31 17:19 <a href="http://www.cppblog.com/fenglin/articles/125423.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>raise 引发异常</title><link>http://www.cppblog.com/fenglin/articles/125421.html</link><dc:creator>风林</dc:creator><author>风林</author><pubDate>Tue, 31 Aug 2010 09:12:00 GMT</pubDate><guid>http://www.cppblog.com/fenglin/articles/125421.html</guid><wfw:comment>http://www.cppblog.com/fenglin/comments/125421.html</wfw:comment><comments>http://www.cppblog.com/fenglin/articles/125421.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/fenglin/comments/commentRss/125421.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/fenglin/services/trackbacks/125421.html</trackback:ping><description><![CDATA[你可以使用raise语句 引发 异常。你还得指明错误/异常的名称和伴随异常 触发的 异常对象。你<br>可以引发的错误或异常应该分别是一个Error或Exception类的直接或间接导出类。<br>
<div style="BORDER-BOTTOM: #cccccc 1px solid; BORDER-LEFT: #cccccc 1px solid; PADDING-BOTTOM: 4px; BACKGROUND-COLOR: #eeeeee; PADDING-LEFT: 4px; WIDTH: 98%; PADDING-RIGHT: 5px; FONT-SIZE: 13px; WORD-BREAK: break-all; BORDER-TOP: #cccccc 1px solid; BORDER-RIGHT: #cccccc 1px solid; PADDING-TOP: 4px"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif"><span style="COLOR: #008000">#</span><span style="COLOR: #008000">!/usr/bin/python</span><span style="COLOR: #008000"><br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif">#</span><span style="COLOR: #008000">&nbsp;Filename:&nbsp;raising.py</span><span style="COLOR: #008000"><br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif"></span><span style="COLOR: #0000ff">class</span><span style="COLOR: #000000">&nbsp;ShortInputException(Exception):<br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #800000">'''</span><span style="COLOR: #800000">A&nbsp;user-defined&nbsp;exception&nbsp;class.</span><span style="COLOR: #800000">'''</span><span style="COLOR: #000000"><br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">def</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #800080">__init__</span><span style="COLOR: #000000">(self,&nbsp;length,&nbsp;atleast):<br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Exception.</span><span style="COLOR: #800080">__init__</span><span style="COLOR: #000000">(self)<br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;self.length&nbsp;</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">&nbsp;length<br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;self.atleast&nbsp;</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">&nbsp;atleast<br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif"></span><span style="COLOR: #0000ff">try</span><span style="COLOR: #000000">:<br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif">&nbsp;&nbsp;&nbsp;&nbsp;s&nbsp;</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">&nbsp;raw_input(</span><span style="COLOR: #800000">'</span><span style="COLOR: #800000">Enter&nbsp;something&nbsp;--&gt;&nbsp;</span><span style="COLOR: #800000">'</span><span style="COLOR: #000000">)<br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">if</span><span style="COLOR: #000000">&nbsp;len(s)&nbsp;</span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #000000">3</span><span style="COLOR: #000000">:<br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">raise</span><span style="COLOR: #000000">&nbsp;ShortInputException(len(s),&nbsp;</span><span style="COLOR: #000000">3</span><span style="COLOR: #000000">)<br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #008000">#</span><span style="COLOR: #008000">&nbsp;Other&nbsp;work&nbsp;can&nbsp;continue&nbsp;as&nbsp;usual&nbsp;here</span><span style="COLOR: #008000"><br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif"></span><span style="COLOR: #0000ff">except</span><span style="COLOR: #000000">&nbsp;EOFError:<br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">print</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #800000">'</span><span style="COLOR: #800000">\nWhy&nbsp;did&nbsp;you&nbsp;do&nbsp;an&nbsp;EOF&nbsp;on&nbsp;me?</span><span style="COLOR: #800000">'</span><span style="COLOR: #000000"><br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif"></span><span style="COLOR: #0000ff">except</span><span style="COLOR: #000000">&nbsp;ShortInputException,&nbsp;x:<br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">print</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #800000">'</span><span style="COLOR: #800000">ShortInputException:&nbsp;The&nbsp;input&nbsp;was&nbsp;of&nbsp;length&nbsp;%d,&nbsp;\</span><span style="COLOR: #800000"><br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif"></span><span style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;was&nbsp;expecting&nbsp;at&nbsp;least&nbsp;</span><span style="COLOR: #000000">%</span><span style="COLOR: #000000">d</span><span style="COLOR: #800000">'</span><span style="COLOR: #800000">&nbsp;%&nbsp;(x.length,&nbsp;x.atleast)</span><span style="COLOR: #800000"><br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif"></span><span style="COLOR: #0000ff">else</span><span style="COLOR: #000000">:<br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">print</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #800000">'</span><span style="COLOR: #800000">No&nbsp;exception&nbsp;was&nbsp;raised.</span><span style="COLOR: #800000">'</span><span style="COLOR: #000000">&nbsp;</span></div>
<strong>输出<br></strong>$ python raising.py<br>Enter something --&gt;<br>Why did you do an EOF on me?<br>$ python raising.py<br>Enter something --&gt; ab<br>ShortInputException: The input was of length 2, was expecting at least 3<br>$ python raising.py<br>Enter something --&gt; abc<br>No exception was raised. <br><br><strong>它如何工作</strong><br>这里，我们创建了我们自己的异常类型，其实我们可以使用任何预定义的异常/错误。这个新<br>的异常类型是ShortInputException类。它有两个域——length是给定输入的长度，atleast则是程序<br>期望的最小长度。<br>在except从句中，我们提供了错误类和用来表示错误/异常对象的变量。这与函数调用中的形参<br>和实参概念类似。在这个特别的except从句中，我们使用异常对象的length和atleast域来为用户<br>打印一个恰当的消息。
<img src ="http://www.cppblog.com/fenglin/aggbug/125421.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/fenglin/" target="_blank">风林</a> 2010-08-31 17:12 <a href="http://www.cppblog.com/fenglin/articles/125421.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>处理异常</title><link>http://www.cppblog.com/fenglin/articles/125420.html</link><dc:creator>风林</dc:creator><author>风林</author><pubDate>Tue, 31 Aug 2010 09:03:00 GMT</pubDate><guid>http://www.cppblog.com/fenglin/articles/125420.html</guid><wfw:comment>http://www.cppblog.com/fenglin/comments/125420.html</wfw:comment><comments>http://www.cppblog.com/fenglin/articles/125420.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/fenglin/comments/commentRss/125420.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/fenglin/services/trackbacks/125420.html</trackback:ping><description><![CDATA[我们可以使用try..except语句来处理异常。我们把通常的语句放在try-块中，而把我们的错误处<br>理语句放在except-块中。<br>
<div style="BORDER-BOTTOM: #cccccc 1px solid; BORDER-LEFT: #cccccc 1px solid; PADDING-BOTTOM: 4px; BACKGROUND-COLOR: #eeeeee; PADDING-LEFT: 4px; WIDTH: 98%; PADDING-RIGHT: 5px; FONT-SIZE: 13px; WORD-BREAK: break-all; BORDER-TOP: #cccccc 1px solid; BORDER-RIGHT: #cccccc 1px solid; PADDING-TOP: 4px"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif"><span style="COLOR: #008000">#</span><span style="COLOR: #008000">!/usr/bin/python</span><span style="COLOR: #008000"><br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif">#</span><span style="COLOR: #008000">&nbsp;Filename:&nbsp;try_except.py</span><span style="COLOR: #008000"><br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif"></span><span style="COLOR: #0000ff">import</span><span style="COLOR: #000000">&nbsp;sys<br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif"></span><span style="COLOR: #0000ff">try</span><span style="COLOR: #000000">:<br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif">&nbsp;&nbsp;&nbsp;&nbsp;s&nbsp;</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">&nbsp;raw_input(</span><span style="COLOR: #800000">'</span><span style="COLOR: #800000">Enter&nbsp;something&nbsp;--&gt;&nbsp;</span><span style="COLOR: #800000">'</span><span style="COLOR: #000000">)<br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif"></span><span style="COLOR: #0000ff">except</span><span style="COLOR: #000000">&nbsp;EOFError:<br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">print</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #800000">'</span><span style="COLOR: #800000">\nWhy&nbsp;did&nbsp;you&nbsp;do&nbsp;an&nbsp;EOF&nbsp;on&nbsp;me?</span><span style="COLOR: #800000">'</span><span style="COLOR: #000000"><br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif">&nbsp;&nbsp;&nbsp;&nbsp;sys.exit()&nbsp;</span><span style="COLOR: #008000">#</span><span style="COLOR: #008000">&nbsp;exit&nbsp;the&nbsp;program</span><span style="COLOR: #008000"><br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif"></span><span style="COLOR: #0000ff">except</span><span style="COLOR: #000000">:<br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">print</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #800000">'</span><span style="COLOR: #800000">\nSome&nbsp;error/exception&nbsp;occurred.</span><span style="COLOR: #800000">'</span><span style="COLOR: #000000"><br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #008000">#</span><span style="COLOR: #008000">&nbsp;here,&nbsp;we&nbsp;are&nbsp;not&nbsp;exiting&nbsp;the&nbsp;program</span><span style="COLOR: #008000"><br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif"></span><span style="COLOR: #0000ff">print</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #800000">'</span><span style="COLOR: #800000">Done</span><span style="COLOR: #800000">'</span><span style="COLOR: #000000">&nbsp;</span></div>
<strong>输出<br></strong>$ python try_except.py<br>Enter something --&gt;<br>Why did you do an EOF on me?<br>$ python try_except.py<br>Enter something --&gt; Python is exceptional!<br>Done<br><br><strong>它如何工作<br></strong>我们把所有可能引发错误的语句放在try块中，然后在except从句/块中处理所有的错误和异常。<br>except从句可以专门处理单一的错误或异常，或者一组包括在圆括号内的错误/异常。如果没有<br>给出错误或异常的名称，它会处理 所有的 错误和异常。对于每个try从句，至少都有一个相关<br>联的except从句。<br>如果某个错误或异常没有被处理，默认的Python处理器就会被调用。它会终止程序的运行，并<br>且打印一个消息，我们已经看到了这样的处理。<br>
<img src ="http://www.cppblog.com/fenglin/aggbug/125420.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/fenglin/" target="_blank">风林</a> 2010-08-31 17:03 <a href="http://www.cppblog.com/fenglin/articles/125420.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>储存器</title><link>http://www.cppblog.com/fenglin/articles/125418.html</link><dc:creator>风林</dc:creator><author>风林</author><pubDate>Tue, 31 Aug 2010 08:54:00 GMT</pubDate><guid>http://www.cppblog.com/fenglin/articles/125418.html</guid><wfw:comment>http://www.cppblog.com/fenglin/comments/125418.html</wfw:comment><comments>http://www.cppblog.com/fenglin/articles/125418.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/fenglin/comments/commentRss/125418.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/fenglin/services/trackbacks/125418.html</trackback:ping><description><![CDATA[Python提供一个标准的模块，称为pickle。使用它你可以在一个文件中储存任何Python对象，之<br>后你又可以把它完整无缺地取出来。这被称为 持久地 储存对象。<br>还有另一个模块称为<span style="COLOR: red">cPickle</span>，它的功能和pickle模块完全相同，只不过它是用C语言编写的，因<br>此要快得多（<span style="COLOR: red">比pickle快1000倍</span>）。你可以使用它们中的任一个，而我们在这里将使用cPickle模<br>块。记住，<span style="COLOR: red">我们把这两个模块都简称为pickle模块</span>。<br>
<div style="BORDER-BOTTOM: #cccccc 1px solid; BORDER-LEFT: #cccccc 1px solid; PADDING-BOTTOM: 4px; BACKGROUND-COLOR: #eeeeee; PADDING-LEFT: 4px; WIDTH: 98%; PADDING-RIGHT: 5px; FONT-SIZE: 13px; WORD-BREAK: break-all; BORDER-TOP: #cccccc 1px solid; BORDER-RIGHT: #cccccc 1px solid; PADDING-TOP: 4px"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif"><span style="COLOR: #008000">#</span><span style="COLOR: #008000">!/usr/bin/python</span><span style="COLOR: #008000"><br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif">#</span><span style="COLOR: #008000">&nbsp;Filename:&nbsp;pickling.py</span><span style="COLOR: #008000"><br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif"></span><span style="COLOR: #0000ff">import</span><span style="COLOR: #000000">&nbsp;cPickle&nbsp;as&nbsp;p<br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif"></span><span style="COLOR: #008000">#</span><span style="COLOR: #008000">import&nbsp;pickle&nbsp;as&nbsp;p</span><span style="COLOR: #008000"><br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif"></span><span style="COLOR: #000000">shoplistfile&nbsp;</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #800000">'</span><span style="COLOR: #800000">shoplist.data</span><span style="COLOR: #800000">'</span><span style="COLOR: #000000"><br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif"></span><span style="COLOR: #008000">#</span><span style="COLOR: #008000">&nbsp;the&nbsp;name&nbsp;of&nbsp;the&nbsp;file&nbsp;where&nbsp;we&nbsp;will&nbsp;store&nbsp;the&nbsp;object</span><span style="COLOR: #008000"><br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif"></span><span style="COLOR: #000000">shoplist&nbsp;</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">&nbsp;[</span><span style="COLOR: #800000">'</span><span style="COLOR: #800000">apple</span><span style="COLOR: #800000">'</span><span style="COLOR: #000000">,&nbsp;</span><span style="COLOR: #800000">'</span><span style="COLOR: #800000">mango</span><span style="COLOR: #800000">'</span><span style="COLOR: #000000">,&nbsp;</span><span style="COLOR: #800000">'</span><span style="COLOR: #800000">carrot</span><span style="COLOR: #800000">'</span><span style="COLOR: #000000">]<br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif"></span><span style="COLOR: #008000">#</span><span style="COLOR: #008000">&nbsp;Write&nbsp;to&nbsp;the&nbsp;file</span><span style="COLOR: #008000"><br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif"></span><span style="COLOR: #000000">f&nbsp;</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">&nbsp;file(shoplistfile,&nbsp;</span><span style="COLOR: #800000">'</span><span style="COLOR: #800000">w</span><span style="COLOR: #800000">'</span><span style="COLOR: #000000">)<br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif">p.dump(shoplist,&nbsp;f)&nbsp;</span><span style="COLOR: #008000">#</span><span style="COLOR: #008000">&nbsp;dump&nbsp;the&nbsp;object&nbsp;to&nbsp;a&nbsp;file</span><span style="COLOR: #008000"><br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif"></span><span style="COLOR: #000000">f.close()<br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif"></span><span style="COLOR: #0000ff">del</span><span style="COLOR: #000000">&nbsp;shoplist&nbsp;</span><span style="COLOR: #008000">#</span><span style="COLOR: #008000">&nbsp;remove&nbsp;the&nbsp;shoplist</span><span style="COLOR: #008000"><br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif">#</span><span style="COLOR: #008000">&nbsp;Read&nbsp;back&nbsp;from&nbsp;the&nbsp;storage</span><span style="COLOR: #008000"><br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif"></span><span style="COLOR: #000000">f&nbsp;</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">&nbsp;file(shoplistfile)<br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif">storedlist&nbsp;</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">&nbsp;p.load(f)<br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif"></span><span style="COLOR: #0000ff">print</span><span style="COLOR: #000000">&nbsp;storedlist&nbsp;</span></div>
<strong>输出</strong><br>$ python pickling.py<br>['apple', 'mango', 'carrot'] <br><br><strong>它如何工作</strong><br>首先，请注意我们使用了import..as语法。这是一种便利方法，以便于我们可以使用更短的模块<br>名称。在这个例子中，它还让我们能够通过简单地改变一行就切换到另一个模块（cPickle或者<br>pickle）！在程序的其余部分的时候，我们简单地把这个模块称为p。<br>为了在文件里储存一个对象，首先以写模式打开一个file对象，然后调用储存器模块的dump函<br>数，把对象储存到打开的文件中。这个过程称为 储存 。<br>接下来，我们使用pickle模块的load函数的返回来取回对象。这个过程称为 取储存 。
<img src ="http://www.cppblog.com/fenglin/aggbug/125418.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/fenglin/" target="_blank">风林</a> 2010-08-31 16:54 <a href="http://www.cppblog.com/fenglin/articles/125418.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>文件</title><link>http://www.cppblog.com/fenglin/articles/125415.html</link><dc:creator>风林</dc:creator><author>风林</author><pubDate>Tue, 31 Aug 2010 08:33:00 GMT</pubDate><guid>http://www.cppblog.com/fenglin/articles/125415.html</guid><wfw:comment>http://www.cppblog.com/fenglin/comments/125415.html</wfw:comment><comments>http://www.cppblog.com/fenglin/articles/125415.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/fenglin/comments/commentRss/125415.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/fenglin/services/trackbacks/125415.html</trackback:ping><description><![CDATA[你可以通过创建一个file类的对象来打开一个文件，分别使用file类的read、readline或write方法来<br>恰当地读写文件。对文件的读写能力依赖于你在打开文件时指定的模式。最后，当你完成对文<br>件的操作的时候，你调用close方法来告诉Python我们完成了对文件的使用。<br>
<div style="BORDER-BOTTOM: #cccccc 1px solid; BORDER-LEFT: #cccccc 1px solid; PADDING-BOTTOM: 4px; BACKGROUND-COLOR: #eeeeee; PADDING-LEFT: 4px; WIDTH: 98%; PADDING-RIGHT: 5px; FONT-SIZE: 13px; WORD-BREAK: break-all; BORDER-TOP: #cccccc 1px solid; BORDER-RIGHT: #cccccc 1px solid; PADDING-TOP: 4px"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif"><span style="COLOR: #008000">#</span><span style="COLOR: #008000">!/usr/bin/python</span><span style="COLOR: #008000"><br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif">#</span><span style="COLOR: #008000">&nbsp;Filename:&nbsp;using_file.py</span><span style="COLOR: #008000"><br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif"></span><span style="COLOR: #000000">poem&nbsp;</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #800000">'''</span><span style="COLOR: #800000">\<br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif">Programming&nbsp;is&nbsp;fun<br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif">When&nbsp;the&nbsp;work&nbsp;is&nbsp;done<br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif">if&nbsp;you&nbsp;wanna&nbsp;make&nbsp;your&nbsp;work&nbsp;also&nbsp;fun:<br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;use&nbsp;Python!<br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif"></span><span style="COLOR: #800000">'''</span><span style="COLOR: #000000"><br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif">f&nbsp;</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">&nbsp;file(</span><span style="COLOR: #800000">'</span><span style="COLOR: #800000">poem.txt</span><span style="COLOR: #800000">'</span><span style="COLOR: #000000">,&nbsp;</span><span style="COLOR: #800000">'</span><span style="COLOR: #800000">w</span><span style="COLOR: #800000">'</span><span style="COLOR: #000000">)&nbsp;</span><span style="COLOR: #008000">#</span><span style="COLOR: #008000">&nbsp;open&nbsp;for&nbsp;'w'riting</span><span style="COLOR: #008000"><br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif"></span><span style="COLOR: #000000">f.write(poem)&nbsp;</span><span style="COLOR: #008000">#</span><span style="COLOR: #008000">&nbsp;write&nbsp;text&nbsp;to&nbsp;file</span><span style="COLOR: #008000"><br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif"></span><span style="COLOR: #000000">f.close()&nbsp;</span><span style="COLOR: #008000">#</span><span style="COLOR: #008000">&nbsp;close&nbsp;the&nbsp;file</span><span style="COLOR: #008000"><br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif"></span><span style="COLOR: #000000">f&nbsp;</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">&nbsp;file(</span><span style="COLOR: #800000">'</span><span style="COLOR: #800000">poem.txt</span><span style="COLOR: #800000">'</span><span style="COLOR: #000000">)<br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif"></span><span style="COLOR: #008000">#</span><span style="COLOR: #008000">&nbsp;if&nbsp;no&nbsp;mode&nbsp;is&nbsp;specified,&nbsp;'r'ead&nbsp;mode&nbsp;is&nbsp;assumed&nbsp;by&nbsp;default</span><span style="COLOR: #008000"><br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif"></span><span style="COLOR: #0000ff">while</span><span style="COLOR: #000000">&nbsp;True:<br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif">&nbsp;&nbsp;&nbsp;&nbsp;line&nbsp;</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">&nbsp;f.readline()<br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">if</span><span style="COLOR: #000000">&nbsp;len(line)&nbsp;</span><span style="COLOR: #000000">==</span><span style="COLOR: #000000">&nbsp;0:&nbsp;</span><span style="COLOR: #008000">#</span><span style="COLOR: #008000">&nbsp;Zero&nbsp;length&nbsp;indicates&nbsp;EOF</span><span style="COLOR: #008000"><br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif"></span><span style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">break</span><span style="COLOR: #000000"><br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">print</span><span style="COLOR: #000000">&nbsp;line,<br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #008000">#</span><span style="COLOR: #008000">&nbsp;Notice&nbsp;comma&nbsp;to&nbsp;avoid&nbsp;automatic&nbsp;newline&nbsp;added&nbsp;by&nbsp;Python</span><span style="COLOR: #008000"><br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif"></span><span style="COLOR: #000000">f.close()&nbsp;</span><span style="COLOR: #008000">#</span><span style="COLOR: #008000">&nbsp;close&nbsp;the&nbsp;file&nbsp;</span></div>
<strong>输出</strong><br>$ python using_file.py<br>Programming is fun<br>When the work is done<br>if you wanna make your work also fun:<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; use Python! <br><br><strong>它如何工作<br></strong>首先，我们通过指明我们希望打开的文件和模式来创建一个file类的实例。模式可以为读模式<br>（'r'）、写模式（'w'）或追加模式（'a'）。事实上还有多得多的模式可以使用，你可以使用<br>help(file)来了解它们的详情。<br>我们首先用写模式打开文件，然后使用file类的write方法来写文件，最后我们用close关闭这个文<br>件。
<img src ="http://www.cppblog.com/fenglin/aggbug/125415.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/fenglin/" target="_blank">风林</a> 2010-08-31 16:33 <a href="http://www.cppblog.com/fenglin/articles/125415.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>使用继承</title><link>http://www.cppblog.com/fenglin/articles/125414.html</link><dc:creator>风林</dc:creator><author>风林</author><pubDate>Tue, 31 Aug 2010 08:23:00 GMT</pubDate><guid>http://www.cppblog.com/fenglin/articles/125414.html</guid><wfw:comment>http://www.cppblog.com/fenglin/comments/125414.html</wfw:comment><comments>http://www.cppblog.com/fenglin/articles/125414.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/fenglin/comments/commentRss/125414.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/fenglin/services/trackbacks/125414.html</trackback:ping><description><![CDATA[<div style="BORDER-BOTTOM: #cccccc 1px solid; BORDER-LEFT: #cccccc 1px solid; PADDING-BOTTOM: 4px; BACKGROUND-COLOR: #eeeeee; PADDING-LEFT: 4px; WIDTH: 98%; PADDING-RIGHT: 5px; FONT-SIZE: 13px; WORD-BREAK: break-all; BORDER-TOP: #cccccc 1px solid; BORDER-RIGHT: #cccccc 1px solid; PADDING-TOP: 4px"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif"><span style="COLOR: #008000">#</span><span style="COLOR: #008000">!/usr/bin/python</span><span style="COLOR: #008000"><br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif">#</span><span style="COLOR: #008000">&nbsp;Filename:&nbsp;inherit.py</span><span style="COLOR: #008000"><br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif"></span><span style="COLOR: #0000ff">class</span><span style="COLOR: #000000">&nbsp;SchoolMember:<br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #800000">'''</span><span style="COLOR: #800000">Represents&nbsp;any&nbsp;school&nbsp;member.</span><span style="COLOR: #800000">'''</span><span style="COLOR: #000000"><br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">def</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #800080">__init__</span><span style="COLOR: #000000">(self,&nbsp;name,&nbsp;age):<br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;self.name&nbsp;</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">&nbsp;name<br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;self.age&nbsp;</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">&nbsp;age<br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">print</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #800000">'</span><span style="COLOR: #800000">(Initialized&nbsp;SchoolMember:&nbsp;%s)</span><span style="COLOR: #800000">'</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #000000">%</span><span style="COLOR: #000000">&nbsp;self.name<br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">def</span><span style="COLOR: #000000">&nbsp;tell(self):<br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #800000">'''</span><span style="COLOR: #800000">Tell&nbsp;my&nbsp;details.</span><span style="COLOR: #800000">'''</span><span style="COLOR: #000000"><br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">print</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #800000">'</span><span style="COLOR: #800000">Name:"%s"&nbsp;Age:"%s"</span><span style="COLOR: #800000">'</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #000000">%</span><span style="COLOR: #000000">&nbsp;(self.name,&nbsp;self.age),<br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif"></span><span style="COLOR: #0000ff">class</span><span style="COLOR: #000000">&nbsp;Teacher(SchoolMember):<br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #800000">'''</span><span style="COLOR: #800000">Represents&nbsp;a&nbsp;teacher.</span><span style="COLOR: #800000">'''</span><span style="COLOR: #000000"><br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">def</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #800080">__init__</span><span style="COLOR: #000000">(self,&nbsp;name,&nbsp;age,&nbsp;salary):<br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;SchoolMember.</span><span style="COLOR: #800080">__init__</span><span style="COLOR: #000000">(self,&nbsp;name,&nbsp;age)<br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;self.salary&nbsp;</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">&nbsp;salary<br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">print</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #800000">'</span><span style="COLOR: #800000">(Initialized&nbsp;Teacher:&nbsp;%s)</span><span style="COLOR: #800000">'</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #000000">%</span><span style="COLOR: #000000">&nbsp;self.name<br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">def</span><span style="COLOR: #000000">&nbsp;tell(self):<br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;SchoolMember.tell(self)<br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">print</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #800000">'</span><span style="COLOR: #800000">Salary:&nbsp;"%d"</span><span style="COLOR: #800000">'</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #000000">%</span><span style="COLOR: #000000">&nbsp;self.salary<br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif"></span><span style="COLOR: #0000ff">class</span><span style="COLOR: #000000">&nbsp;Student(SchoolMember):<br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #800000">'''</span><span style="COLOR: #800000">Represents&nbsp;a&nbsp;student.</span><span style="COLOR: #800000">'''</span><span style="COLOR: #000000"><br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">def</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #800080">__init__</span><span style="COLOR: #000000">(self,&nbsp;name,&nbsp;age,&nbsp;marks):<br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;SchoolMember.</span><span style="COLOR: #800080">__init__</span><span style="COLOR: #000000">(self,&nbsp;name,&nbsp;age)<br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;self.marks&nbsp;</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">&nbsp;marks<br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">print</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #800000">'</span><span style="COLOR: #800000">(Initialized&nbsp;Student:&nbsp;%s)</span><span style="COLOR: #800000">'</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #000000">%</span><span style="COLOR: #000000">&nbsp;self.name<br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">def</span><span style="COLOR: #000000">&nbsp;tell(self):<br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;SchoolMember.tell(self)<br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">print</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #800000">'</span><span style="COLOR: #800000">Marks:&nbsp;"%d"</span><span style="COLOR: #800000">'</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #000000">%</span><span style="COLOR: #000000">&nbsp;self.marks<br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif">t&nbsp;</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">&nbsp;Teacher(</span><span style="COLOR: #800000">'</span><span style="COLOR: #800000">Mrs.&nbsp;Shrividya</span><span style="COLOR: #800000">'</span><span style="COLOR: #000000">,&nbsp;</span><span style="COLOR: #000000">40</span><span style="COLOR: #000000">,&nbsp;</span><span style="COLOR: #000000">30000</span><span style="COLOR: #000000">)<br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif">s&nbsp;</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">&nbsp;Student(</span><span style="COLOR: #800000">'</span><span style="COLOR: #800000">Swaroop</span><span style="COLOR: #800000">'</span><span style="COLOR: #000000">,&nbsp;</span><span style="COLOR: #000000">22</span><span style="COLOR: #000000">,&nbsp;</span><span style="COLOR: #000000">75</span><span style="COLOR: #000000">)<br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif"></span><span style="COLOR: #0000ff">print</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #008000">#</span><span style="COLOR: #008000">&nbsp;prints&nbsp;a&nbsp;blank&nbsp;line</span><span style="COLOR: #008000"><br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif"></span><span style="COLOR: #000000">members&nbsp;</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">&nbsp;[t,&nbsp;s]<br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif"></span><span style="COLOR: #0000ff">for</span><span style="COLOR: #000000">&nbsp;member&nbsp;</span><span style="COLOR: #0000ff">in</span><span style="COLOR: #000000">&nbsp;members:<br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif">&nbsp;&nbsp;&nbsp;&nbsp;member.tell()&nbsp;</span><span style="COLOR: #008000">#</span><span style="COLOR: #008000">&nbsp;works&nbsp;for&nbsp;both&nbsp;Teachers&nbsp;and&nbsp;Students&nbsp;</span></div>
<strong>输出</strong><br>$ python inherit.py<br>(Initialized SchoolMember: Mrs. Shrividya)<br>(Initialized Teacher: Mrs. Shrividya)<br>(Initialized SchoolMember: Swaroop)<br>(Initialized Student: Swaroop)<br>Name:"Mrs. Shrividya" Age:"40" Salary: "30000"<br>Name:"Swaroop" Age:"22" Marks: "75" 
<img src ="http://www.cppblog.com/fenglin/aggbug/125414.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/fenglin/" target="_blank">风林</a> 2010-08-31 16:23 <a href="http://www.cppblog.com/fenglin/articles/125414.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>使用类与对象的变量</title><link>http://www.cppblog.com/fenglin/articles/125411.html</link><dc:creator>风林</dc:creator><author>风林</author><pubDate>Tue, 31 Aug 2010 08:04:00 GMT</pubDate><guid>http://www.cppblog.com/fenglin/articles/125411.html</guid><wfw:comment>http://www.cppblog.com/fenglin/comments/125411.html</wfw:comment><comments>http://www.cppblog.com/fenglin/articles/125411.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/fenglin/comments/commentRss/125411.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/fenglin/services/trackbacks/125411.html</trackback:ping><description><![CDATA[<div style="BORDER-BOTTOM: #cccccc 1px solid; BORDER-LEFT: #cccccc 1px solid; PADDING-BOTTOM: 4px; BACKGROUND-COLOR: #eeeeee; PADDING-LEFT: 4px; WIDTH: 98%; PADDING-RIGHT: 5px; FONT-SIZE: 13px; WORD-BREAK: break-all; BORDER-TOP: #cccccc 1px solid; BORDER-RIGHT: #cccccc 1px solid; PADDING-TOP: 4px"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif"><span style="COLOR: #008000">#</span><span style="COLOR: #008000">!/usr/bin/python</span><span style="COLOR: #008000"><br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif">#</span><span style="COLOR: #008000">&nbsp;Filename:&nbsp;objvar.py</span><span style="COLOR: #008000"><br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif"></span><span style="COLOR: #0000ff">class</span><span style="COLOR: #000000">&nbsp;Person:<br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #800000">'''</span><span style="COLOR: #800000">Represents&nbsp;a&nbsp;person.</span><span style="COLOR: #800000">'''</span><span style="COLOR: #000000"><br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif">&nbsp;&nbsp;&nbsp;&nbsp;population&nbsp;</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">&nbsp;0<br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">def</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #800080">__init__</span><span style="COLOR: #000000">(self,&nbsp;name):<br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #800000">'''</span><span style="COLOR: #800000">Initializes&nbsp;the&nbsp;person's&nbsp;data.</span><span style="COLOR: #800000">'''</span><span style="COLOR: #000000"><br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;self.name&nbsp;</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">&nbsp;name<br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">print</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #800000">'</span><span style="COLOR: #800000">(Initializing&nbsp;%s)</span><span style="COLOR: #800000">'</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #000000">%</span><span style="COLOR: #000000">&nbsp;self.name<br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #008000">#</span><span style="COLOR: #008000">&nbsp;When&nbsp;this&nbsp;person&nbsp;is&nbsp;created,&nbsp;he/she</span><span style="COLOR: #008000"><br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif"></span><span style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #008000">#</span><span style="COLOR: #008000">&nbsp;adds&nbsp;to&nbsp;the&nbsp;population</span><span style="COLOR: #008000"><br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif"></span><span style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Person.population&nbsp;</span><span style="COLOR: #000000">+=</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000"><br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">def</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #800080">__del__</span><span style="COLOR: #000000">(self):<br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #800000">'''</span><span style="COLOR: #800000">I&nbsp;am&nbsp;dying.</span><span style="COLOR: #800000">'''</span><span style="COLOR: #000000"><br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">print</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #800000">'</span><span style="COLOR: #800000">%s&nbsp;says&nbsp;bye.</span><span style="COLOR: #800000">'</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #000000">%</span><span style="COLOR: #000000">&nbsp;self.name<br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Person.population&nbsp;</span><span style="COLOR: #000000">-=</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000"><br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">if</span><span style="COLOR: #000000">&nbsp;Person.population&nbsp;</span><span style="COLOR: #000000">==</span><span style="COLOR: #000000">&nbsp;0:<br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">print</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #800000">'</span><span style="COLOR: #800000">I&nbsp;am&nbsp;the&nbsp;last&nbsp;one.</span><span style="COLOR: #800000">'</span><span style="COLOR: #000000"><br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">else</span><span style="COLOR: #000000">:<br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">print</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #800000">'</span><span style="COLOR: #800000">There&nbsp;are&nbsp;still&nbsp;%d&nbsp;people&nbsp;left.</span><span style="COLOR: #800000">'</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #000000">%</span><span style="COLOR: #000000">&nbsp;Person.population<br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">def</span><span style="COLOR: #000000">&nbsp;sayHi(self):<br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #800000">'''</span><span style="COLOR: #800000">Greeting&nbsp;by&nbsp;the&nbsp;person.<br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Really,&nbsp;that's&nbsp;all&nbsp;it&nbsp;does.</span><span style="COLOR: #800000">'''</span><span style="COLOR: #000000"><br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">print</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #800000">'</span><span style="COLOR: #800000">Hi,&nbsp;my&nbsp;name&nbsp;is&nbsp;%s.</span><span style="COLOR: #800000">'</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #000000">%</span><span style="COLOR: #000000">&nbsp;self.name<br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">def</span><span style="COLOR: #000000">&nbsp;howMany(self):<br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #800000">'''</span><span style="COLOR: #800000">Prints&nbsp;the&nbsp;current&nbsp;population.</span><span style="COLOR: #800000">'''</span><span style="COLOR: #000000"><br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">if</span><span style="COLOR: #000000">&nbsp;Person.population&nbsp;</span><span style="COLOR: #000000">==</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">:<br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">print</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #800000">'</span><span style="COLOR: #800000">I&nbsp;am&nbsp;the&nbsp;only&nbsp;person&nbsp;here.</span><span style="COLOR: #800000">'</span><span style="COLOR: #000000"><br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">else</span><span style="COLOR: #000000">:<br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">print</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #800000">'</span><span style="COLOR: #800000">We&nbsp;have&nbsp;%d&nbsp;persons&nbsp;here.</span><span style="COLOR: #800000">'</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #000000">%</span><span style="COLOR: #000000">&nbsp;Person.population<br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif">swaroop&nbsp;</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">&nbsp;Person(</span><span style="COLOR: #800000">'</span><span style="COLOR: #800000">Swaroop</span><span style="COLOR: #800000">'</span><span style="COLOR: #000000">)<br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif">swaroop.sayHi()<br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif">swaroop.howMany()<br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif">kalam&nbsp;</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">&nbsp;Person(</span><span style="COLOR: #800000">'</span><span style="COLOR: #800000">Abdul&nbsp;Kalam</span><span style="COLOR: #800000">'</span><span style="COLOR: #000000">)<br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif">kalam.sayHi()<br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif">kalam.howMany()<br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif">swaroop.sayHi()<br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif">swaroop.howMany()&nbsp;</span></div>
<strong>输出</strong><br>$ python objvar.py<br>(Initializing Swaroop)<br>Hi, my name is Swaroop.<br>I am the only person here.<br>(Initializing Abdul Kalam)<br>Hi, my name is Abdul Kalam.<br>We have 2 persons here.<br>Hi, my name is Swaroop.<br>We have 2 persons here.<br>Abdul Kalam says bye.<br>There are still 1 people left.<br>Swaroop says bye.<br>I am the last one. <br><br><strong>它如何工作</strong><br>这是一个很长的例子，但是它有助于说明类与对象的变量的本质。这里，population属于Person<br>类，因此是一个类的变量。name变量属于对象（它使用self赋值）因此是对象的变量。<br>观察可以发现__init__方法用一个名字来初始化Person实例。在这个方法中，我们让population<br>增加1，这是因为我们增加了一个人。同样可以发现，self.name的值根据每个对象指定，这表<br>明了它作为对象的变量的本质。<br>记住，你只能使用self变量来参考同一个对象的变量和方法。这被称为 属性参考 。<br>在这个程序中，我们还看到docstring对于类和方法同样有用。我们可以在运行时使用Person.<br>__doc__和Person.sayHi.__doc__来分别访问类与方法的文档字符串。<br>就如同__init__方法一样，还有一个特殊的方法__del__，它在对象消逝的时候被调用。对象消<br>逝即对象不再被使用，它所占用的内存将返回给系统作它用。在这个方法里面，我们只是简单<br>地把Person.population减1。<br>当对象不再被使用时，__del__方法运行，但是很难保证这个方法究竟在 什么时候 运行。如果<br>你想要指明它的运行，你就得使用del语句，就如同我们在以前的例子中使用的那样。<br>给C++/Java/C#程序员的注释<br>Python中所有的类成员（包括数据成员）都是 公共的 ，所有的方法都是 有效的 。<br>只有一个例外：如果你使用的数据成员名称以 双下划线前缀 比如__privatevar，Python的名称<br>管理体系会有效地把它作为私有变量。<br>这样就有一个惯例，如果某个变量只想在类或对象中使用，就应该以单下划线前缀。而其他的<br>名称都将作为公共的，可以被其他类/对象使用。记住这只是一个惯例，并不是Python所要求<br>的（与双下划线前缀不同）。<br>同样，注意__del__方法与 destructor 的概念类似。 
<img src ="http://www.cppblog.com/fenglin/aggbug/125411.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/fenglin/" target="_blank">风林</a> 2010-08-31 16:04 <a href="http://www.cppblog.com/fenglin/articles/125411.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>__init__方法</title><link>http://www.cppblog.com/fenglin/articles/125408.html</link><dc:creator>风林</dc:creator><author>风林</author><pubDate>Tue, 31 Aug 2010 07:39:00 GMT</pubDate><guid>http://www.cppblog.com/fenglin/articles/125408.html</guid><wfw:comment>http://www.cppblog.com/fenglin/comments/125408.html</wfw:comment><comments>http://www.cppblog.com/fenglin/articles/125408.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/fenglin/comments/commentRss/125408.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/fenglin/services/trackbacks/125408.html</trackback:ping><description><![CDATA[__init__方法在类的一个对象被建立时，马上运行。这个方法可以用来对你的对象做一些你希<br>望的 初始化 。注意，这个名称的开始和结尾都是双下划线。<br>
<div style="BORDER-BOTTOM: #cccccc 1px solid; BORDER-LEFT: #cccccc 1px solid; PADDING-BOTTOM: 4px; BACKGROUND-COLOR: #eeeeee; PADDING-LEFT: 4px; WIDTH: 98%; PADDING-RIGHT: 5px; FONT-SIZE: 13px; WORD-BREAK: break-all; BORDER-TOP: #cccccc 1px solid; BORDER-RIGHT: #cccccc 1px solid; PADDING-TOP: 4px"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif"><span style="COLOR: #008000">#</span><span style="COLOR: #008000">!/usr/bin/python</span><span style="COLOR: #008000"><br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif">#</span><span style="COLOR: #008000">&nbsp;Filename:&nbsp;class_init.py</span><span style="COLOR: #008000"><br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif"></span><span style="COLOR: #0000ff">class</span><span style="COLOR: #000000">&nbsp;Person:<br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">def</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #800080">__init__</span><span style="COLOR: #000000">(self,&nbsp;name):<br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;self.name&nbsp;</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">&nbsp;name<br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">def</span><span style="COLOR: #000000">&nbsp;sayHi(self):<br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">print</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #800000">'</span><span style="COLOR: #800000">Hello,&nbsp;my&nbsp;name&nbsp;is</span><span style="COLOR: #800000">'</span><span style="COLOR: #000000">,&nbsp;self.name<br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif">p&nbsp;</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">&nbsp;Person(</span><span style="COLOR: #800000">'</span><span style="COLOR: #800000">Swaroop</span><span style="COLOR: #800000">'</span><span style="COLOR: #000000">)<br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif">p.sayHi()<br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif"></span><span style="COLOR: #008000">#</span><span style="COLOR: #008000">&nbsp;This&nbsp;short&nbsp;example&nbsp;can&nbsp;also&nbsp;be&nbsp;written&nbsp;as&nbsp;Person('Swaroop').sayHi()&nbsp;</span></div>
<strong>输出</strong><br>$ python class_init.py<br>Hello, my name is Swaroop <br><br><strong>它如何工作<br></strong>这里，我们把__init__方法定义为取一个参数name（以及普通的参数self）。在这个__init__里，<br>我们只是创建一个新的域，也称为name。注意它们是两个不同的变量，尽管它们有相同的名<br>字。点号使我们能够区分它们。<br>最重要的是，我们没有专门调用__init__方法，只是在创建一个类的新实例的时候，把参数包<br>括在圆括号内跟在类名后面，从而传递给__init__方法。这是这种方法的重要之处。<br>现在，我们能够在我们的方法中使用self.name域。这在sayHi方法中得到了验证。
<img src ="http://www.cppblog.com/fenglin/aggbug/125408.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/fenglin/" target="_blank">风林</a> 2010-08-31 15:39 <a href="http://www.cppblog.com/fenglin/articles/125408.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>类</title><link>http://www.cppblog.com/fenglin/articles/125407.html</link><dc:creator>风林</dc:creator><author>风林</author><pubDate>Tue, 31 Aug 2010 07:33:00 GMT</pubDate><guid>http://www.cppblog.com/fenglin/articles/125407.html</guid><wfw:comment>http://www.cppblog.com/fenglin/comments/125407.html</wfw:comment><comments>http://www.cppblog.com/fenglin/articles/125407.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/fenglin/comments/commentRss/125407.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/fenglin/services/trackbacks/125407.html</trackback:ping><description><![CDATA[<div style="BORDER-BOTTOM: #cccccc 1px solid; BORDER-LEFT: #cccccc 1px solid; PADDING-BOTTOM: 4px; BACKGROUND-COLOR: #eeeeee; PADDING-LEFT: 4px; WIDTH: 98%; PADDING-RIGHT: 5px; FONT-SIZE: 13px; WORD-BREAK: break-all; BORDER-TOP: #cccccc 1px solid; BORDER-RIGHT: #cccccc 1px solid; PADDING-TOP: 4px"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif"><span style="COLOR: #008000">#</span><span style="COLOR: #008000">!/usr/bin/python</span><span style="COLOR: #008000"><br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif">#</span><span style="COLOR: #008000">&nbsp;Filename:&nbsp;simplestclass.py</span><span style="COLOR: #008000"><br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif"></span><span style="COLOR: #0000ff">class</span><span style="COLOR: #000000">&nbsp;Person:<br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">pass</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #008000">#</span><span style="COLOR: #008000">&nbsp;An&nbsp;empty&nbsp;block</span><span style="COLOR: #008000"><br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif"></span><span style="COLOR: #000000">p&nbsp;</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">&nbsp;Person()<br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif"></span><span style="COLOR: #0000ff">print</span><span style="COLOR: #000000">&nbsp;p&nbsp;</span></div>
<strong>输出</strong><br>$ python simplestclass.py<br>&lt;__main__.Person instance at 0xf6fcb18c&gt; <br><br>我们使用class语句后跟类名，创建了一个新的类。这后面跟着一个缩进的语句块形成类体。在<br>这个例子中，我们使用了一个空白块，它由pass语句表示。<br>接下来，我们使用类名后跟一对圆括号来创建一个对象/实例。为了验证，我们简单地打印了这<br>个变量的类型。它告诉我们我们已经在__main__模块中有了一个Person类的实例。<br>可以注意到存储对象的计算机内存地址也打印了出来。这个地址在你的计算机上会是另外一个<br>值，因为Python可以在任何空位存储对象。
<img src ="http://www.cppblog.com/fenglin/aggbug/125407.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/fenglin/" target="_blank">风林</a> 2010-08-31 15:33 <a href="http://www.cppblog.com/fenglin/articles/125407.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>字符串的方法</title><link>http://www.cppblog.com/fenglin/articles/125399.html</link><dc:creator>风林</dc:creator><author>风林</author><pubDate>Tue, 31 Aug 2010 06:22:00 GMT</pubDate><guid>http://www.cppblog.com/fenglin/articles/125399.html</guid><wfw:comment>http://www.cppblog.com/fenglin/comments/125399.html</wfw:comment><comments>http://www.cppblog.com/fenglin/articles/125399.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/fenglin/comments/commentRss/125399.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/fenglin/services/trackbacks/125399.html</trackback:ping><description><![CDATA[<div style="BORDER-BOTTOM: #cccccc 1px solid; BORDER-LEFT: #cccccc 1px solid; PADDING-BOTTOM: 4px; BACKGROUND-COLOR: #eeeeee; PADDING-LEFT: 4px; WIDTH: 98%; PADDING-RIGHT: 5px; FONT-SIZE: 13px; WORD-BREAK: break-all; BORDER-TOP: #cccccc 1px solid; BORDER-RIGHT: #cccccc 1px solid; PADDING-TOP: 4px"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif"><span style="COLOR: #008000">#</span><span style="COLOR: #008000">!/usr/bin/python</span><span style="COLOR: #008000"><br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif">#</span><span style="COLOR: #008000">&nbsp;Filename:&nbsp;str_methods.py</span><span style="COLOR: #008000"><br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif"></span><span style="COLOR: #000000">name&nbsp;</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #800000">'</span><span style="COLOR: #800000">Swaroop</span><span style="COLOR: #800000">'</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #008000">#</span><span style="COLOR: #008000">&nbsp;This&nbsp;is&nbsp;a&nbsp;string&nbsp;object&nbsp;</span><span style="COLOR: #008000"><br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif"></span><span style="COLOR: #0000ff">if</span><span style="COLOR: #000000">&nbsp;name.startswith(</span><span style="COLOR: #800000">'</span><span style="COLOR: #800000">Swa</span><span style="COLOR: #800000">'</span><span style="COLOR: #000000">):<br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">print</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #800000">'</span><span style="COLOR: #800000">Yes,&nbsp;the&nbsp;string&nbsp;starts&nbsp;with&nbsp;"Swa"</span><span style="COLOR: #800000">'</span><span style="COLOR: #000000"><br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif"></span><span style="COLOR: #0000ff">if</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #800000">'</span><span style="COLOR: #800000">a</span><span style="COLOR: #800000">'</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #0000ff">in</span><span style="COLOR: #000000">&nbsp;name:<br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">print</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #800000">'</span><span style="COLOR: #800000">Yes,&nbsp;it&nbsp;contains&nbsp;the&nbsp;string&nbsp;"a"</span><span style="COLOR: #800000">'</span><span style="COLOR: #000000"><br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif"></span><span style="COLOR: #0000ff">if</span><span style="COLOR: #000000">&nbsp;name.find(</span><span style="COLOR: #800000">'</span><span style="COLOR: #800000">war</span><span style="COLOR: #800000">'</span><span style="COLOR: #000000">)&nbsp;</span><span style="COLOR: #000000">!=</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #000000">-</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">:<br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">print</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #800000">'</span><span style="COLOR: #800000">Yes,&nbsp;it&nbsp;contains&nbsp;the&nbsp;string&nbsp;"war"</span><span style="COLOR: #800000">'</span><span style="COLOR: #000000"><br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif">delimiter&nbsp;</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #800000">'</span><span style="COLOR: #800000">_*_</span><span style="COLOR: #800000">'</span><span style="COLOR: #000000"><br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif">mylist&nbsp;</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">&nbsp;[</span><span style="COLOR: #800000">'</span><span style="COLOR: #800000">Brazil</span><span style="COLOR: #800000">'</span><span style="COLOR: #000000">,&nbsp;</span><span style="COLOR: #800000">'</span><span style="COLOR: #800000">Russia</span><span style="COLOR: #800000">'</span><span style="COLOR: #000000">,&nbsp;</span><span style="COLOR: #800000">'</span><span style="COLOR: #800000">India</span><span style="COLOR: #800000">'</span><span style="COLOR: #000000">,&nbsp;</span><span style="COLOR: #800000">'</span><span style="COLOR: #800000">China</span><span style="COLOR: #800000">'</span><span style="COLOR: #000000">]<br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif"></span><span style="COLOR: #0000ff">print</span><span style="COLOR: #000000">&nbsp;delimiter.join(mylist)&nbsp;</span></div>
<strong>输出<br></strong>$ python str_methods.py<br>Yes, the string starts with "Swa"<br>Yes, it contains the string "a"<br>Yes, it contains the string "war"<br>Brazil_*_Russia_*_India_*_China<br>&nbsp;<br><strong>它如何工作</strong><br>这里，我们看到使用了许多字符串方法。<span style="COLOR: red">startwith</span>方法是用来测试字符串是否以给定字符串开<br>始。<span style="COLOR: red">in</span>操作符用来检验一个给定字符串是否为另一个字符串的一部分。<br><span style="COLOR: red">find</span>方法用来找出给定字符串在另一个字符串中的位置，或者返回-1以表示找不到子字符串。<br>str类也有以一个作为分隔符的字符串<span style="COLOR: red">join</span>序列的项目的整洁的方法，它返回一个生成的大字符<br>串。
<img src ="http://www.cppblog.com/fenglin/aggbug/125399.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/fenglin/" target="_blank">风林</a> 2010-08-31 14:22 <a href="http://www.cppblog.com/fenglin/articles/125399.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>引用</title><link>http://www.cppblog.com/fenglin/articles/125398.html</link><dc:creator>风林</dc:creator><author>风林</author><pubDate>Tue, 31 Aug 2010 06:14:00 GMT</pubDate><guid>http://www.cppblog.com/fenglin/articles/125398.html</guid><wfw:comment>http://www.cppblog.com/fenglin/comments/125398.html</wfw:comment><comments>http://www.cppblog.com/fenglin/articles/125398.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/fenglin/comments/commentRss/125398.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/fenglin/services/trackbacks/125398.html</trackback:ping><description><![CDATA[当你创建一个对象并给它赋一个变量的时候，这个变量仅仅 引用 那个对象，而不是表示这个<br>对象本身！也就是说，变量名指向你计算机中存储那个对象的内存。这被称作名称到对象的绑<br>定。<br>
<div style="BORDER-BOTTOM: #cccccc 1px solid; BORDER-LEFT: #cccccc 1px solid; PADDING-BOTTOM: 4px; BACKGROUND-COLOR: #eeeeee; PADDING-LEFT: 4px; WIDTH: 98%; PADDING-RIGHT: 5px; FONT-SIZE: 13px; WORD-BREAK: break-all; BORDER-TOP: #cccccc 1px solid; BORDER-RIGHT: #cccccc 1px solid; PADDING-TOP: 4px"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif"><span style="COLOR: #008000">#</span><span style="COLOR: #008000">!/usr/bin/python</span><span style="COLOR: #008000"><br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif">#</span><span style="COLOR: #008000">&nbsp;Filename:&nbsp;reference.py</span><span style="COLOR: #008000"><br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif"></span><span style="COLOR: #0000ff">print</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #800000">'</span><span style="COLOR: #800000">Simple&nbsp;Assignment</span><span style="COLOR: #800000">'</span><span style="COLOR: #000000"><br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif">shoplist&nbsp;</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">&nbsp;[</span><span style="COLOR: #800000">'</span><span style="COLOR: #800000">apple</span><span style="COLOR: #800000">'</span><span style="COLOR: #000000">,&nbsp;</span><span style="COLOR: #800000">'</span><span style="COLOR: #800000">mango</span><span style="COLOR: #800000">'</span><span style="COLOR: #000000">,&nbsp;</span><span style="COLOR: #800000">'</span><span style="COLOR: #800000">carrot</span><span style="COLOR: #800000">'</span><span style="COLOR: #000000">,&nbsp;</span><span style="COLOR: #800000">'</span><span style="COLOR: #800000">banana</span><span style="COLOR: #800000">'</span><span style="COLOR: #000000">]<br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif">mylist&nbsp;</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">&nbsp;shoplist&nbsp;</span><span style="COLOR: #008000">#</span><span style="COLOR: #008000">&nbsp;mylist&nbsp;is&nbsp;just&nbsp;another&nbsp;name&nbsp;pointing&nbsp;to&nbsp;the&nbsp;same&nbsp;object!</span><span style="COLOR: #008000"><br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif"></span><span style="COLOR: #0000ff">del</span><span style="COLOR: #000000">&nbsp;shoplist[0]<br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif"></span><span style="COLOR: #0000ff">print</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #800000">'</span><span style="COLOR: #800000">shoplist&nbsp;is</span><span style="COLOR: #800000">'</span><span style="COLOR: #000000">,&nbsp;shoplist<br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif"></span><span style="COLOR: #0000ff">print</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #800000">'</span><span style="COLOR: #800000">mylist&nbsp;is</span><span style="COLOR: #800000">'</span><span style="COLOR: #000000">,&nbsp;mylist<br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif"></span><span style="COLOR: #008000">#</span><span style="COLOR: #008000">&nbsp;notice&nbsp;that&nbsp;both&nbsp;shoplist&nbsp;and&nbsp;mylist&nbsp;both&nbsp;print&nbsp;the&nbsp;same&nbsp;list&nbsp;without</span><span style="COLOR: #008000"><br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif">#</span><span style="COLOR: #008000">&nbsp;the&nbsp;'apple'&nbsp;confirming&nbsp;that&nbsp;they&nbsp;point&nbsp;to&nbsp;the&nbsp;same&nbsp;object</span><span style="COLOR: #008000"><br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif"></span><span style="COLOR: #0000ff">print</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #800000">'</span><span style="COLOR: #800000">Copy&nbsp;by&nbsp;making&nbsp;a&nbsp;full&nbsp;slice</span><span style="COLOR: #800000">'</span><span style="COLOR: #000000"><br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif">mylist&nbsp;</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">&nbsp;shoplist[:]&nbsp;</span><span style="COLOR: #008000">#</span><span style="COLOR: #008000">&nbsp;make&nbsp;a&nbsp;copy&nbsp;by&nbsp;doing&nbsp;a&nbsp;full&nbsp;slice</span><span style="COLOR: #008000"><br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif"></span><span style="COLOR: #0000ff">del</span><span style="COLOR: #000000">&nbsp;mylist[0]&nbsp;</span><span style="COLOR: #008000">#</span><span style="COLOR: #008000">&nbsp;remove&nbsp;first&nbsp;item</span><span style="COLOR: #008000"><br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif"></span><span style="COLOR: #0000ff">print</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #800000">'</span><span style="COLOR: #800000">shoplist&nbsp;is</span><span style="COLOR: #800000">'</span><span style="COLOR: #000000">,&nbsp;shoplist<br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif"></span><span style="COLOR: #0000ff">print</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #800000">'</span><span style="COLOR: #800000">mylist&nbsp;is</span><span style="COLOR: #800000">'</span><span style="COLOR: #000000">,&nbsp;mylist<br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif"></span><span style="COLOR: #008000">#</span><span style="COLOR: #008000">&nbsp;notice&nbsp;that&nbsp;now&nbsp;the&nbsp;two&nbsp;lists&nbsp;are&nbsp;different&nbsp;</span></div>
<strong>输出<br></strong>$ python reference.py<br>Simple Assignment<br>shoplist is ['mango', 'carrot', 'banana']<br>mylist is ['mango', 'carrot', 'banana']<br>Copy by making a full slice<br>shoplist is ['mango', 'carrot', 'banana']<br>mylist is ['carrot', 'banana']<br><br>你需要记住的只是如果你想要复制一个列表或者类似的序<br>列或者其他复杂的对象（不是如整数那样的简单 对象 ），那么你必须使用切片操作符来取得<br>拷贝。如果你只是想要使用另一个变量名，两个名称都 引用 同一个对象，那么如果你不小心<br>的话，可能会引来各种麻烦。
<img src ="http://www.cppblog.com/fenglin/aggbug/125398.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/fenglin/" target="_blank">风林</a> 2010-08-31 14:14 <a href="http://www.cppblog.com/fenglin/articles/125398.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>序列</title><link>http://www.cppblog.com/fenglin/articles/125384.html</link><dc:creator>风林</dc:creator><author>风林</author><pubDate>Tue, 31 Aug 2010 03:41:00 GMT</pubDate><guid>http://www.cppblog.com/fenglin/articles/125384.html</guid><wfw:comment>http://www.cppblog.com/fenglin/comments/125384.html</wfw:comment><comments>http://www.cppblog.com/fenglin/articles/125384.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/fenglin/comments/commentRss/125384.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/fenglin/services/trackbacks/125384.html</trackback:ping><description><![CDATA[列表、元组和字符串都是序列，但是序列是什么，它们为什么如此特别呢？序列的两个主要特<br>点是索引操作符和切片操作符。索引操作符让我们可以从序列中抓取一个特定项目。切片操作<br>符让我们能够获取序列的一个切片，即一部分序列。<br>
<div style="BORDER-BOTTOM: #cccccc 1px solid; BORDER-LEFT: #cccccc 1px solid; PADDING-BOTTOM: 4px; BACKGROUND-COLOR: #eeeeee; PADDING-LEFT: 4px; WIDTH: 98%; PADDING-RIGHT: 5px; FONT-SIZE: 13px; WORD-BREAK: break-all; BORDER-TOP: #cccccc 1px solid; BORDER-RIGHT: #cccccc 1px solid; PADDING-TOP: 4px"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif"><span style="COLOR: #008000">#</span><span style="COLOR: #008000">!/usr/bin/python</span><span style="COLOR: #008000"><br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif">#</span><span style="COLOR: #008000">&nbsp;Filename:&nbsp;seq.py</span><span style="COLOR: #008000"><br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif"></span><span style="COLOR: #000000">shoplist&nbsp;</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">&nbsp;[</span><span style="COLOR: #800000">'</span><span style="COLOR: #800000">apple</span><span style="COLOR: #800000">'</span><span style="COLOR: #000000">,&nbsp;</span><span style="COLOR: #800000">'</span><span style="COLOR: #800000">mango</span><span style="COLOR: #800000">'</span><span style="COLOR: #000000">,&nbsp;</span><span style="COLOR: #800000">'</span><span style="COLOR: #800000">carrot</span><span style="COLOR: #800000">'</span><span style="COLOR: #000000">,&nbsp;</span><span style="COLOR: #800000">'</span><span style="COLOR: #800000">banana</span><span style="COLOR: #800000">'</span><span style="COLOR: #000000">]<br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif"></span><span style="COLOR: #008000">#</span><span style="COLOR: #008000">&nbsp;Indexing&nbsp;or&nbsp;'Subscription'&nbsp;operation</span><span style="COLOR: #008000"><br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif"></span><span style="COLOR: #0000ff">print</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #800000">'</span><span style="COLOR: #800000">Item&nbsp;0&nbsp;is</span><span style="COLOR: #800000">'</span><span style="COLOR: #000000">,&nbsp;shoplist[0]<br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif"></span><span style="COLOR: #0000ff">print</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #800000">'</span><span style="COLOR: #800000">Item&nbsp;1&nbsp;is</span><span style="COLOR: #800000">'</span><span style="COLOR: #000000">,&nbsp;shoplist[</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">]<br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif"></span><span style="COLOR: #0000ff">print</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #800000">'</span><span style="COLOR: #800000">Item&nbsp;2&nbsp;is</span><span style="COLOR: #800000">'</span><span style="COLOR: #000000">,&nbsp;shoplist[</span><span style="COLOR: #000000">2</span><span style="COLOR: #000000">]<br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif"></span><span style="COLOR: #0000ff">print</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #800000">'</span><span style="COLOR: #800000">Item&nbsp;3&nbsp;is</span><span style="COLOR: #800000">'</span><span style="COLOR: #000000">,&nbsp;shoplist[</span><span style="COLOR: #000000">3</span><span style="COLOR: #000000">]<br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif"></span><span style="COLOR: #0000ff">print</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #800000">'</span><span style="COLOR: #800000">Item&nbsp;-1&nbsp;is</span><span style="COLOR: #800000">'</span><span style="COLOR: #000000">,&nbsp;shoplist[</span><span style="COLOR: #000000">-</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">]<br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif"></span><span style="COLOR: #0000ff">print</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #800000">'</span><span style="COLOR: #800000">Item&nbsp;-2&nbsp;is</span><span style="COLOR: #800000">'</span><span style="COLOR: #000000">,&nbsp;shoplist[</span><span style="COLOR: #000000">-</span><span style="COLOR: #000000">2</span><span style="COLOR: #000000">]<br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif"></span><span style="COLOR: #008000">#</span><span style="COLOR: #008000">&nbsp;Slicing&nbsp;on&nbsp;a&nbsp;list</span><span style="COLOR: #008000"><br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif"></span><span style="COLOR: #0000ff">print</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #800000">'</span><span style="COLOR: #800000">Item&nbsp;1&nbsp;to&nbsp;3&nbsp;is</span><span style="COLOR: #800000">'</span><span style="COLOR: #000000">,&nbsp;shoplist[</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">:</span><span style="COLOR: #000000">3</span><span style="COLOR: #000000">]<br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif"></span><span style="COLOR: #0000ff">print</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #800000">'</span><span style="COLOR: #800000">Item&nbsp;2&nbsp;to&nbsp;end&nbsp;is</span><span style="COLOR: #800000">'</span><span style="COLOR: #000000">,&nbsp;shoplist[</span><span style="COLOR: #000000">2</span><span style="COLOR: #000000">:]<br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif"></span><span style="COLOR: #0000ff">print</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #800000">'</span><span style="COLOR: #800000">Item&nbsp;1&nbsp;to&nbsp;-1&nbsp;is</span><span style="COLOR: #800000">'</span><span style="COLOR: #000000">,&nbsp;shoplist[</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">:</span><span style="COLOR: #000000">-</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">]<br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif"></span><span style="COLOR: #0000ff">print</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #800000">'</span><span style="COLOR: #800000">Item&nbsp;start&nbsp;to&nbsp;end&nbsp;is</span><span style="COLOR: #800000">'</span><span style="COLOR: #000000">,&nbsp;shoplist[:]<br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif"></span><span style="COLOR: #008000">#</span><span style="COLOR: #008000">&nbsp;Slicing&nbsp;on&nbsp;a&nbsp;string</span><span style="COLOR: #008000"><br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif"></span><span style="COLOR: #000000">name&nbsp;</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #800000">'</span><span style="COLOR: #800000">swaroop</span><span style="COLOR: #800000">'</span><span style="COLOR: #000000"><br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif"></span><span style="COLOR: #0000ff">print</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #800000">'</span><span style="COLOR: #800000">characters&nbsp;1&nbsp;to&nbsp;3&nbsp;is</span><span style="COLOR: #800000">'</span><span style="COLOR: #000000">,&nbsp;name[</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">:</span><span style="COLOR: #000000">3</span><span style="COLOR: #000000">]<br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif"></span><span style="COLOR: #0000ff">print</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #800000">'</span><span style="COLOR: #800000">characters&nbsp;2&nbsp;to&nbsp;end&nbsp;is</span><span style="COLOR: #800000">'</span><span style="COLOR: #000000">,&nbsp;name[</span><span style="COLOR: #000000">2</span><span style="COLOR: #000000">:]<br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif"></span><span style="COLOR: #0000ff">print</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #800000">'</span><span style="COLOR: #800000">characters&nbsp;1&nbsp;to&nbsp;-1&nbsp;is</span><span style="COLOR: #800000">'</span><span style="COLOR: #000000">,&nbsp;name[</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">:</span><span style="COLOR: #000000">-</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">]<br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif"></span><span style="COLOR: #0000ff">print</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #800000">'</span><span style="COLOR: #800000">characters&nbsp;start&nbsp;to&nbsp;end&nbsp;is</span><span style="COLOR: #800000">'</span><span style="COLOR: #000000">,&nbsp;name[:]&nbsp;</span></div>
<strong>输出</strong><br>$ python seq.py<br>Item 0 is apple<br>Item 1 is mango<br>Item 2 is carrot<br>Item 3 is banana<br>Item -1 is banana<br>Item -2 is carrot<br>Item 1 to 3 is ['mango', 'carrot']<br>Item 2 to end is ['carrot', 'banana']<br>Item 1 to -1 is ['mango', 'carrot']<br>Item start to end is ['apple', 'mango', 'carrot', 'banana']<br>characters 1 to 3 is wa<br>characters 2 to end is aroop<br>characters 1 to -1 is waroo<br>characters start to end is swaroop <br><br><strong>它如何工作</strong><br>首先，我们来学习如何使用索引来取得序列中的单个项目。这也被称作是下标操作。每当你用<br>方括号中的一个数来指定一个序列的时候，Python会为你抓取序列中对应位置的项目。记住，<br>Python从0开始计数。因此，shoplist[0]抓取第一个项目，shoplist[3]抓取shoplist序列中的第四个<br>元素。<br><span style="COLOR: red">索引同样可以是负数，在那样的情况下，位置是从序列尾开始计算的。</span>因此，shoplist[-1]表示<br>序列的最后一个元素而shoplist[-2]抓取序列的倒数第二个项目。<br>切片操作符是序列名后跟一个方括号，方括号中有一对可选的数字，并用冒号分割。注意这与<br>你使用的索引操作符十分相似。<span style="COLOR: red">记住数是可选的，而冒号是必须的。</span><br>切片操作符中的第一个数（冒号之前）表示切片开始的位置，第二个数（冒号之后）表示切片<br>到哪里结束。如果不指定第一个数，Python就从序列首开始。如果没有指定第二个数，则<br>Python会停止在序列尾。注意，返回的序列从开始位置 开始 ，刚好在 结束 位置之前结束。即<br>开始位置是包含在序列切片中的，而结束位置被排斥在切片外。<br>这样，shoplist[1:3]返回从位置1开始，包括位置2，但是停止在位置3的一个序列切片，因此返<br>回一个含有两个项目的切片。类似地，shoplist[:]返回整个序列的拷贝。<br>你可以用负数做切片。负数用在从序列尾开始计算的位置。例如，shoplist[:-1]会返回除了最后<br>一个项目外包含所有项目的序列切片。<br>使用Python解释器交互地尝试不同切片指定组合，即在提示符下你能够马上看到结果。序列的<br>神奇之处在于你可以用相同的方法访问元组、列表和字符串。
<img src ="http://www.cppblog.com/fenglin/aggbug/125384.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/fenglin/" target="_blank">风林</a> 2010-08-31 11:41 <a href="http://www.cppblog.com/fenglin/articles/125384.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>字典</title><link>http://www.cppblog.com/fenglin/articles/125383.html</link><dc:creator>风林</dc:creator><author>风林</author><pubDate>Tue, 31 Aug 2010 03:26:00 GMT</pubDate><guid>http://www.cppblog.com/fenglin/articles/125383.html</guid><wfw:comment>http://www.cppblog.com/fenglin/comments/125383.html</wfw:comment><comments>http://www.cppblog.com/fenglin/articles/125383.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/fenglin/comments/commentRss/125383.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/fenglin/services/trackbacks/125383.html</trackback:ping><description><![CDATA[字典类似于你通过联系人名字查找地址和联系人详细情况的地址簿，即，我们把键（名字）和<br>值（详细情况）联系在一起。注意，键必须是唯一的，就像如果有两个人恰巧同名的话，你无<br>法找到正确的信息。<br>注意，你只能使用不可变的对象（比如字符串）来作为字典的键，但是你可以把不可变或可变<br>的对象作为字典的值。基本说来就是，你应该只使用简单的对象作为键。<br>键值对在字典中以这样的方式标记：d = {key1 : value1, key2 : value2 }。注意它们的键/值对用冒<br>号分割，而各个对用逗号分割，所有这些都包括在花括号中。<br>记住字典中的键/值对是没有顺序的。如果你想要一个特定的顺序，那么你应该在使用前自己<br>对它们排序。<br>字典是dict类的实例/对象。<br><br>
<div style="BORDER-BOTTOM: #cccccc 1px solid; BORDER-LEFT: #cccccc 1px solid; PADDING-BOTTOM: 4px; BACKGROUND-COLOR: #eeeeee; PADDING-LEFT: 4px; WIDTH: 98%; PADDING-RIGHT: 5px; FONT-SIZE: 13px; WORD-BREAK: break-all; BORDER-TOP: #cccccc 1px solid; BORDER-RIGHT: #cccccc 1px solid; PADDING-TOP: 4px"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif"><span style="COLOR: #008000">#</span><span style="COLOR: #008000">!/usr/bin/python</span><span style="COLOR: #008000"><br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif">#</span><span style="COLOR: #008000">&nbsp;Filename:&nbsp;using_dict.py</span><span style="COLOR: #008000"><br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif">#</span><span style="COLOR: #008000">&nbsp;'ab'&nbsp;is&nbsp;short&nbsp;for&nbsp;'a'ddress'b'ook</span><span style="COLOR: #008000"><br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif"></span><span style="COLOR: #000000">ab&nbsp;</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">&nbsp;{&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #800000">'</span><span style="COLOR: #800000">Swaroop</span><span style="COLOR: #800000">'</span><span style="COLOR: #000000">&nbsp;&nbsp;&nbsp;:&nbsp;</span><span style="COLOR: #800000">'</span><span style="COLOR: #800000">swaroopch@byteofpython.info</span><span style="COLOR: #800000">'</span><span style="COLOR: #000000">,<br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #800000">'</span><span style="COLOR: #800000">Larry</span><span style="COLOR: #800000">'</span><span style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;:&nbsp;</span><span style="COLOR: #800000">'</span><span style="COLOR: #800000">larry@wall.org</span><span style="COLOR: #800000">'</span><span style="COLOR: #000000">,<br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #800000">'</span><span style="COLOR: #800000">Matsumoto</span><span style="COLOR: #800000">'</span><span style="COLOR: #000000">&nbsp;:&nbsp;</span><span style="COLOR: #800000">'</span><span style="COLOR: #800000">matz@ruby-lang.org</span><span style="COLOR: #800000">'</span><span style="COLOR: #000000">,<br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #800000">'</span><span style="COLOR: #800000">Spammer</span><span style="COLOR: #800000">'</span><span style="COLOR: #000000">&nbsp;&nbsp;&nbsp;:&nbsp;</span><span style="COLOR: #800000">'</span><span style="COLOR: #800000">spammer@hotmail.com</span><span style="COLOR: #800000">'</span><span style="COLOR: #000000"><br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif"></span><span style="COLOR: #0000ff">print</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #800000">"</span><span style="COLOR: #800000">Swaroop's&nbsp;address&nbsp;is&nbsp;%s</span><span style="COLOR: #800000">"</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #000000">%</span><span style="COLOR: #000000">&nbsp;ab[</span><span style="COLOR: #800000">'</span><span style="COLOR: #800000">Swaroop</span><span style="COLOR: #800000">'</span><span style="COLOR: #000000">]<br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif"></span><span style="COLOR: #008000">#</span><span style="COLOR: #008000">&nbsp;Adding&nbsp;a&nbsp;key/value&nbsp;pair</span><span style="COLOR: #008000"><br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif"></span><span style="COLOR: #000000">ab[</span><span style="COLOR: #800000">'</span><span style="COLOR: #800000">Guido</span><span style="COLOR: #800000">'</span><span style="COLOR: #000000">]&nbsp;</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #800000">'</span><span style="COLOR: #800000">guido@python.org</span><span style="COLOR: #800000">'</span><span style="COLOR: #000000"><br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif"></span><span style="COLOR: #008000">#</span><span style="COLOR: #008000">&nbsp;Deleting&nbsp;a&nbsp;key/value&nbsp;pair</span><span style="COLOR: #008000"><br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif"></span><span style="COLOR: #0000ff">del</span><span style="COLOR: #000000">&nbsp;ab[</span><span style="COLOR: #800000">'</span><span style="COLOR: #800000">Spammer</span><span style="COLOR: #800000">'</span><span style="COLOR: #000000">]<br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif"></span><span style="COLOR: #0000ff">print</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #800000">'</span><span style="COLOR: #800000">\nThere&nbsp;are&nbsp;%d&nbsp;contacts&nbsp;in&nbsp;the&nbsp;address-book\n</span><span style="COLOR: #800000">'</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #000000">%</span><span style="COLOR: #000000">&nbsp;len(ab)<br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif"></span><span style="COLOR: #0000ff">for</span><span style="COLOR: #000000">&nbsp;name,&nbsp;address&nbsp;</span><span style="COLOR: #0000ff">in</span><span style="COLOR: #000000">&nbsp;ab.items():<br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">print</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #800000">'</span><span style="COLOR: #800000">Contact&nbsp;%s&nbsp;at&nbsp;%s</span><span style="COLOR: #800000">'</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #000000">%</span><span style="COLOR: #000000">&nbsp;(name,&nbsp;address)<br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif"></span><span style="COLOR: #0000ff">if</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #800000">'</span><span style="COLOR: #800000">Guido</span><span style="COLOR: #800000">'</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #0000ff">in</span><span style="COLOR: #000000">&nbsp;ab:&nbsp;</span><span style="COLOR: #008000">#</span><span style="COLOR: #008000">&nbsp;OR&nbsp;ab.has_key('Guido')</span><span style="COLOR: #008000"><br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif"></span><span style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">print</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #800000">"</span><span style="COLOR: #800000">\nGuido's&nbsp;address&nbsp;is&nbsp;%s</span><span style="COLOR: #800000">"</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #000000">%</span><span style="COLOR: #000000">&nbsp;ab[</span><span style="COLOR: #800000">'</span><span style="COLOR: #800000">Guido</span><span style="COLOR: #800000">'</span><span style="COLOR: #000000">]&nbsp;</span></div>
<strong>输出</strong><br>$ python using_dict.py<br>Swaroop's address is <a href="&#109;&#97;&#105;&#108;&#116;&#111;&#58;&#115;&#119;&#97;&#114;&#111;&#111;&#112;&#99;&#104;&#64;&#98;&#121;&#116;&#101;&#111;&#102;&#112;&#121;&#116;&#104;&#111;&#110;&#46;&#105;&#110;&#102;&#111;">swaroopch@byteofpython.info</a><br>There are 4 contacts in the address-book<br>Contact Swaroop at <a href="&#109;&#97;&#105;&#108;&#116;&#111;&#58;&#115;&#119;&#97;&#114;&#111;&#111;&#112;&#99;&#104;&#64;&#98;&#121;&#116;&#101;&#111;&#102;&#112;&#121;&#116;&#104;&#111;&#110;&#46;&#105;&#110;&#102;&#111;">swaroopch@byteofpython.info</a><br>Contact Matsumoto at <a href="&#109;&#97;&#105;&#108;&#116;&#111;&#58;&#109;&#97;&#116;&#122;&#64;&#114;&#117;&#98;&#121;&#45;&#108;&#97;&#110;&#103;&#46;&#111;&#114;&#103;">matz@ruby-lang.org</a><br>Contact Larry at <a href="&#109;&#97;&#105;&#108;&#116;&#111;&#58;&#108;&#97;&#114;&#114;&#121;&#64;&#119;&#97;&#108;&#108;&#46;&#111;&#114;&#103;">larry@wall.org</a><br>Contact Guido at <a href="&#109;&#97;&#105;&#108;&#116;&#111;&#58;&#103;&#117;&#105;&#100;&#111;&#64;&#112;&#121;&#116;&#104;&#111;&#110;&#46;&#111;&#114;&#103;">guido@python.org</a><br>Guido's address is <a href="&#109;&#97;&#105;&#108;&#116;&#111;&#58;&#103;&#117;&#105;&#100;&#111;&#64;&#112;&#121;&#116;&#104;&#111;&#110;&#46;&#111;&#114;&#103;">guido@python.org</a> <br><br>
<img src ="http://www.cppblog.com/fenglin/aggbug/125383.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/fenglin/" target="_blank">风林</a> 2010-08-31 11:26 <a href="http://www.cppblog.com/fenglin/articles/125383.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>元组</title><link>http://www.cppblog.com/fenglin/articles/125380.html</link><dc:creator>风林</dc:creator><author>风林</author><pubDate>Tue, 31 Aug 2010 03:09:00 GMT</pubDate><guid>http://www.cppblog.com/fenglin/articles/125380.html</guid><wfw:comment>http://www.cppblog.com/fenglin/comments/125380.html</wfw:comment><comments>http://www.cppblog.com/fenglin/articles/125380.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/fenglin/comments/commentRss/125380.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/fenglin/services/trackbacks/125380.html</trackback:ping><description><![CDATA[元组和列表十分类似，只不过元组和字符串一样是 不可变的 即你不能修改元组。元组通过圆<br>括号中用逗号分割的项目定义。元组通常用在使语句或用户定义的函数能够安全地采用一组值<br>的时候，即被使用的元组的值不会改变。<br><br>
<div style="BORDER-BOTTOM: #cccccc 1px solid; BORDER-LEFT: #cccccc 1px solid; PADDING-BOTTOM: 4px; BACKGROUND-COLOR: #eeeeee; PADDING-LEFT: 4px; WIDTH: 98%; PADDING-RIGHT: 5px; FONT-SIZE: 13px; WORD-BREAK: break-all; BORDER-TOP: #cccccc 1px solid; BORDER-RIGHT: #cccccc 1px solid; PADDING-TOP: 4px"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif"><span style="COLOR: #008000">#</span><span style="COLOR: #008000">!/usr/bin/python</span><span style="COLOR: #008000"><br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif">#</span><span style="COLOR: #008000">&nbsp;Filename:&nbsp;using_tuple.py</span><span style="COLOR: #008000"><br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif"></span><span style="COLOR: #000000">zoo&nbsp;</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">&nbsp;(</span><span style="COLOR: #800000">'</span><span style="COLOR: #800000">wolf</span><span style="COLOR: #800000">'</span><span style="COLOR: #000000">,&nbsp;</span><span style="COLOR: #800000">'</span><span style="COLOR: #800000">elephant</span><span style="COLOR: #800000">'</span><span style="COLOR: #000000">,&nbsp;</span><span style="COLOR: #800000">'</span><span style="COLOR: #800000">penguin</span><span style="COLOR: #800000">'</span><span style="COLOR: #000000">)<br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif"></span><span style="COLOR: #0000ff">print</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #800000">'</span><span style="COLOR: #800000">Number&nbsp;of&nbsp;animals&nbsp;in&nbsp;the&nbsp;zoo&nbsp;is</span><span style="COLOR: #800000">'</span><span style="COLOR: #000000">,&nbsp;len(zoo)<br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif">new_zoo&nbsp;</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">&nbsp;(</span><span style="COLOR: #800000">'</span><span style="COLOR: #800000">monkey</span><span style="COLOR: #800000">'</span><span style="COLOR: #000000">,&nbsp;</span><span style="COLOR: #800000">'</span><span style="COLOR: #800000">dolphin</span><span style="COLOR: #800000">'</span><span style="COLOR: #000000">,&nbsp;zoo)<br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif"></span><span style="COLOR: #0000ff">print</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #800000">'</span><span style="COLOR: #800000">Number&nbsp;of&nbsp;animals&nbsp;in&nbsp;the&nbsp;new&nbsp;zoo&nbsp;is</span><span style="COLOR: #800000">'</span><span style="COLOR: #000000">,&nbsp;len(new_zoo)<br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif"></span><span style="COLOR: #0000ff">print</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #800000">'</span><span style="COLOR: #800000">All&nbsp;animals&nbsp;in&nbsp;new&nbsp;zoo&nbsp;are</span><span style="COLOR: #800000">'</span><span style="COLOR: #000000">,&nbsp;new_zoo<br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif"></span><span style="COLOR: #0000ff">print</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #800000">'</span><span style="COLOR: #800000">Animals&nbsp;brought&nbsp;from&nbsp;old&nbsp;zoo&nbsp;are</span><span style="COLOR: #800000">'</span><span style="COLOR: #000000">,&nbsp;new_zoo[</span><span style="COLOR: #000000">2</span><span style="COLOR: #000000">]<br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif"></span><span style="COLOR: #0000ff">print</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #800000">'</span><span style="COLOR: #800000">Last&nbsp;animal&nbsp;brought&nbsp;from&nbsp;old&nbsp;zoo&nbsp;is</span><span style="COLOR: #800000">'</span><span style="COLOR: #000000">,&nbsp;new_zoo[</span><span style="COLOR: #000000">2</span><span style="COLOR: #000000">][</span><span style="COLOR: #000000">2</span><span style="COLOR: #000000">]</span></div>
<strong>输出<br></strong>$ python using_tuple.py<br>Number of animals in the zoo is 3<br>Number of animals in the new zoo is 3<br>All animals in new zoo are ('monkey', 'dolphin', ('wolf', 'elephant', 'penguin'))<br>Animals brought from old zoo are ('wolf', 'elephant', 'penguin')<br>Last animal brought from old zoo is penguin<br>&nbsp;<br><span style="COLOR: red">含有0个或1个项目的元组。一个空的元组由一对空的圆括号组成，如myempty = ()。然而，含<br>有单个元素的元组就不那么简单了。你必须在第一个（唯一一个）项目后跟一个逗号，这样<br>Python才能区分元组和表达式中一个带圆括号的对象。即如果你想要的是一个包含项目2的元<br>组的时候，你应该指明singleton = (2 , )。<br><br><span><strong>元组与打印语句<br></strong>
<div style="BORDER-BOTTOM: #cccccc 1px solid; BORDER-LEFT: #cccccc 1px solid; PADDING-BOTTOM: 4px; BACKGROUND-COLOR: #eeeeee; PADDING-LEFT: 4px; WIDTH: 98%; PADDING-RIGHT: 5px; FONT-SIZE: 13px; WORD-BREAK: break-all; BORDER-TOP: #cccccc 1px solid; BORDER-RIGHT: #cccccc 1px solid; PADDING-TOP: 4px"><strong><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif"><span style="COLOR: #008000">#</span><span style="COLOR: #008000">!/usr/bin/python</span></strong><span style="COLOR: #008000"><br><strong><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif">#</strong></span><span style="COLOR: #008000"><strong>&nbsp;Filename:&nbsp;print_tuple.py</strong></span><span style="COLOR: #008000"><br><strong><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif"></strong></span><strong><span style="COLOR: #000000">age&nbsp;</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #000000">22</span></strong><span style="COLOR: #000000"><br><strong><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif">name&nbsp;</strong></span><strong><span style="COLOR: #000000">=</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #800000">'</span><span style="COLOR: #800000">Swaroop</span><span style="COLOR: #800000">'</span></strong><span style="COLOR: #000000"><br><strong><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif"></strong></span><strong><span style="COLOR: #0000ff">print</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #800000">'</span><span style="COLOR: #800000">%s&nbsp;is&nbsp;%d&nbsp;years&nbsp;old</span><span style="COLOR: #800000">'</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #000000">%</span></strong><strong><span style="COLOR: #000000">&nbsp;(name,&nbsp;age)<br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif"></span><span style="COLOR: #0000ff">print</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #800000">'</span><span style="COLOR: #800000">Why&nbsp;is&nbsp;%s&nbsp;playing&nbsp;with&nbsp;that&nbsp;python?</span><span style="COLOR: #800000">'</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #000000">%</span><span style="COLOR: #000000">&nbsp;name&nbsp;</span></strong></div>
<br><span style="COLOR: #000000"><strong>输出<br></strong>$ python print_tuple.py<br>Swaroop is 22 years old<br>Why is Swaroop playing with that python?</span></span></span>
<img src ="http://www.cppblog.com/fenglin/aggbug/125380.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/fenglin/" target="_blank">风林</a> 2010-08-31 11:09 <a href="http://www.cppblog.com/fenglin/articles/125380.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>列表</title><link>http://www.cppblog.com/fenglin/articles/125368.html</link><dc:creator>风林</dc:creator><author>风林</author><pubDate>Tue, 31 Aug 2010 02:54:00 GMT</pubDate><guid>http://www.cppblog.com/fenglin/articles/125368.html</guid><wfw:comment>http://www.cppblog.com/fenglin/comments/125368.html</wfw:comment><comments>http://www.cppblog.com/fenglin/articles/125368.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/fenglin/comments/commentRss/125368.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/fenglin/services/trackbacks/125368.html</trackback:ping><description><![CDATA[list是处理一组有序项目的数据结构，即你可以在一个列表中存储一个 序列 的项目。假想你有<br>一个购物列表，上面记载着你要买的东西，你就容易理解列表了。只不过在你的购物表上，可<br>能每样东西都独自占有一行，而在Python中，你在每个项目之间用逗号分割。<br>列表中的项目应该包括在方括号中，这样Python就知道你是在指明一个列表。一旦你创建了一<br>个列表，你可以添加、删除或是搜索列表中的项目。由于你可以增加或删除项目，我们说列表<br>是 可变的 数据类型，即这种类型是可以被改变的。<br><br>
<div style="BORDER-BOTTOM: #cccccc 1px solid; BORDER-LEFT: #cccccc 1px solid; PADDING-BOTTOM: 4px; BACKGROUND-COLOR: #eeeeee; PADDING-LEFT: 4px; WIDTH: 98%; PADDING-RIGHT: 5px; FONT-SIZE: 13px; WORD-BREAK: break-all; BORDER-TOP: #cccccc 1px solid; BORDER-RIGHT: #cccccc 1px solid; PADDING-TOP: 4px"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif"><span style="COLOR: #008000">#</span><span style="COLOR: #008000">!/usr/bin/python</span><span style="COLOR: #008000"><br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif">#</span><span style="COLOR: #008000">&nbsp;Filename:&nbsp;using_list.py</span><span style="COLOR: #008000"><br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif">#</span><span style="COLOR: #008000">&nbsp;This&nbsp;is&nbsp;my&nbsp;shopping&nbsp;list</span><span style="COLOR: #008000"><br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif"></span><span style="COLOR: #000000">shoplist&nbsp;</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">&nbsp;[</span><span style="COLOR: #800000">'</span><span style="COLOR: #800000">apple</span><span style="COLOR: #800000">'</span><span style="COLOR: #000000">,&nbsp;</span><span style="COLOR: #800000">'</span><span style="COLOR: #800000">mango</span><span style="COLOR: #800000">'</span><span style="COLOR: #000000">,&nbsp;</span><span style="COLOR: #800000">'</span><span style="COLOR: #800000">carrot</span><span style="COLOR: #800000">'</span><span style="COLOR: #000000">,&nbsp;</span><span style="COLOR: #800000">'</span><span style="COLOR: #800000">banana</span><span style="COLOR: #800000">'</span><span style="COLOR: #000000">]<br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif"></span><span style="COLOR: #0000ff">print</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #800000">'</span><span style="COLOR: #800000">I&nbsp;have</span><span style="COLOR: #800000">'</span><span style="COLOR: #000000">,&nbsp;len(shoplist),</span><span style="COLOR: #800000">'</span><span style="COLOR: #800000">items&nbsp;to&nbsp;purchase.</span><span style="COLOR: #800000">'</span><span style="COLOR: #000000"><br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif"></span><span style="COLOR: #0000ff">print</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #800000">'</span><span style="COLOR: #800000">These&nbsp;items&nbsp;are:</span><span style="COLOR: #800000">'</span><span style="COLOR: #000000">,&nbsp;</span><span style="COLOR: #008000">#</span><span style="COLOR: #008000">&nbsp;Notice&nbsp;the&nbsp;comma&nbsp;at&nbsp;end&nbsp;of&nbsp;the&nbsp;line</span><span style="COLOR: #008000"><br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif"></span><span style="COLOR: #0000ff">for</span><span style="COLOR: #000000">&nbsp;item&nbsp;</span><span style="COLOR: #0000ff">in</span><span style="COLOR: #000000">&nbsp;shoplist:<br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">print</span><span style="COLOR: #000000">&nbsp;item,<br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif"></span><span style="COLOR: #0000ff">print</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #800000">'</span><span style="COLOR: #800000">\nI&nbsp;also&nbsp;have&nbsp;to&nbsp;buy&nbsp;rice.</span><span style="COLOR: #800000">'</span><span style="COLOR: #000000"><br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif">shoplist.append(</span><span style="COLOR: #800000">'</span><span style="COLOR: #800000">rice</span><span style="COLOR: #800000">'</span><span style="COLOR: #000000">)<br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif"></span><span style="COLOR: #0000ff">print</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #800000">'</span><span style="COLOR: #800000">My&nbsp;shopping&nbsp;list&nbsp;is&nbsp;now</span><span style="COLOR: #800000">'</span><span style="COLOR: #000000">,&nbsp;shoplist<br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif"></span><span style="COLOR: #0000ff">print</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #800000">'</span><span style="COLOR: #800000">I&nbsp;will&nbsp;sort&nbsp;my&nbsp;list&nbsp;now</span><span style="COLOR: #800000">'</span><span style="COLOR: #000000"><br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif">shoplist.sort()<br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif"></span><span style="COLOR: #0000ff">print</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #800000">'</span><span style="COLOR: #800000">Sorted&nbsp;shopping&nbsp;list&nbsp;is</span><span style="COLOR: #800000">'</span><span style="COLOR: #000000">,&nbsp;shoplist<br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif"><br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif"></span><span style="COLOR: #0000ff">print</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #800000">'</span><span style="COLOR: #800000">The&nbsp;first&nbsp;item&nbsp;I&nbsp;will&nbsp;buy&nbsp;is</span><span style="COLOR: #800000">'</span><span style="COLOR: #000000">,&nbsp;shoplist[0]<br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif">olditem&nbsp;</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">&nbsp;shoplist[0]<br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif"></span><span style="COLOR: #0000ff">del</span><span style="COLOR: #000000">&nbsp;shoplist[0]<br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif"></span><span style="COLOR: #0000ff">print</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #800000">'</span><span style="COLOR: #800000">I&nbsp;bought&nbsp;the</span><span style="COLOR: #800000">'</span><span style="COLOR: #000000">,&nbsp;olditem<br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif"></span><span style="COLOR: #0000ff">print</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #800000">'</span><span style="COLOR: #800000">My&nbsp;shopping&nbsp;list&nbsp;is&nbsp;now</span><span style="COLOR: #800000">'</span><span style="COLOR: #000000">,&nbsp;shoplist&nbsp;</span></div>
<strong>输出</strong><br>$ python using_list.py<br>I have 4 items to purchase.<br>These items are: apple mango carrot banana<br>I also have to buy rice.<br>My shopping list is now ['apple', 'mango', 'carrot', 'banana', 'rice']<br>I will sort my list now<br>Sorted shopping list is ['apple', 'banana', 'carrot', 'mango', 'rice']<br>The first item I will buy is apple<br>I bought the apple<br>My shopping list is now ['banana', 'carrot', 'mango', 'rice']<br><br><span style="COLOR: red">注意，我们在print语句的结尾使用了一个 逗号 来消除每个print语句自动打印的换行符。这样<br>做有点难看，不过确实简单有效。</span> 
<img src ="http://www.cppblog.com/fenglin/aggbug/125368.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/fenglin/" target="_blank">风林</a> 2010-08-31 10:54 <a href="http://www.cppblog.com/fenglin/articles/125368.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>数据结构</title><link>http://www.cppblog.com/fenglin/articles/125365.html</link><dc:creator>风林</dc:creator><author>风林</author><pubDate>Tue, 31 Aug 2010 02:38:00 GMT</pubDate><guid>http://www.cppblog.com/fenglin/articles/125365.html</guid><wfw:comment>http://www.cppblog.com/fenglin/comments/125365.html</wfw:comment><comments>http://www.cppblog.com/fenglin/articles/125365.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/fenglin/comments/commentRss/125365.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/fenglin/services/trackbacks/125365.html</trackback:ping><description><![CDATA[数据结构基本上就是——它们是可以处理一些 数据 的 结构 。或者说，它们是用来存储一组相关数据的。<br>在Python中有三种内建的数据结构——列表、元组和字典。
<img src ="http://www.cppblog.com/fenglin/aggbug/125365.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/fenglin/" target="_blank">风林</a> 2010-08-31 10:38 <a href="http://www.cppblog.com/fenglin/articles/125365.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>dir()函数</title><link>http://www.cppblog.com/fenglin/articles/125364.html</link><dc:creator>风林</dc:creator><author>风林</author><pubDate>Tue, 31 Aug 2010 02:36:00 GMT</pubDate><guid>http://www.cppblog.com/fenglin/articles/125364.html</guid><wfw:comment>http://www.cppblog.com/fenglin/comments/125364.html</wfw:comment><comments>http://www.cppblog.com/fenglin/articles/125364.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/fenglin/comments/commentRss/125364.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/fenglin/services/trackbacks/125364.html</trackback:ping><description><![CDATA[你可以使用内建的dir函数来列出模块定义的标识符。标识符有函数、类和变量。<br>当你为dir()提供一个模块名的时候，它返回模块定义的名称列表。如果不提供参数，它返回当<br>前模块中定义的名称列表。<br>
<div style="BORDER-BOTTOM: #cccccc 1px solid; BORDER-LEFT: #cccccc 1px solid; PADDING-BOTTOM: 4px; BACKGROUND-COLOR: #eeeeee; PADDING-LEFT: 4px; WIDTH: 98%; PADDING-RIGHT: 5px; FONT-SIZE: 13px; WORD-BREAK: break-all; BORDER-TOP: #cccccc 1px solid; BORDER-RIGHT: #cccccc 1px solid; PADDING-TOP: 4px"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif"><span style="COLOR: #000000">$&nbsp;python<br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif"></span><span style="COLOR: #000000">&gt;&gt;&gt;</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #0000ff">import</span><span style="COLOR: #000000">&nbsp;sys<br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif"></span><span style="COLOR: #000000">&gt;&gt;&gt;</span><span style="COLOR: #000000">&nbsp;dir(sys)&nbsp;</span><span style="COLOR: #008000">#</span><span style="COLOR: #008000">&nbsp;get&nbsp;list&nbsp;of&nbsp;attributes&nbsp;for&nbsp;sys&nbsp;module</span><span style="COLOR: #008000"><br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif"></span><span style="COLOR: #000000">[</span><span style="COLOR: #800000">'</span><span style="COLOR: #800000">__displayhook__</span><span style="COLOR: #800000">'</span><span style="COLOR: #000000">,&nbsp;</span><span style="COLOR: #800000">'</span><span style="COLOR: #800000">__doc__</span><span style="COLOR: #800000">'</span><span style="COLOR: #000000">,&nbsp;</span><span style="COLOR: #800000">'</span><span style="COLOR: #800000">__excepthook__</span><span style="COLOR: #800000">'</span><span style="COLOR: #000000">,&nbsp;</span><span style="COLOR: #800000">'</span><span style="COLOR: #800000">__name__</span><span style="COLOR: #800000">'</span><span style="COLOR: #000000">,&nbsp;</span><span style="COLOR: #800000">'</span><span style="COLOR: #800000">__stderr__</span><span style="COLOR: #800000">'</span><span style="COLOR: #000000">,<br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif"></span><span style="COLOR: #800000">'</span><span style="COLOR: #800000">__stdin__</span><span style="COLOR: #800000">'</span><span style="COLOR: #000000">,&nbsp;</span><span style="COLOR: #800000">'</span><span style="COLOR: #800000">__stdout__</span><span style="COLOR: #800000">'</span><span style="COLOR: #000000">,&nbsp;</span><span style="COLOR: #800000">'</span><span style="COLOR: #800000">_getframe</span><span style="COLOR: #800000">'</span><span style="COLOR: #000000">,&nbsp;</span><span style="COLOR: #800000">'</span><span style="COLOR: #800000">api_version</span><span style="COLOR: #800000">'</span><span style="COLOR: #000000">,&nbsp;</span><span style="COLOR: #800000">'</span><span style="COLOR: #800000">argv</span><span style="COLOR: #800000">'</span><span style="COLOR: #000000">,<br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif"></span><span style="COLOR: #800000">'</span><span style="COLOR: #800000">builtin_module_names</span><span style="COLOR: #800000">'</span><span style="COLOR: #000000">,&nbsp;</span><span style="COLOR: #800000">'</span><span style="COLOR: #800000">byteorder</span><span style="COLOR: #800000">'</span><span style="COLOR: #000000">,&nbsp;</span><span style="COLOR: #800000">'</span><span style="COLOR: #800000">call_tracing</span><span style="COLOR: #800000">'</span><span style="COLOR: #000000">,&nbsp;</span><span style="COLOR: #800000">'</span><span style="COLOR: #800000">callstats</span><span style="COLOR: #800000">'</span><span style="COLOR: #000000">,<br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif"></span><span style="COLOR: #800000">'</span><span style="COLOR: #800000">copyright</span><span style="COLOR: #800000">'</span><span style="COLOR: #000000">,&nbsp;</span><span style="COLOR: #800000">'</span><span style="COLOR: #800000">displayhook</span><span style="COLOR: #800000">'</span><span style="COLOR: #000000">,&nbsp;</span><span style="COLOR: #800000">'</span><span style="COLOR: #800000">exc_clear</span><span style="COLOR: #800000">'</span><span style="COLOR: #000000">,&nbsp;</span><span style="COLOR: #800000">'</span><span style="COLOR: #800000">exc_info</span><span style="COLOR: #800000">'</span><span style="COLOR: #000000">,&nbsp;</span><span style="COLOR: #800000">'</span><span style="COLOR: #800000">exc_type</span><span style="COLOR: #800000">'</span><span style="COLOR: #000000">,<br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif"></span><span style="COLOR: #800000">'</span><span style="COLOR: #800000">excepthook</span><span style="COLOR: #800000">'</span><span style="COLOR: #000000">,&nbsp;</span><span style="COLOR: #800000">'</span><span style="COLOR: #800000">exec_prefix</span><span style="COLOR: #800000">'</span><span style="COLOR: #000000">,&nbsp;</span><span style="COLOR: #800000">'</span><span style="COLOR: #800000">executable</span><span style="COLOR: #800000">'</span><span style="COLOR: #000000">,&nbsp;</span><span style="COLOR: #800000">'</span><span style="COLOR: #800000">exit</span><span style="COLOR: #800000">'</span><span style="COLOR: #000000">,&nbsp;</span><span style="COLOR: #800000">'</span><span style="COLOR: #800000">getcheckinterval</span><span style="COLOR: #800000">'</span><span style="COLOR: #000000">,<br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif"></span><span style="COLOR: #800000">'</span><span style="COLOR: #800000">getdefaultencoding</span><span style="COLOR: #800000">'</span><span style="COLOR: #000000">,&nbsp;</span><span style="COLOR: #800000">'</span><span style="COLOR: #800000">getdlopenflags</span><span style="COLOR: #800000">'</span><span style="COLOR: #000000">,&nbsp;</span><span style="COLOR: #800000">'</span><span style="COLOR: #800000">getfilesystemencoding</span><span style="COLOR: #800000">'</span><span style="COLOR: #000000">,<br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif"></span><span style="COLOR: #800000">'</span><span style="COLOR: #800000">getrecursionlimit</span><span style="COLOR: #800000">'</span><span style="COLOR: #000000">,&nbsp;</span><span style="COLOR: #800000">'</span><span style="COLOR: #800000">getrefcount</span><span style="COLOR: #800000">'</span><span style="COLOR: #000000">,&nbsp;</span><span style="COLOR: #800000">'</span><span style="COLOR: #800000">hexversion</span><span style="COLOR: #800000">'</span><span style="COLOR: #000000">,&nbsp;</span><span style="COLOR: #800000">'</span><span style="COLOR: #800000">maxint</span><span style="COLOR: #800000">'</span><span style="COLOR: #000000">,&nbsp;</span><span style="COLOR: #800000">'</span><span style="COLOR: #800000">maxunicode</span><span style="COLOR: #800000">'</span><span style="COLOR: #000000">,<br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif"></span><span style="COLOR: #800000">'</span><span style="COLOR: #800000">meta_path</span><span style="COLOR: #800000">'</span><span style="COLOR: #000000">,</span><span style="COLOR: #800000">'</span><span style="COLOR: #800000">modules</span><span style="COLOR: #800000">'</span><span style="COLOR: #000000">,&nbsp;</span><span style="COLOR: #800000">'</span><span style="COLOR: #800000">path</span><span style="COLOR: #800000">'</span><span style="COLOR: #000000">,&nbsp;</span><span style="COLOR: #800000">'</span><span style="COLOR: #800000">path_hooks</span><span style="COLOR: #800000">'</span><span style="COLOR: #000000">,&nbsp;</span><span style="COLOR: #800000">'</span><span style="COLOR: #800000">path_importer_cache</span><span style="COLOR: #800000">'</span><span style="COLOR: #000000">,<br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif"></span><span style="COLOR: #800000">'</span><span style="COLOR: #800000">platform</span><span style="COLOR: #800000">'</span><span style="COLOR: #000000">,&nbsp;</span><span style="COLOR: #800000">'</span><span style="COLOR: #800000">prefix</span><span style="COLOR: #800000">'</span><span style="COLOR: #000000">,&nbsp;</span><span style="COLOR: #800000">'</span><span style="COLOR: #800000">ps1</span><span style="COLOR: #800000">'</span><span style="COLOR: #000000">,&nbsp;</span><span style="COLOR: #800000">'</span><span style="COLOR: #800000">ps2</span><span style="COLOR: #800000">'</span><span style="COLOR: #000000">,&nbsp;</span><span style="COLOR: #800000">'</span><span style="COLOR: #800000">setcheckinterval</span><span style="COLOR: #800000">'</span><span style="COLOR: #000000">,&nbsp;</span><span style="COLOR: #800000">'</span><span style="COLOR: #800000">setdlopenflags</span><span style="COLOR: #800000">'</span><span style="COLOR: #000000">,<br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif"></span><span style="COLOR: #800000">'</span><span style="COLOR: #800000">setprofile</span><span style="COLOR: #800000">'</span><span style="COLOR: #000000">,&nbsp;</span><span style="COLOR: #800000">'</span><span style="COLOR: #800000">setrecursionlimit</span><span style="COLOR: #800000">'</span><span style="COLOR: #000000">,&nbsp;</span><span style="COLOR: #800000">'</span><span style="COLOR: #800000">settrace</span><span style="COLOR: #800000">'</span><span style="COLOR: #000000">,&nbsp;</span><span style="COLOR: #800000">'</span><span style="COLOR: #800000">stderr</span><span style="COLOR: #800000">'</span><span style="COLOR: #000000">,&nbsp;</span><span style="COLOR: #800000">'</span><span style="COLOR: #800000">stdin</span><span style="COLOR: #800000">'</span><span style="COLOR: #000000">,&nbsp;</span><span style="COLOR: #800000">'</span><span style="COLOR: #800000">stdout</span><span style="COLOR: #800000">'</span><span style="COLOR: #000000">,<br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif"></span><span style="COLOR: #800000">'</span><span style="COLOR: #800000">version</span><span style="COLOR: #800000">'</span><span style="COLOR: #000000">,&nbsp;</span><span style="COLOR: #800000">'</span><span style="COLOR: #800000">version_info</span><span style="COLOR: #800000">'</span><span style="COLOR: #000000">,&nbsp;</span><span style="COLOR: #800000">'</span><span style="COLOR: #800000">warnoptions</span><span style="COLOR: #800000">'</span><span style="COLOR: #000000">]<br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif"></span><span style="COLOR: #000000">&gt;&gt;&gt;</span><span style="COLOR: #000000">&nbsp;dir()&nbsp;</span><span style="COLOR: #008000">#</span><span style="COLOR: #008000">&nbsp;get&nbsp;list&nbsp;of&nbsp;attributes&nbsp;for&nbsp;current&nbsp;module</span><span style="COLOR: #008000"><br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif"></span><span style="COLOR: #000000">[</span><span style="COLOR: #800000">'</span><span style="COLOR: #800000">__builtins__</span><span style="COLOR: #800000">'</span><span style="COLOR: #000000">,&nbsp;</span><span style="COLOR: #800000">'</span><span style="COLOR: #800000">__doc__</span><span style="COLOR: #800000">'</span><span style="COLOR: #000000">,&nbsp;</span><span style="COLOR: #800000">'</span><span style="COLOR: #800000">__name__</span><span style="COLOR: #800000">'</span><span style="COLOR: #000000">,&nbsp;</span><span style="COLOR: #800000">'</span><span style="COLOR: #800000">sys</span><span style="COLOR: #800000">'</span><span style="COLOR: #000000">]<br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif"></span><span style="COLOR: #000000">&gt;&gt;&gt;</span><span style="COLOR: #000000"><br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif"></span><span style="COLOR: #000000">&gt;&gt;&gt;</span><span style="COLOR: #000000">&nbsp;a&nbsp;</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #000000">5</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #008000">#</span><span style="COLOR: #008000">&nbsp;create&nbsp;a&nbsp;new&nbsp;variable&nbsp;'a'</span><span style="COLOR: #008000"><br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif"></span><span style="COLOR: #000000">&gt;&gt;&gt;</span><span style="COLOR: #000000">&nbsp;dir()<br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif">[</span><span style="COLOR: #800000">'</span><span style="COLOR: #800000">__builtins__</span><span style="COLOR: #800000">'</span><span style="COLOR: #000000">,&nbsp;</span><span style="COLOR: #800000">'</span><span style="COLOR: #800000">__doc__</span><span style="COLOR: #800000">'</span><span style="COLOR: #000000">,&nbsp;</span><span style="COLOR: #800000">'</span><span style="COLOR: #800000">__name__</span><span style="COLOR: #800000">'</span><span style="COLOR: #000000">,&nbsp;</span><span style="COLOR: #800000">'</span><span style="COLOR: #800000">a</span><span style="COLOR: #800000">'</span><span style="COLOR: #000000">,&nbsp;</span><span style="COLOR: #800000">'</span><span style="COLOR: #800000">sys</span><span style="COLOR: #800000">'</span><span style="COLOR: #000000">]<br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif"></span><span style="COLOR: #000000">&gt;&gt;&gt;</span><span style="COLOR: #000000"><br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif"></span><span style="COLOR: #000000">&gt;&gt;&gt;</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #0000ff">del</span><span style="COLOR: #000000">&nbsp;a&nbsp;</span><span style="COLOR: #008000">#</span><span style="COLOR: #008000">&nbsp;delete/remove&nbsp;a&nbsp;name</span><span style="COLOR: #008000"><br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif"></span><span style="COLOR: #000000">&gt;&gt;&gt;</span><span style="COLOR: #000000"><br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif"></span><span style="COLOR: #000000">&gt;&gt;&gt;</span><span style="COLOR: #000000">&nbsp;dir()<br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif">[</span><span style="COLOR: #800000">'</span><span style="COLOR: #800000">__builtins__</span><span style="COLOR: #800000">'</span><span style="COLOR: #000000">,&nbsp;</span><span style="COLOR: #800000">'</span><span style="COLOR: #800000">__doc__</span><span style="COLOR: #800000">'</span><span style="COLOR: #000000">,&nbsp;</span><span style="COLOR: #800000">'</span><span style="COLOR: #800000">__name__</span><span style="COLOR: #800000">'</span><span style="COLOR: #000000">,&nbsp;</span><span style="COLOR: #800000">'</span><span style="COLOR: #800000">sys</span><span style="COLOR: #800000">'</span><span style="COLOR: #000000">]</span></div>
<img src ="http://www.cppblog.com/fenglin/aggbug/125364.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/fenglin/" target="_blank">风林</a> 2010-08-31 10:36 <a href="http://www.cppblog.com/fenglin/articles/125364.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>create module and call module</title><link>http://www.cppblog.com/fenglin/articles/125363.html</link><dc:creator>风林</dc:creator><author>风林</author><pubDate>Tue, 31 Aug 2010 02:31:00 GMT</pubDate><guid>http://www.cppblog.com/fenglin/articles/125363.html</guid><wfw:comment>http://www.cppblog.com/fenglin/comments/125363.html</wfw:comment><comments>http://www.cppblog.com/fenglin/articles/125363.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/fenglin/comments/commentRss/125363.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/fenglin/services/trackbacks/125363.html</trackback:ping><description><![CDATA[<div style="BORDER-BOTTOM: #cccccc 1px solid; BORDER-LEFT: #cccccc 1px solid; PADDING-BOTTOM: 4px; BACKGROUND-COLOR: #eeeeee; PADDING-LEFT: 4px; WIDTH: 98%; PADDING-RIGHT: 5px; FONT-SIZE: 13px; WORD-BREAK: break-all; BORDER-TOP: #cccccc 1px solid; BORDER-RIGHT: #cccccc 1px solid; PADDING-TOP: 4px"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif"><span style="COLOR: #008000">#</span><span style="COLOR: #008000">!/usr/bin/python</span><span style="COLOR: #008000"><br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif">#</span><span style="COLOR: #008000">&nbsp;Filename:&nbsp;mymodule.py</span><span style="COLOR: #008000"><br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif"></span><span style="COLOR: #0000ff">def</span><span style="COLOR: #000000">&nbsp;sayhi():<br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">print</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #800000">'</span><span style="COLOR: #800000">Hi,&nbsp;this&nbsp;is&nbsp;mymodule&nbsp;speaking.</span><span style="COLOR: #800000">'</span><span style="COLOR: #000000"><br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif">version&nbsp;</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #800000">'</span><span style="COLOR: #800000">0.1</span><span style="COLOR: #800000">'</span><span style="COLOR: #000000"><br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif"></span><span style="COLOR: #008000">#</span><span style="COLOR: #008000">&nbsp;End&nbsp;of&nbsp;mymodule.py&nbsp;</span></div>
<span style="COLOR: red"><strong>记住这个模块应该被放置在我们输入它的程序的同一个目录中，或者在sys.path所列目录之一。<br></strong>
<div style="BORDER-BOTTOM: #cccccc 1px solid; BORDER-LEFT: #cccccc 1px solid; PADDING-BOTTOM: 4px; BACKGROUND-COLOR: #eeeeee; PADDING-LEFT: 4px; WIDTH: 98%; PADDING-RIGHT: 5px; FONT-SIZE: 13px; WORD-BREAK: break-all; BORDER-TOP: #cccccc 1px solid; BORDER-RIGHT: #cccccc 1px solid; PADDING-TOP: 4px"><strong><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif"><span style="COLOR: #008000">#</span><span style="COLOR: #008000">!/usr/bin/python</span></strong><span style="COLOR: #008000"><br><strong><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif">#</strong></span><span style="COLOR: #008000"><strong>&nbsp;Filename:&nbsp;mymodule_demo.py</strong></span><span style="COLOR: #008000"><br><strong><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif"></strong></span><span style="COLOR: #0000ff"><strong>import</strong></span><strong><span style="COLOR: #000000">&nbsp;mymodule<br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif">mymodule.sayhi()<br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif"></span><span style="COLOR: #0000ff">print</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #800000">'</span><span style="COLOR: #800000">Version</span><span style="COLOR: #800000">'</span><span style="COLOR: #000000">,&nbsp;mymodule.version</span></strong></div>
</span><font style="COLOR: #000000" color=#ff0000><strong>输出</strong><br>$ python mymodule_demo.py<br>Hi, this is mymodule speaking.<br>Version 0.1 <br><br><strong>下面是一个使用from..import语法的版本。</strong><br>
<div style="BORDER-BOTTOM: #cccccc 1px solid; BORDER-LEFT: #cccccc 1px solid; PADDING-BOTTOM: 4px; BACKGROUND-COLOR: #eeeeee; PADDING-LEFT: 4px; WIDTH: 98%; PADDING-RIGHT: 5px; FONT-SIZE: 13px; WORD-BREAK: break-all; BORDER-TOP: #cccccc 1px solid; BORDER-RIGHT: #cccccc 1px solid; PADDING-TOP: 4px"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif"><span style="COLOR: #008000">#</span><span style="COLOR: #008000">!/usr/bin/python</span><span style="COLOR: #008000"><br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif">#</span><span style="COLOR: #008000">&nbsp;Filename:&nbsp;mymodule_demo2.py</span><span style="COLOR: #008000"><br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif"></span><span style="COLOR: #0000ff">from</span><span style="COLOR: #000000">&nbsp;mymodule&nbsp;</span><span style="COLOR: #0000ff">import</span><span style="COLOR: #000000">&nbsp;sayhi,&nbsp;version<br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif"></span><span style="COLOR: #008000">#</span><span style="COLOR: #008000">&nbsp;Alternative:</span><span style="COLOR: #008000"><br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif">#</span><span style="COLOR: #008000">&nbsp;from&nbsp;mymodule&nbsp;import&nbsp;*</span><span style="COLOR: #008000"><br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif"></span><span style="COLOR: #000000">sayhi()<br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif"></span><span style="COLOR: #0000ff">print</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #800000">'</span><span style="COLOR: #800000">Version</span><span style="COLOR: #800000">'</span><span style="COLOR: #000000">,&nbsp;version&nbsp;</span></div>
</font>
<img src ="http://www.cppblog.com/fenglin/aggbug/125363.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/fenglin/" target="_blank">风林</a> 2010-08-31 10:31 <a href="http://www.cppblog.com/fenglin/articles/125363.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item></channel></rss>