﻿<?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++博客-unixfy</title><link>http://www.cppblog.com/unixfy/</link><description>just do it</description><language>zh-cn</language><lastBuildDate>Thu, 23 Apr 2026 04:12:35 GMT</lastBuildDate><pubDate>Thu, 23 Apr 2026 04:12:35 GMT</pubDate><ttl>60</ttl><item><title>博客重定向至 http://www.cnblogs.com/unixfy</title><link>http://www.cppblog.com/unixfy/archive/2013/09/14/203237.html</link><dc:creator>unixfy</dc:creator><author>unixfy</author><pubDate>Sat, 14 Sep 2013 15:49:00 GMT</pubDate><guid>http://www.cppblog.com/unixfy/archive/2013/09/14/203237.html</guid><wfw:comment>http://www.cppblog.com/unixfy/comments/203237.html</wfw:comment><comments>http://www.cppblog.com/unixfy/archive/2013/09/14/203237.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/unixfy/comments/commentRss/203237.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/unixfy/services/trackbacks/203237.html</trackback:ping><description><![CDATA[<p align="center"><span style="font-size: 36pt"><br /><br /><br />博客重定向至<br /><br /><br /></span><a style="color: " href="http://www.cnblogs.com/unixfy" target="_blank"><span style="font-size: 36pt">http://www.cnblogs.com/unixfy</span></a><br /><br /><br /><br /><br /></p><img src ="http://www.cppblog.com/unixfy/aggbug/203237.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/unixfy/" target="_blank">unixfy</a> 2013-09-14 23:49 <a href="http://www.cppblog.com/unixfy/archive/2013/09/14/203237.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>lower_bound and upper_bound</title><link>http://www.cppblog.com/unixfy/archive/2013/05/30/200701.html</link><dc:creator>unixfy</dc:creator><author>unixfy</author><pubDate>Thu, 30 May 2013 12:55:00 GMT</pubDate><guid>http://www.cppblog.com/unixfy/archive/2013/05/30/200701.html</guid><wfw:comment>http://www.cppblog.com/unixfy/comments/200701.html</wfw:comment><comments>http://www.cppblog.com/unixfy/archive/2013/05/30/200701.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/unixfy/comments/commentRss/200701.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/unixfy/services/trackbacks/200701.html</trackback:ping><description><![CDATA[<div><font style="background-color: #cce8cf">STL&nbsp;algorithm 中的 lower_bound and upper_bound<br /><br />lower_bound 返回的结果 &gt;= 参数 value<br />upper_bound 返回的结果 &gt; 参数 value<br /><br />具体明细可参见百科<br />lower_bound：<a href="http://baike.baidu.cn/view/4720650.htm">http://baike.baidu.cn/view/4720650.htm</a><br />upper_bound：<a href="http://baike.baidu.cn/view/4163451.htm">http://baike.baidu.cn/view/4163451.htm</a><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"><!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />--><span style="color: #008080">&nbsp;1</span>&nbsp;<span style="color: #000000">#include&nbsp;</span><span style="color: #000000">&lt;</span><span style="color: #000000">iostream</span><span style="color: #000000">&gt;</span><span style="color: #000000"><br /></span><span style="color: #008080">&nbsp;2</span>&nbsp;<span style="color: #000000">#include&nbsp;</span><span style="color: #000000">&lt;</span><span style="color: #0000ff">set</span><span style="color: #000000">&gt;</span><span style="color: #000000"><br /></span><span style="color: #008080">&nbsp;3</span>&nbsp;<span style="color: #000000">#include&nbsp;</span><span style="color: #000000">&lt;</span><span style="color: #000000">algorithm</span><span style="color: #000000">&gt;</span><span style="color: #000000"><br /></span><span style="color: #008080">&nbsp;4</span>&nbsp;<span style="color: #000000"></span><span style="color: #0000ff">using</span><span style="color: #000000">&nbsp;</span><span style="color: #0000ff">namespace</span><span style="color: #000000">&nbsp;std;<br /></span><span style="color: #008080">&nbsp;5</span>&nbsp;<span style="color: #000000"><br /></span><span style="color: #008080">&nbsp;6</span>&nbsp;<span style="color: #000000"></span><span style="color: #0000ff">int</span><span style="color: #000000">&nbsp;main()<br /></span><span style="color: #008080">&nbsp;7</span>&nbsp;<span style="color: #000000">{<br /></span><span style="color: #008080">&nbsp;8</span>&nbsp;<span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">set</span><span style="color: #000000">&lt;</span><span style="color: #0000ff">int</span><span style="color: #000000">&gt;</span><span style="color: #000000">&nbsp;si;<br /></span><span style="color: #008080">&nbsp;9</span>&nbsp;<span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #008000">//</span><span style="color: #008000">cout&nbsp;&lt;&lt;&nbsp;si.insert(3)&nbsp;&lt;&lt;&nbsp;endl;</span><span style="color: #008000"><br /></span><span style="color: #008080">10</span>&nbsp;<span style="color: #008000"></span><span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;cout&nbsp;</span><span style="color: #000000">&lt;&lt;</span><span style="color: #000000">&nbsp;</span><span style="color: #000000">*</span><span style="color: #000000">(si.insert(</span><span style="color: #000000">3</span><span style="color: #000000">).first)&nbsp;</span><span style="color: #000000">&lt;&lt;</span><span style="color: #000000">&nbsp;endl;<br /></span><span style="color: #008080">11</span>&nbsp;<span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;<br /></span><span style="color: #008080">12</span>&nbsp;<span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">for</span><span style="color: #000000">&nbsp;(</span><span style="color: #0000ff">int</span><span style="color: #000000">&nbsp;i&nbsp;</span><span style="color: #000000">=</span><span style="color: #000000">&nbsp;</span><span style="color: #000000">1</span><span style="color: #000000">;&nbsp;i&nbsp;</span><span style="color: #000000">&lt;=</span><span style="color: #000000">&nbsp;</span><span style="color: #000000">10</span><span style="color: #000000">;&nbsp;i&nbsp;</span><span style="color: #000000">+=</span><span style="color: #000000">&nbsp;</span><span style="color: #000000">2</span><span style="color: #000000">)<br /></span><span style="color: #008080">13</span>&nbsp;<span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;{<br /></span><span style="color: #008080">14</span>&nbsp;<span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;si.insert(i);<br /></span><span style="color: #008080">15</span>&nbsp;<span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;}<br /></span><span style="color: #008080">16</span>&nbsp;<span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">for</span><span style="color: #000000">&nbsp;(</span><span style="color: #0000ff">set</span><span style="color: #000000">&lt;</span><span style="color: #0000ff">int</span><span style="color: #000000">&gt;</span><span style="color: #000000">::const_iterator&nbsp;cit&nbsp;</span><span style="color: #000000">=</span><span style="color: #000000">&nbsp;si.begin();&nbsp;cit&nbsp;</span><span style="color: #000000">!=</span><span style="color: #000000">&nbsp;si.end();&nbsp;</span><span style="color: #000000">++</span><span style="color: #000000">cit)<br /></span><span style="color: #008080">17</span>&nbsp;<span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;{<br /></span><span style="color: #008080">18</span>&nbsp;<span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;cout&nbsp;</span><span style="color: #000000">&lt;&lt;</span><span style="color: #000000">&nbsp;</span><span style="color: #000000">*</span><span style="color: #000000">cit&nbsp;</span><span style="color: #000000">&lt;&lt;</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">;<br /></span><span style="color: #008080">19</span>&nbsp;<span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;}<br /></span><span style="color: #008080">20</span>&nbsp;<span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;cout&nbsp;</span><span style="color: #000000">&lt;&lt;</span><span style="color: #000000">&nbsp;endl;<br /></span><span style="color: #008080">21</span>&nbsp;<span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;<br /></span><span style="color: #008080">22</span>&nbsp;<span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;cout&nbsp;</span><span style="color: #000000">&lt;&lt;</span><span style="color: #000000">&nbsp;endl;<br /></span><span style="color: #008080">23</span>&nbsp;<span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;cout&nbsp;</span><span style="color: #000000">&lt;&lt;</span><span style="color: #000000">&nbsp;</span><span style="color: #000000">*</span><span style="color: #000000">lower_bound(si.begin(),&nbsp;si.end(),&nbsp;</span><span style="color: #000000">3</span><span style="color: #000000">)&nbsp;</span><span style="color: #000000">&lt;&lt;</span><span style="color: #000000">&nbsp;endl;<br /></span><span style="color: #008080">24</span>&nbsp;<span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;cout&nbsp;</span><span style="color: #000000">&lt;&lt;</span><span style="color: #000000">&nbsp;</span><span style="color: #000000">*</span><span style="color: #000000">lower_bound(si.begin(),&nbsp;si.end(),&nbsp;</span><span style="color: #000000">4</span><span style="color: #000000">)&nbsp;</span><span style="color: #000000">&lt;&lt;</span><span style="color: #000000">&nbsp;endl;<br /></span><span style="color: #008080">25</span>&nbsp;<span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;cout&nbsp;</span><span style="color: #000000">&lt;&lt;</span><span style="color: #000000">&nbsp;</span><span style="color: #000000">*</span><span style="color: #000000">lower_bound(si.begin(),&nbsp;si.end(),&nbsp;</span><span style="color: #000000">5</span><span style="color: #000000">)&nbsp;</span><span style="color: #000000">&lt;&lt;</span><span style="color: #000000">&nbsp;endl;<br /></span><span style="color: #008080">26</span>&nbsp;<span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;cout&nbsp;</span><span style="color: #000000">&lt;&lt;</span><span style="color: #000000">&nbsp;</span><span style="color: #000000">*</span><span style="color: #000000">lower_bound(si.begin(),&nbsp;si.end(),&nbsp;</span><span style="color: #000000">6</span><span style="color: #000000">)&nbsp;</span><span style="color: #000000">&lt;&lt;</span><span style="color: #000000">&nbsp;endl;<br /></span><span style="color: #008080">27</span>&nbsp;<span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;<br /></span><span style="color: #008080">28</span>&nbsp;<span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;cout&nbsp;</span><span style="color: #000000">&lt;&lt;</span><span style="color: #000000">&nbsp;endl;<br /></span><span style="color: #008080">29</span>&nbsp;<span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;cout&nbsp;</span><span style="color: #000000">&lt;&lt;</span><span style="color: #000000">&nbsp;</span><span style="color: #000000">*</span><span style="color: #000000">upper_bound(si.begin(),&nbsp;si.end(),&nbsp;</span><span style="color: #000000">3</span><span style="color: #000000">)&nbsp;</span><span style="color: #000000">&lt;&lt;</span><span style="color: #000000">&nbsp;endl;<br /></span><span style="color: #008080">30</span>&nbsp;<span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;cout&nbsp;</span><span style="color: #000000">&lt;&lt;</span><span style="color: #000000">&nbsp;</span><span style="color: #000000">*</span><span style="color: #000000">upper_bound(si.begin(),&nbsp;si.end(),&nbsp;</span><span style="color: #000000">4</span><span style="color: #000000">)&nbsp;</span><span style="color: #000000">&lt;&lt;</span><span style="color: #000000">&nbsp;endl;<br /></span><span style="color: #008080">31</span>&nbsp;<span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;cout&nbsp;</span><span style="color: #000000">&lt;&lt;</span><span style="color: #000000">&nbsp;</span><span style="color: #000000">*</span><span style="color: #000000">upper_bound(si.begin(),&nbsp;si.end(),&nbsp;</span><span style="color: #000000">5</span><span style="color: #000000">)&nbsp;</span><span style="color: #000000">&lt;&lt;</span><span style="color: #000000">&nbsp;endl;<br /></span><span style="color: #008080">32</span>&nbsp;<span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;cout&nbsp;</span><span style="color: #000000">&lt;&lt;</span><span style="color: #000000">&nbsp;</span><span style="color: #000000">*</span><span style="color: #000000">upper_bound(si.begin(),&nbsp;si.end(),&nbsp;</span><span style="color: #000000">6</span><span style="color: #000000">)&nbsp;</span><span style="color: #000000">&lt;&lt;</span><span style="color: #000000">&nbsp;endl;<br /></span><span style="color: #008080">33</span>&nbsp;<span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;<br /></span><span style="color: #008080">34</span>&nbsp;<span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;system(</span><span style="color: #000000">"</span><span style="color: #000000">PAUSE</span><span style="color: #000000">"</span><span style="color: #000000">);<br /></span><span style="color: #008080">35</span>&nbsp;<span style="color: #000000">}<br /></span><span style="color: #008080">36</span>&nbsp;<span style="color: #000000"></span></div><br />输出：<br />
<p>3<br />1 3 5 7 9</p>
<p>3<br />5<br />5<br />7</p>
<p>5<br />5<br />7<br />7</p><br /></font></div><img src ="http://www.cppblog.com/unixfy/aggbug/200701.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/unixfy/" target="_blank">unixfy</a> 2013-05-30 20:55 <a href="http://www.cppblog.com/unixfy/archive/2013/05/30/200701.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>子类化列表</title><link>http://www.cppblog.com/unixfy/archive/2013/05/21/200459.html</link><dc:creator>unixfy</dc:creator><author>unixfy</author><pubDate>Tue, 21 May 2013 14:14:00 GMT</pubDate><guid>http://www.cppblog.com/unixfy/archive/2013/05/21/200459.html</guid><wfw:comment>http://www.cppblog.com/unixfy/comments/200459.html</wfw:comment><comments>http://www.cppblog.com/unixfy/archive/2013/05/21/200459.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/unixfy/comments/commentRss/200459.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/unixfy/services/trackbacks/200459.html</trackback:ping><description><![CDATA[<div style="font-size: 13px; border-top: #cccccc 1px solid; border-right: #cccccc 1px solid; border-bottom: #cccccc 1px solid; word-break: break-all; padding-bottom: 4px; padding-top: 4px; padding-left: 4px; border-left: #cccccc 1px solid; padding-right: 5px; width: 98%; background-color: #eeeeee"><!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />--><span style="color: #008080">&nbsp;1</span>&nbsp;<span style="color: #0000ff">class</span><span style="color: #000000">&nbsp;CounterList(list):<br /></span><span style="color: #008080">&nbsp;2</span>&nbsp;<span style="color: #000000">&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;</span><span style="color: #000000">*</span><span style="color: #000000">args):<br /></span><span style="color: #008080">&nbsp;3</span>&nbsp;<span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;super(CounterList,&nbsp;self).</span><span style="color: #800080">__init__</span><span style="color: #000000">(</span><span style="color: #000000">*</span><span style="color: #000000">args)<br /></span><span style="color: #008080">&nbsp;4</span>&nbsp;<span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;self.counter&nbsp;</span><span style="color: #000000">=</span><span style="color: #000000">&nbsp;0<br /></span><span style="color: #008080">&nbsp;5</span>&nbsp;<span style="color: #000000"><br /></span><span style="color: #008080">&nbsp;6</span>&nbsp;<span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">def</span><span style="color: #000000">&nbsp;</span><span style="color: #800080">__getitem__</span><span style="color: #000000">(self,&nbsp;index):<br /></span><span style="color: #008080">&nbsp;7</span>&nbsp;<span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;self.counter&nbsp;</span><span style="color: #000000">+=</span><span style="color: #000000">&nbsp;</span><span style="color: #000000">1</span><span style="color: #000000"><br /></span><span style="color: #008080">&nbsp;8</span>&nbsp;<span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">return</span><span style="color: #000000">&nbsp;super(CounterList,&nbsp;self).</span><span style="color: #800080">__getitem__</span><span style="color: #000000">(index)<br /></span><span style="color: #008080">&nbsp;9</span>&nbsp;<span style="color: #000000"><br /></span><span style="color: #008080">10</span>&nbsp;<span style="color: #000000"><br /></span><span style="color: #008080">11</span>&nbsp;<span style="color: #000000">cl&nbsp;</span><span style="color: #000000">=</span><span style="color: #000000">&nbsp;CounterList(range(</span><span style="color: #000000">10</span><span style="color: #000000">))<br /></span><span style="color: #008080">12</span>&nbsp;<span style="color: #000000"></span><span style="color: #0000ff">print</span><span style="color: #000000">(cl)<br /></span><span style="color: #008080">13</span>&nbsp;<span style="color: #000000">cl.reverse()<br /></span><span style="color: #008080">14</span>&nbsp;<span style="color: #000000"></span><span style="color: #0000ff">print</span><span style="color: #000000">(cl)<br /></span><span style="color: #008080">15</span>&nbsp;<span style="color: #000000"></span><span style="color: #0000ff">del</span><span style="color: #000000">(cl[</span><span style="color: #000000">3</span><span style="color: #000000">:</span><span style="color: #000000">6</span><span style="color: #000000">])<br /></span><span style="color: #008080">16</span>&nbsp;<span style="color: #000000"></span><span style="color: #0000ff">print</span><span style="color: #000000">(cl)<br /></span><span style="color: #008080">17</span>&nbsp;<span style="color: #000000"></span><span style="color: #0000ff">print</span><span style="color: #000000">(cl.counter)<br /></span><span style="color: #008080">18</span>&nbsp;<span style="color: #000000">cl[</span><span style="color: #000000">4</span><span style="color: #000000">]&nbsp;</span><span style="color: #000000">+</span><span style="color: #000000">&nbsp;cl[</span><span style="color: #000000">2</span><span style="color: #000000">]<br /></span><span style="color: #008080">19</span>&nbsp;<span style="color: #000000"></span><span style="color: #0000ff">print</span><span style="color: #000000">(cl.counter)</span></div><br />输出：<br />&gt;&gt;&gt; <br />[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]<br />[9, 8, 7, 6, 5, 4, 3, 2, 1, 0]<br />[9, 8, 7, 3, 2, 1, 0]<br />0<br />2<img src ="http://www.cppblog.com/unixfy/aggbug/200459.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/unixfy/" target="_blank">unixfy</a> 2013-05-21 22:14 <a href="http://www.cppblog.com/unixfy/archive/2013/05/21/200459.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>实现一个无穷序列</title><link>http://www.cppblog.com/unixfy/archive/2013/05/21/200457.html</link><dc:creator>unixfy</dc:creator><author>unixfy</author><pubDate>Tue, 21 May 2013 14:03:00 GMT</pubDate><guid>http://www.cppblog.com/unixfy/archive/2013/05/21/200457.html</guid><wfw:comment>http://www.cppblog.com/unixfy/comments/200457.html</wfw:comment><comments>http://www.cppblog.com/unixfy/archive/2013/05/21/200457.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/unixfy/comments/commentRss/200457.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/unixfy/services/trackbacks/200457.html</trackback:ping><description><![CDATA[<div style="font-size: 13px; border-top: #cccccc 1px solid; border-right: #cccccc 1px solid; border-bottom: #cccccc 1px solid; word-break: break-all; padding-bottom: 4px; padding-top: 4px; padding-left: 4px; border-left: #cccccc 1px solid; padding-right: 5px; width: 98%; background-color: #eeeeee"><!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />--><span style="color: #008080">&nbsp;1</span>&nbsp;<span style="color: #0000ff">def</span><span style="color: #000000">&nbsp;checkIndex(key):<br /></span><span style="color: #008080">&nbsp;2</span>&nbsp;<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;isinstance(key,&nbsp;(int,)):<br /></span><span style="color: #008080">&nbsp;3</span>&nbsp;<span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">raise</span><span style="color: #000000">&nbsp;TypeError<br /></span><span style="color: #008080">&nbsp;4</span>&nbsp;<span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">if</span><span style="color: #000000">&nbsp;key&nbsp;</span><span style="color: #000000">&lt;</span><span style="color: #000000">&nbsp;0:<br /></span><span style="color: #008080">&nbsp;5</span>&nbsp;<span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">raise</span><span style="color: #000000">&nbsp;IndexError<br /></span><span style="color: #008080">&nbsp;6</span>&nbsp;<span style="color: #000000"><br /></span><span style="color: #008080">&nbsp;7</span>&nbsp;<span style="color: #000000"></span><span style="color: #0000ff">class</span><span style="color: #000000">&nbsp;ArithmeticSequence:<br /></span><span style="color: #008080">&nbsp;8</span>&nbsp;<span style="color: #000000">&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;start&nbsp;</span><span style="color: #000000">=</span><span style="color: #000000">&nbsp;0,&nbsp;step&nbsp;</span><span style="color: #000000">=</span><span style="color: #000000">&nbsp;</span><span style="color: #000000">1</span><span style="color: #000000">):<br /></span><span style="color: #008080">&nbsp;9</span>&nbsp;<span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;self.start&nbsp;</span><span style="color: #000000">=</span><span style="color: #000000">&nbsp;start<br /></span><span style="color: #008080">10</span>&nbsp;<span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;self.step&nbsp;&nbsp;</span><span style="color: #000000">=</span><span style="color: #000000">&nbsp;step<br /></span><span style="color: #008080">11</span>&nbsp;<span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;self.changed&nbsp;</span><span style="color: #000000">=</span><span style="color: #000000">&nbsp;{}<br /></span><span style="color: #008080">12</span>&nbsp;<span style="color: #000000"><br /></span><span style="color: #008080">13</span>&nbsp;<span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">def</span><span style="color: #000000">&nbsp;</span><span style="color: #800080">__getitem__</span><span style="color: #000000">(self,&nbsp;key):<br /></span><span style="color: #008080">14</span>&nbsp;<span style="color: #000000"><br /></span><span style="color: #008080">15</span>&nbsp;<span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;checkIndex(key)<br /></span><span style="color: #008080">16</span>&nbsp;<span style="color: #000000"><br /></span><span style="color: #008080">17</span>&nbsp;<span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">print</span><span style="color: #000000">(</span><span style="color: #800000">'</span><span style="color: #800000">__getitem__</span><span style="color: #800000">'</span><span style="color: #000000">)<br /></span><span style="color: #008080">18</span>&nbsp;<span style="color: #000000"><br /></span><span style="color: #008080">19</span>&nbsp;<span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">try</span><span style="color: #000000">:<br /></span><span style="color: #008080">20</span>&nbsp;<span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">return</span><span style="color: #000000">&nbsp;self.changed[key]<br /></span><span style="color: #008080">21</span>&nbsp;<span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">except</span><span style="color: #000000">&nbsp;KeyError:<br /></span><span style="color: #008080">22</span>&nbsp;<span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">return</span><span style="color: #000000">&nbsp;self.start&nbsp;</span><span style="color: #000000">+</span><span style="color: #000000">&nbsp;key&nbsp;</span><span style="color: #000000">*</span><span style="color: #000000">&nbsp;self.step<br /></span><span style="color: #008080">23</span>&nbsp;<span style="color: #000000"><br /></span><span style="color: #008080">24</span>&nbsp;<span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">def</span><span style="color: #000000">&nbsp;</span><span style="color: #800080">__setitem__</span><span style="color: #000000">(self,&nbsp;key,&nbsp;value):<br /></span><span style="color: #008080">25</span>&nbsp;<span style="color: #000000"><br /></span><span style="color: #008080">26</span>&nbsp;<span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;checkIndex(key)<br /></span><span style="color: #008080">27</span>&nbsp;<span style="color: #000000"><br /></span><span style="color: #008080">28</span>&nbsp;<span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">print</span><span style="color: #000000">(</span><span style="color: #800000">'</span><span style="color: #800000">__setitem__</span><span style="color: #800000">'</span><span style="color: #000000">)<br /></span><span style="color: #008080">29</span>&nbsp;<span style="color: #000000"><br /></span><span style="color: #008080">30</span>&nbsp;<span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;self.changed[key]&nbsp;</span><span style="color: #000000">=</span><span style="color: #000000">&nbsp;value<br /></span><span style="color: #008080">31</span>&nbsp;<span style="color: #000000"><br /></span><span style="color: #008080">32</span>&nbsp;<span style="color: #000000"><br /></span><span style="color: #008080">33</span>&nbsp;<span style="color: #000000">s&nbsp;</span><span style="color: #000000">=</span><span style="color: #000000">&nbsp;ArithmeticSequence(</span><span style="color: #000000">1</span><span style="color: #000000">,&nbsp;</span><span style="color: #000000">2</span><span style="color: #000000">)<br /></span><span style="color: #008080">34</span>&nbsp;<span style="color: #000000"></span><span style="color: #0000ff">print</span><span style="color: #000000">(s[</span><span style="color: #000000">4</span><span style="color: #000000">])<br /></span><span style="color: #008080">35</span>&nbsp;<span style="color: #000000">s[</span><span style="color: #000000">4</span><span style="color: #000000">]&nbsp;</span><span style="color: #000000">=</span><span style="color: #000000">&nbsp;</span><span style="color: #000000">2</span><span style="color: #000000"><br /></span><span style="color: #008080">36</span>&nbsp;<span style="color: #000000"></span><span style="color: #0000ff">print</span><span style="color: #000000">(s[</span><span style="color: #000000">4</span><span style="color: #000000">])<br /></span><span style="color: #008080">37</span>&nbsp;<span style="color: #000000"></span><span style="color: #0000ff">print</span><span style="color: #000000">(s[</span><span style="color: #000000">5</span><span style="color: #000000">])<br /></span><span style="color: #008080">38</span>&nbsp;<span style="color: #000000">s[</span><span style="color: #000000">3</span><span style="color: #000000">]&nbsp;</span><span style="color: #000000">=</span><span style="color: #000000">&nbsp;</span><span style="color: #000000">100</span><span style="color: #000000"><br /></span><span style="color: #008080">39</span>&nbsp;<span style="color: #000000"></span><span style="color: #0000ff">print</span><span style="color: #000000">(s[</span><span style="color: #000000">3</span><span style="color: #000000">])<br /></span><span style="color: #008080">40</span>&nbsp;<span style="color: #000000">s.</span><span style="color: #800080">__setitem__</span><span style="color: #000000">(</span><span style="color: #000000">100</span><span style="color: #000000">,&nbsp;</span><span style="color: #000000">3</span><span style="color: #000000">)<br /></span><span style="color: #008080">41</span>&nbsp;<span style="color: #000000"></span><span style="color: #0000ff">print</span><span style="color: #000000">(s.</span><span style="color: #800080">__getitem__</span><span style="color: #000000">(</span><span style="color: #000000">100</span><span style="color: #000000">))</span></div><br />输出：<br />&gt;&gt;&gt; <br />__getitem__<br />9<br />__setitem__<br />__getitem__<br />2<br />__getitem__<br />11<br />__setitem__<br />__getitem__<br />100<br />__setitem__<br />__getitem__<br />3<img src ="http://www.cppblog.com/unixfy/aggbug/200457.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/unixfy/" target="_blank">unixfy</a> 2013-05-21 22:03 <a href="http://www.cppblog.com/unixfy/archive/2013/05/21/200457.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>Python 继承-实现一个过滤器</title><link>http://www.cppblog.com/unixfy/archive/2013/05/19/200391.html</link><dc:creator>unixfy</dc:creator><author>unixfy</author><pubDate>Sun, 19 May 2013 03:39:00 GMT</pubDate><guid>http://www.cppblog.com/unixfy/archive/2013/05/19/200391.html</guid><wfw:comment>http://www.cppblog.com/unixfy/comments/200391.html</wfw:comment><comments>http://www.cppblog.com/unixfy/archive/2013/05/19/200391.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/unixfy/comments/commentRss/200391.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/unixfy/services/trackbacks/200391.html</trackback:ping><description><![CDATA[<div style="font-size: 13px; border-top: #cccccc 1px solid; border-right: #cccccc 1px solid; border-bottom: #cccccc 1px solid; word-break: break-all; padding-bottom: 4px; padding-top: 4px; padding-left: 4px; border-left: #cccccc 1px solid; padding-right: 5px; width: 98%; background-color: #eeeeee"><!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />--><span style="color: #008080">&nbsp;1</span>&nbsp;<span style="color: #0000ff">class</span><span style="color: #000000">&nbsp;Filter:<br /></span><span style="color: #008080">&nbsp;2</span>&nbsp;<span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">def</span><span style="color: #000000">&nbsp;init(self):<br /></span><span style="color: #008080">&nbsp;3</span>&nbsp;<span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;self.blocked&nbsp;</span><span style="color: #000000">=</span><span style="color: #000000">&nbsp;[]<br /></span><span style="color: #008080">&nbsp;4</span>&nbsp;<span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">def</span><span style="color: #000000">&nbsp;filter(self,&nbsp;sequence):<br /></span><span style="color: #008080">&nbsp;5</span>&nbsp;<span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">return</span><span style="color: #000000">&nbsp;[x&nbsp;</span><span style="color: #0000ff">for</span><span style="color: #000000">&nbsp;x&nbsp;</span><span style="color: #0000ff">in</span><span style="color: #000000">&nbsp;sequence&nbsp;</span><span style="color: #0000ff">if</span><span style="color: #000000">&nbsp;x&nbsp;</span><span style="color: #0000ff">not</span><span style="color: #000000">&nbsp;</span><span style="color: #0000ff">in</span><span style="color: #000000">&nbsp;self.blocked]<br /></span><span style="color: #008080">&nbsp;6</span>&nbsp;<span style="color: #000000"><br /></span><span style="color: #008080">&nbsp;7</span>&nbsp;<span style="color: #000000"></span><span style="color: #0000ff">class</span><span style="color: #000000">&nbsp;SPAMFilter(Filter):&nbsp;</span><span style="color: #008000">#</span><span style="color: #008000">&nbsp;SPAMFilter&nbsp;是&nbsp;Filter&nbsp;的子类</span><span style="color: #008000"><br /></span><span style="color: #008080">&nbsp;8</span>&nbsp;<span style="color: #008000"></span><span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">def</span><span style="color: #000000">&nbsp;init(self):<br /></span><span style="color: #008080">&nbsp;9</span>&nbsp;<span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;self.blocked&nbsp;</span><span style="color: #000000">=</span><span style="color: #000000">&nbsp;[</span><span style="color: #800000">'</span><span style="color: #800000">SPAM</span><span style="color: #800000">'</span><span style="color: #000000">]<br /></span><span style="color: #008080">10</span>&nbsp;<span style="color: #000000"><br /></span><span style="color: #008080">11</span>&nbsp;<span style="color: #000000">f&nbsp;</span><span style="color: #000000">=</span><span style="color: #000000">&nbsp;Filter()<br /></span><span style="color: #008080">12</span>&nbsp;<span style="color: #000000">f.init()<br /></span><span style="color: #008080">13</span>&nbsp;<span style="color: #000000"></span><span style="color: #0000ff">print</span><span style="color: #000000">(f.filter([</span><span style="color: #000000">1</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">]))<br /></span><span style="color: #008080">14</span>&nbsp;<span style="color: #000000"><br /></span><span style="color: #008080">15</span>&nbsp;<span style="color: #000000">s&nbsp;</span><span style="color: #000000">=</span><span style="color: #000000">&nbsp;SPAMFilter()<br /></span><span style="color: #008080">16</span>&nbsp;<span style="color: #000000">s.init()<br /></span><span style="color: #008080">17</span>&nbsp;<span style="color: #000000"></span><span style="color: #0000ff">print</span><span style="color: #000000">(s.filter([</span><span style="color: #800000">'</span><span style="color: #800000">SPAM</span><span style="color: #800000">'</span><span style="color: #000000">,&nbsp;</span><span style="color: #800000">'</span><span style="color: #800000">SPAM</span><span style="color: #800000">'</span><span style="color: #000000">,&nbsp;</span><span style="color: #800000">'</span><span style="color: #800000">SPAM</span><span style="color: #800000">'</span><span style="color: #000000">,&nbsp;</span><span style="color: #800000">'</span><span style="color: #800000">SPAM</span><span style="color: #800000">'</span><span style="color: #000000">,&nbsp;</span><span style="color: #800000">'</span><span style="color: #800000">eggs</span><span style="color: #800000">'</span><span style="color: #000000">,&nbsp;</span><span style="color: #800000">'</span><span style="color: #800000">bacon</span><span style="color: #800000">'</span><span style="color: #000000">,&nbsp;</span><span style="color: #800000">'</span><span style="color: #800000">SPAM</span><span style="color: #800000">'</span><span style="color: #000000">]))</span></div><br />输出：<br />&gt;&gt;&gt; <br />[1, 2, 3]<br />['eggs', 'bacon']<img src ="http://www.cppblog.com/unixfy/aggbug/200391.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/unixfy/" target="_blank">unixfy</a> 2013-05-19 11:39 <a href="http://www.cppblog.com/unixfy/archive/2013/05/19/200391.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>Python 中类的静态成员与普通成员</title><link>http://www.cppblog.com/unixfy/archive/2013/05/18/200381.html</link><dc:creator>unixfy</dc:creator><author>unixfy</author><pubDate>Sat, 18 May 2013 08:43:00 GMT</pubDate><guid>http://www.cppblog.com/unixfy/archive/2013/05/18/200381.html</guid><wfw:comment>http://www.cppblog.com/unixfy/comments/200381.html</wfw:comment><comments>http://www.cppblog.com/unixfy/archive/2013/05/18/200381.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/unixfy/comments/commentRss/200381.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/unixfy/services/trackbacks/200381.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"><!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />--><span style="color: #008080">&nbsp;1</span>&nbsp;<span style="color: #0000ff">class</span><span style="color: #000000">&nbsp;MemberCounter:<br /></span><span style="color: #008080">&nbsp;2</span>&nbsp;<span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;member&nbsp;</span><span style="color: #000000">=</span><span style="color: #000000">&nbsp;0<br /></span><span style="color: #008080">&nbsp;3</span>&nbsp;<span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">def</span><span style="color: #000000">&nbsp;init(self):<br /></span><span style="color: #008080">&nbsp;4</span>&nbsp;<span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;self.member&nbsp;</span><span style="color: #000000">+=</span><span style="color: #000000">&nbsp;</span><span style="color: #000000">1</span><span style="color: #000000"><br /></span><span style="color: #008080">&nbsp;5</span>&nbsp;<span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;MemberCounter.member&nbsp;</span><span style="color: #000000">+=</span><span style="color: #000000">&nbsp;</span><span style="color: #000000">1</span><span style="color: #000000"><br /></span><span style="color: #008080">&nbsp;6</span>&nbsp;<span style="color: #000000"><br /></span><span style="color: #008080">&nbsp;7</span>&nbsp;<span style="color: #000000">m1&nbsp;</span><span style="color: #000000">=</span><span style="color: #000000">&nbsp;MemberCounter()<br /></span><span style="color: #008080">&nbsp;8</span>&nbsp;<span style="color: #000000">m1.init()<br /></span><span style="color: #008080">&nbsp;9</span>&nbsp;<span style="color: #000000"></span><span style="color: #0000ff">print</span><span style="color: #000000">(m1.member)<br /></span><span style="color: #008080">10</span>&nbsp;<span style="color: #000000"></span><span style="color: #0000ff">print</span><span style="color: #000000">(MemberCounter.member)<br /></span><span style="color: #008080">11</span>&nbsp;<span style="color: #000000"><br /></span><span style="color: #008080">12</span>&nbsp;<span style="color: #000000">m2&nbsp;</span><span style="color: #000000">=</span><span style="color: #000000">&nbsp;MemberCounter()<br /></span><span style="color: #008080">13</span>&nbsp;<span style="color: #000000">m2.init()<br /></span><span style="color: #008080">14</span>&nbsp;<span style="color: #000000"></span><span style="color: #0000ff">print</span><span style="color: #000000">(m2.member)<br /></span><span style="color: #008080">15</span>&nbsp;<span style="color: #000000"></span><span style="color: #0000ff">print</span><span style="color: #000000">(MemberCounter.member)<br /></span><span style="color: #008080">16</span>&nbsp;<span style="color: #000000"><br /></span><span style="color: #008080">17</span>&nbsp;<span style="color: #000000"></span><span style="color: #0000ff">print</span><span style="color: #000000">(m1.member&nbsp;</span><span style="color: #0000ff">is</span><span style="color: #000000">&nbsp;m2.member)<br /></span><span style="color: #008080">18</span>&nbsp;<span style="color: #000000"></span><span style="color: #0000ff">print</span><span style="color: #000000">(m1.member&nbsp;</span><span style="color: #0000ff">is</span><span style="color: #000000">&nbsp;MemberCounter.member)<br /></span><span style="color: #008080">19</span>&nbsp;<span style="color: #000000"></span><span style="color: #0000ff">print</span><span style="color: #000000">(m2.member&nbsp;</span><span style="color: #0000ff">is</span><span style="color: #000000">&nbsp;MemberCounter.member)<br /></span><span style="color: #008080">20</span>&nbsp;<span style="color: #000000"><br /></span><span style="color: #008080">21</span>&nbsp;<span style="color: #000000">m3&nbsp;</span><span style="color: #000000">=</span><span style="color: #000000">&nbsp;MemberCounter()<br /></span><span style="color: #008080">22</span>&nbsp;<span style="color: #000000">m3.init()<br /></span><span style="color: #008080">23</span>&nbsp;<span style="color: #000000"></span><span style="color: #0000ff">print</span><span style="color: #000000">(m3.member)<br /></span><span style="color: #008080">24</span>&nbsp;<span style="color: #000000"></span><span style="color: #0000ff">print</span><span style="color: #000000">(MemberCounter.member)<br /></span><span style="color: #008080">25</span>&nbsp;<span style="color: #000000"></span><span style="color: #0000ff">print</span><span style="color: #000000">(m2.member&nbsp;</span><span style="color: #0000ff">is</span><span style="color: #000000">&nbsp;MemberCounter.member)<br /></span><span style="color: #008080">26</span>&nbsp;<span style="color: #000000"><br /></span><span style="color: #008080">27</span>&nbsp;<span style="color: #000000"></span><span style="color: #0000ff">print</span><span style="color: #000000">(m1.member)<br /></span><span style="color: #008080">28</span>&nbsp;<span style="color: #000000"></span><span style="color: #0000ff">print</span><span style="color: #000000">(MemberCounter.member)</span></div>
<div><br />输出：<br />&gt;&gt;&gt; <br />1<br />1<br />2<br />2<br />False<br />False<br />True<br />3<br />3<br />False<br />1<br />3</div><img src ="http://www.cppblog.com/unixfy/aggbug/200381.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/unixfy/" target="_blank">unixfy</a> 2013-05-18 16:43 <a href="http://www.cppblog.com/unixfy/archive/2013/05/18/200381.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>Python 私有成员的访问</title><link>http://www.cppblog.com/unixfy/archive/2013/05/18/200380.html</link><dc:creator>unixfy</dc:creator><author>unixfy</author><pubDate>Sat, 18 May 2013 08:31:00 GMT</pubDate><guid>http://www.cppblog.com/unixfy/archive/2013/05/18/200380.html</guid><wfw:comment>http://www.cppblog.com/unixfy/comments/200380.html</wfw:comment><comments>http://www.cppblog.com/unixfy/archive/2013/05/18/200380.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/unixfy/comments/commentRss/200380.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/unixfy/services/trackbacks/200380.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"><!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />--><span style="color: #008080">&nbsp;1</span>&nbsp;<span style="color: #800080">__metaclass__</span><span style="color: #000000">&nbsp;</span><span style="color: #000000">=</span><span style="color: #000000">&nbsp;type&nbsp;</span><span style="color: #008000">#</span><span style="color: #008000">&nbsp;确定使用新式类</span><span style="color: #008000"><br /></span><span style="color: #008080">&nbsp;2</span>&nbsp;<span style="color: #008000"></span><span style="color: #000000"><br /></span><span style="color: #008080">&nbsp;3</span>&nbsp;<span style="color: #000000"></span><span style="color: #0000ff">class</span><span style="color: #000000">&nbsp;Person:<br /></span><span style="color: #008080">&nbsp;4</span>&nbsp;<span style="color: #000000"><br /></span><span style="color: #008080">&nbsp;5</span>&nbsp;<span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">def</span><span style="color: #000000">&nbsp;setName(self,&nbsp;name):<br /></span><span style="color: #008080">&nbsp;6</span>&nbsp;<span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;self.name&nbsp;</span><span style="color: #000000">=</span><span style="color: #000000">&nbsp;name<br /></span><span style="color: #008080">&nbsp;7</span>&nbsp;<span style="color: #000000"><br /></span><span style="color: #008080">&nbsp;8</span>&nbsp;<span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">def</span><span style="color: #000000">&nbsp;getName(self):<br /></span><span style="color: #008080">&nbsp;9</span>&nbsp;<span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">return</span><span style="color: #000000">&nbsp;self.name<br /></span><span style="color: #008080">10</span>&nbsp;<span style="color: #000000"><br /></span><span style="color: #008080">11</span>&nbsp;<span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">def</span><span style="color: #000000">&nbsp;greet(self):<br /></span><span style="color: #008080">12</span>&nbsp;<span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">print</span><span style="color: #000000">(</span><span style="color: #800000">"</span><span style="color: #800000">Hello,&nbsp;world!&nbsp;I'm&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 /></span><span style="color: #008080">13</span>&nbsp;<span style="color: #000000"><br /></span><span style="color: #008080">14</span>&nbsp;<span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">def</span><span style="color: #000000">&nbsp;</span><span style="color: #800080">__foo</span><span style="color: #000000">(self):<br /></span><span style="color: #008080">15</span>&nbsp;<span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">print</span><span style="color: #000000">(</span><span style="color: #800000">"</span><span style="color: #800000">FOO</span><span style="color: #800000">"</span><span style="color: #000000">,&nbsp;self.name)<br /></span><span style="color: #008080">16</span>&nbsp;<span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">def</span><span style="color: #000000">&nbsp;_bar(self):<br /></span><span style="color: #008080">17</span>&nbsp;<span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">print</span><span style="color: #000000">(</span><span style="color: #800000">"</span><span style="color: #800000">BAR</span><span style="color: #800000">"</span><span style="color: #000000">,&nbsp;self.name)<br /></span><span style="color: #008080">18</span>&nbsp;<span style="color: #000000"><br /></span><span style="color: #008080">19</span>&nbsp;<span style="color: #000000"><br /></span><span style="color: #008080">20</span>&nbsp;<span style="color: #000000">foo&nbsp;</span><span style="color: #000000">=</span><span style="color: #000000">&nbsp;Person()<br /></span><span style="color: #008080">21</span>&nbsp;<span style="color: #000000">bar&nbsp;</span><span style="color: #000000">=</span><span style="color: #000000">&nbsp;Person()<br /></span><span style="color: #008080">22</span>&nbsp;<span style="color: #000000">foo.setName(</span><span style="color: #800000">'</span><span style="color: #800000">Luke&nbsp;Skywalker</span><span style="color: #800000">'</span><span style="color: #000000">)<br /></span><span style="color: #008080">23</span>&nbsp;<span style="color: #000000">bar.setName(</span><span style="color: #800000">'</span><span style="color: #800000">Anakin&nbsp;Skywalker</span><span style="color: #800000">'</span><span style="color: #000000">)<br /></span><span style="color: #008080">24</span>&nbsp;<span style="color: #000000">foo.greet()<br /></span><span style="color: #008080">25</span>&nbsp;<span style="color: #000000">bar.greet()<br /></span><span style="color: #008080">26</span>&nbsp;<span style="color: #000000">Person.greet(bar)<br /></span><span style="color: #008080">27</span>&nbsp;<span style="color: #000000"><br /></span><span style="color: #008080">28</span>&nbsp;<span style="color: #000000"></span><span style="color: #008000">#</span><span style="color: #008000">&nbsp;foo.__foo()</span><span style="color: #008000"><br /></span><span style="color: #008080">29</span>&nbsp;<span style="color: #008000"></span><span style="color: #000000">foo._Person__foo()<br /></span><span style="color: #008080">30</span>&nbsp;<span style="color: #000000">foo._bar()<br /></span><span style="color: #008080">31</span>&nbsp;<span style="color: #000000"></span><span style="color: #008000">#</span><span style="color: #008000">&nbsp;foo._Person_bar()</span></div><br />输出：<br />&gt;&gt;&gt; <br />Hello, world! I'm Luke Skywalker.<br />Hello, world! I'm Anakin Skywalker.<br />Hello, world! I'm Anakin Skywalker.<br />FOO Luke Skywalker<br />BAR Luke Skywalker<img src ="http://www.cppblog.com/unixfy/aggbug/200380.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/unixfy/" target="_blank">unixfy</a> 2013-05-18 16:31 <a href="http://www.cppblog.com/unixfy/archive/2013/05/18/200380.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>自定义 Python range 函数 2</title><link>http://www.cppblog.com/unixfy/archive/2013/05/18/200375.html</link><dc:creator>unixfy</dc:creator><author>unixfy</author><pubDate>Sat, 18 May 2013 06:32:00 GMT</pubDate><guid>http://www.cppblog.com/unixfy/archive/2013/05/18/200375.html</guid><wfw:comment>http://www.cppblog.com/unixfy/comments/200375.html</wfw:comment><comments>http://www.cppblog.com/unixfy/archive/2013/05/18/200375.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/unixfy/comments/commentRss/200375.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/unixfy/services/trackbacks/200375.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"><!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />--><span style="color: #008080">&nbsp;1</span>&nbsp;<span style="color: #0000ff">def</span><span style="color: #000000">&nbsp;my_range_2(start,&nbsp;stop&nbsp;</span><span style="color: #000000">=</span><span style="color: #000000">&nbsp;None,&nbsp;step&nbsp;</span><span style="color: #000000">=</span><span style="color: #000000">&nbsp;</span><span style="color: #000000">1</span><span style="color: #000000">):<br /></span><span style="color: #008080">&nbsp;2</span>&nbsp;<span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">if</span><span style="color: #000000">&nbsp;stop&nbsp;</span><span style="color: #000000">==</span><span style="color: #000000">&nbsp;None:<br /></span><span style="color: #008080">&nbsp;3</span>&nbsp;<span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">if</span><span style="color: #000000">&nbsp;start&nbsp;</span><span style="color: #000000">&gt;</span><span style="color: #000000">&nbsp;0:<br /></span><span style="color: #008080">&nbsp;4</span>&nbsp;<span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;start,&nbsp;stop&nbsp;</span><span style="color: #000000">=</span><span style="color: #000000">&nbsp;0,&nbsp;start<br /></span><span style="color: #008080">&nbsp;5</span>&nbsp;<span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">else</span><span style="color: #000000">:<br /></span><span style="color: #008080">&nbsp;6</span>&nbsp;<span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;start,&nbsp;stop&nbsp;</span><span style="color: #000000">=</span><span style="color: #000000">&nbsp;start,&nbsp;0<br /></span><span style="color: #008080">&nbsp;7</span>&nbsp;<span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;result&nbsp;</span><span style="color: #000000">=</span><span style="color: #000000">&nbsp;[]<br /></span><span style="color: #008080">&nbsp;8</span>&nbsp;<span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">if</span><span style="color: #000000">&nbsp;step&nbsp;</span><span style="color: #000000">&gt;</span><span style="color: #000000">&nbsp;0&nbsp;</span><span style="color: #0000ff">and</span><span style="color: #000000">&nbsp;start&nbsp;</span><span style="color: #000000">&lt;</span><span style="color: #000000">&nbsp;stop:<br /></span><span style="color: #008080">&nbsp;9</span>&nbsp;<span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;i&nbsp;</span><span style="color: #000000">=</span><span style="color: #000000">&nbsp;start<br /></span><span style="color: #008080">10</span>&nbsp;<span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">while</span><span style="color: #000000">&nbsp;(i&nbsp;</span><span style="color: #000000">&lt;</span><span style="color: #000000">&nbsp;stop):<br /></span><span style="color: #008080">11</span>&nbsp;<span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;result.append(i)<br /></span><span style="color: #008080">12</span>&nbsp;<span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;i&nbsp;</span><span style="color: #000000">+=</span><span style="color: #000000">&nbsp;step<br /></span><span style="color: #008080">13</span>&nbsp;<span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">elif</span><span style="color: #000000">&nbsp;step&nbsp;</span><span style="color: #000000">&lt;</span><span style="color: #000000">&nbsp;0&nbsp;</span><span style="color: #0000ff">and</span><span style="color: #000000">&nbsp;start&nbsp;</span><span style="color: #000000">&gt;</span><span style="color: #000000">&nbsp;stop:<br /></span><span style="color: #008080">14</span>&nbsp;<span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;i&nbsp;</span><span style="color: #000000">=</span><span style="color: #000000">&nbsp;start<br /></span><span style="color: #008080">15</span>&nbsp;<span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">while</span><span style="color: #000000">&nbsp;i&nbsp;</span><span style="color: #000000">&gt;</span><span style="color: #000000">&nbsp;stop:<br /></span><span style="color: #008080">16</span>&nbsp;<span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;result.append(i)<br /></span><span style="color: #008080">17</span>&nbsp;<span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;i&nbsp;</span><span style="color: #000000">+=</span><span style="color: #000000">&nbsp;step<br /></span><span style="color: #008080">18</span>&nbsp;<span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">return</span><span style="color: #000000">&nbsp;result<br /></span><span style="color: #008080">19</span>&nbsp;<span style="color: #000000"><br /></span><span style="color: #008080">20</span>&nbsp;<span style="color: #000000"></span><span style="color: #0000ff">print</span><span style="color: #000000">(my_range_2(</span><span style="color: #000000">10</span><span style="color: #000000">))<br /></span><span style="color: #008080">21</span>&nbsp;<span style="color: #000000"></span><span style="color: #0000ff">print</span><span style="color: #000000">(my_range_2(</span><span style="color: #000000">5</span><span style="color: #000000">,&nbsp;</span><span style="color: #000000">20</span><span style="color: #000000">,&nbsp;</span><span style="color: #000000">2</span><span style="color: #000000">))<br /></span><span style="color: #008080">22</span>&nbsp;<span style="color: #000000"></span><span style="color: #0000ff">print</span><span style="color: #000000">(my_range_2(</span><span style="color: #000000">1</span><span style="color: #000000">,&nbsp;</span><span style="color: #000000">10</span><span style="color: #000000">))<br /></span><span style="color: #008080">23</span>&nbsp;<span style="color: #000000"></span><span style="color: #0000ff">print</span><span style="color: #000000">(my_range_2(</span><span style="color: #000000">10</span><span style="color: #000000">,&nbsp;0,&nbsp;</span><span style="color: #000000">-</span><span style="color: #000000">1</span><span style="color: #000000">))<br /></span><span style="color: #008080">24</span>&nbsp;<span style="color: #000000"></span><span style="color: #0000ff">print</span><span style="color: #000000">(my_range_2(</span><span style="color: #000000">100</span><span style="color: #000000">,&nbsp;0,&nbsp;</span><span style="color: #000000">-</span><span style="color: #000000">9</span><span style="color: #000000">))<br /></span><span style="color: #008080">25</span>&nbsp;<span style="color: #000000"></span><span style="color: #0000ff">print</span><span style="color: #000000">(my_range_2(</span><span style="color: #000000">10</span><span style="color: #000000">,&nbsp;</span><span style="color: #000000">20</span><span style="color: #000000">,&nbsp;</span><span style="color: #000000">-</span><span style="color: #000000">5</span><span style="color: #000000">))<br /></span><span style="color: #008080">26</span>&nbsp;<span style="color: #000000"><br /></span><span style="color: #008080">27</span>&nbsp;<span style="color: #000000"></span><span style="color: #0000ff">print</span><span style="color: #000000">(my_range_2(</span><span style="color: #000000">100</span><span style="color: #000000">,&nbsp;0,&nbsp;</span><span style="color: #000000">-</span><span style="color: #000000">10</span><span style="color: #000000">))<br /></span><span style="color: #008080">28</span>&nbsp;<span style="color: #000000"></span><span style="color: #0000ff">print</span><span style="color: #000000">(my_range_2(</span><span style="color: #000000">-</span><span style="color: #000000">10</span><span style="color: #000000">))<br /></span><span style="color: #008080">29</span>&nbsp;<span style="color: #000000"></span><span style="color: #0000ff">print</span><span style="color: #000000">(my_range_2(</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">5</span><span style="color: #000000">,&nbsp;</span><span style="color: #000000">1</span><span style="color: #000000">))<br /></span><span style="color: #008080">30</span>&nbsp;<span style="color: #000000"></span><span style="color: #0000ff">print</span><span style="color: #000000">(my_range_2(</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">20</span><span style="color: #000000">,&nbsp;</span><span style="color: #000000">1</span><span style="color: #000000">))<br /></span><span style="color: #008080">31</span>&nbsp;<span style="color: #000000"></span><span style="color: #0000ff">print</span><span style="color: #000000">(my_range_2(</span><span style="color: #000000">-</span><span style="color: #000000">50</span><span style="color: #000000">,&nbsp;</span><span style="color: #000000">-</span><span style="color: #000000">45</span><span style="color: #000000">))</span></div><br />&gt;&gt;&gt; <br />[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]<br />[5, 7, 9, 11, 13, 15, 17, 19]<br />[1, 2, 3, 4, 5, 6, 7, 8, 9]<br />[10, 9, 8, 7, 6, 5, 4, 3, 2, 1]<br />[100, 91, 82, 73, 64, 55, 46, 37, 28, 19, 10, 1]<br />[]<br />[100, 90, 80, 70, 60, 50, 40, 30, 20, 10]<br />[-10, -9, -8, -7, -6, -5, -4, -3, -2, -1]<br />[-10, -9, -8, -7, -6]<br />[]<br />[-50, -49, -48, -47, -46]<img src ="http://www.cppblog.com/unixfy/aggbug/200375.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/unixfy/" target="_blank">unixfy</a> 2013-05-18 14:32 <a href="http://www.cppblog.com/unixfy/archive/2013/05/18/200375.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>Python 递归二分查找</title><link>http://www.cppblog.com/unixfy/archive/2013/05/16/200337.html</link><dc:creator>unixfy</dc:creator><author>unixfy</author><pubDate>Thu, 16 May 2013 14:24:00 GMT</pubDate><guid>http://www.cppblog.com/unixfy/archive/2013/05/16/200337.html</guid><wfw:comment>http://www.cppblog.com/unixfy/comments/200337.html</wfw:comment><comments>http://www.cppblog.com/unixfy/archive/2013/05/16/200337.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/unixfy/comments/commentRss/200337.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/unixfy/services/trackbacks/200337.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"><!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />--><span style="color: #008080">&nbsp;1</span>&nbsp;<span style="color: #0000ff">def</span><span style="color: #000000">&nbsp;search(sequence,&nbsp;number,&nbsp;lower&nbsp;</span><span style="color: #000000">=</span><span style="color: #000000">&nbsp;0,&nbsp;upper&nbsp;</span><span style="color: #000000">=</span><span style="color: #000000">&nbsp;None):<br /></span><span style="color: #008080">&nbsp;2</span>&nbsp;<span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">if</span><span style="color: #000000">&nbsp;upper&nbsp;</span><span style="color: #0000ff">is</span><span style="color: #000000">&nbsp;None:<br /></span><span style="color: #008080">&nbsp;3</span>&nbsp;<span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;upper&nbsp;</span><span style="color: #000000">=</span><span style="color: #000000">&nbsp;len(sequence)&nbsp;</span><span style="color: #000000">-</span><span style="color: #000000">&nbsp;</span><span style="color: #000000">1</span><span style="color: #000000"><br /></span><span style="color: #008080">&nbsp;4</span>&nbsp;<span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">if</span><span style="color: #000000">&nbsp;lower&nbsp;</span><span style="color: #000000">==</span><span style="color: #000000">&nbsp;upper:<br /></span><span style="color: #008080">&nbsp;5</span>&nbsp;<span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">assert</span><span style="color: #000000">(number&nbsp;</span><span style="color: #000000">==</span><span style="color: #000000">&nbsp;sequence[upper])<br /></span><span style="color: #008080">&nbsp;6</span>&nbsp;<span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">return</span><span style="color: #000000">&nbsp;upper<br /></span><span style="color: #008080">&nbsp;7</span>&nbsp;<span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">else</span><span style="color: #000000">:<br /></span><span style="color: #008080">&nbsp;8</span>&nbsp;<span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;middle&nbsp;</span><span style="color: #000000">=</span><span style="color: #000000">&nbsp;(lower&nbsp;</span><span style="color: #000000">+</span><span style="color: #000000">&nbsp;upper)&nbsp;</span><span style="color: #000000">//</span><span style="color: #000000">&nbsp;</span><span style="color: #000000">2</span><span style="color: #000000"><br /></span><span style="color: #008080">&nbsp;9</span>&nbsp;<span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">if</span><span style="color: #000000">&nbsp;number&nbsp;</span><span style="color: #000000">&gt;</span><span style="color: #000000">&nbsp;sequence[middle]:<br /></span><span style="color: #008080">10</span>&nbsp;<span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">return</span><span style="color: #000000">&nbsp;search(sequence,&nbsp;number,&nbsp;middle&nbsp;</span><span style="color: #000000">+</span><span style="color: #000000">&nbsp;</span><span style="color: #000000">1</span><span style="color: #000000">,&nbsp;upper)<br /></span><span style="color: #008080">11</span>&nbsp;<span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">else</span><span style="color: #000000">:<br /></span><span style="color: #008080">12</span>&nbsp;<span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">return</span><span style="color: #000000">&nbsp;search(sequence,&nbsp;number,&nbsp;lower,&nbsp;middle)<br /></span><span style="color: #008080">13</span>&nbsp;<span style="color: #000000"><br /></span><span style="color: #008080">14</span>&nbsp;<span style="color: #000000">seq&nbsp;</span><span style="color: #000000">=</span><span style="color: #000000">&nbsp;[</span><span style="color: #000000">34</span><span style="color: #000000">,&nbsp;</span><span style="color: #000000">67</span><span style="color: #000000">,&nbsp;</span><span style="color: #000000">8</span><span style="color: #000000">,&nbsp;</span><span style="color: #000000">123</span><span style="color: #000000">,&nbsp;</span><span style="color: #000000">4</span><span style="color: #000000">,&nbsp;</span><span style="color: #000000">100</span><span style="color: #000000">,&nbsp;</span><span style="color: #000000">95</span><span style="color: #000000">]<br /></span><span style="color: #008080">15</span>&nbsp;<span style="color: #000000"></span><span style="color: #0000ff">print</span><span style="color: #000000">(seq)<br /></span><span style="color: #008080">16</span>&nbsp;<span style="color: #000000">seq.sort()<br /></span><span style="color: #008080">17</span>&nbsp;<span style="color: #000000"></span><span style="color: #0000ff">print</span><span style="color: #000000">(seq)<br /></span><span style="color: #008080">18</span>&nbsp;<span style="color: #000000"></span><span style="color: #0000ff">print</span><span style="color: #000000">(search(seq,&nbsp;</span><span style="color: #000000">34</span><span style="color: #000000">))<br /></span><span style="color: #008080">19</span>&nbsp;<span style="color: #000000"></span><span style="color: #0000ff">print</span><span style="color: #000000">(search(seq,&nbsp;</span><span style="color: #000000">100</span><span style="color: #000000">))</span></div><br />输出：<br />[34, 67, 8, 123, 4, 100, 95]<br />[4, 8, 34, 67, 95, 100, 123]<br />2<br />5<img src ="http://www.cppblog.com/unixfy/aggbug/200337.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/unixfy/" target="_blank">unixfy</a> 2013-05-16 22:24 <a href="http://www.cppblog.com/unixfy/archive/2013/05/16/200337.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>何为闭包</title><link>http://www.cppblog.com/unixfy/archive/2013/05/16/200336.html</link><dc:creator>unixfy</dc:creator><author>unixfy</author><pubDate>Thu, 16 May 2013 13:18:00 GMT</pubDate><guid>http://www.cppblog.com/unixfy/archive/2013/05/16/200336.html</guid><wfw:comment>http://www.cppblog.com/unixfy/comments/200336.html</wfw:comment><comments>http://www.cppblog.com/unixfy/archive/2013/05/16/200336.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/unixfy/comments/commentRss/200336.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/unixfy/services/trackbacks/200336.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"><!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />--><span style="color: #008080">&nbsp;1</span>&nbsp;<span style="color: #0000ff">def</span><span style="color: #000000">&nbsp;multiplier(factor):<br /></span><span style="color: #008080">&nbsp;2</span>&nbsp;<span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">def</span><span style="color: #000000">&nbsp;multiply_by_factor(number):<br /></span><span style="color: #008080">&nbsp;3</span>&nbsp;<span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">return</span><span style="color: #000000">&nbsp;number&nbsp;</span><span style="color: #000000">*</span><span style="color: #000000">&nbsp;factor<br /></span><span style="color: #008080">&nbsp;4</span>&nbsp;<span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">return</span><span style="color: #000000">&nbsp;multiply_by_factor<br /></span><span style="color: #008080">&nbsp;5</span>&nbsp;<span style="color: #000000"><br /></span><span style="color: #008080">&nbsp;6</span>&nbsp;<span style="color: #000000"><br /></span><span style="color: #008080">&nbsp;7</span>&nbsp;<span style="color: #000000">double&nbsp;</span><span style="color: #000000">=</span><span style="color: #000000">&nbsp;multiplier(</span><span style="color: #000000">2</span><span style="color: #000000">)&nbsp;</span><span style="color: #008000">#</span><span style="color: #008000">&nbsp;factor&nbsp;=&nbsp;2</span><span style="color: #008000"><br /></span><span style="color: #008080">&nbsp;8</span>&nbsp;<span style="color: #008000"></span><span style="color: #0000ff">print</span><span style="color: #000000">(double(</span><span style="color: #000000">5</span><span style="color: #000000">))&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #008000">#</span><span style="color: #008000">&nbsp;number&nbsp;=&nbsp;5</span><span style="color: #008000"><br /></span><span style="color: #008080">&nbsp;9</span>&nbsp;<span style="color: #008000"></span><span style="color: #000000">triple&nbsp;</span><span style="color: #000000">=</span><span style="color: #000000">&nbsp;multiplier(</span><span style="color: #000000">3</span><span style="color: #000000">)&nbsp;</span><span style="color: #008000">#</span><span style="color: #008000">&nbsp;factor&nbsp;=&nbsp;3</span><span style="color: #008000"><br /></span><span style="color: #008080">10</span>&nbsp;<span style="color: #008000"></span><span style="color: #0000ff">print</span><span style="color: #000000">(triple(</span><span style="color: #000000">4</span><span style="color: #000000">))&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #008000">#</span><span style="color: #008000">&nbsp;number&nbsp;=&nbsp;4</span><span style="color: #008000"><br /></span><span style="color: #008080">11</span>&nbsp;<span style="color: #008000"></span><span style="color: #0000ff">print</span><span style="color: #000000">(multiplier(</span><span style="color: #000000">5</span><span style="color: #000000">)(</span><span style="color: #000000">4</span><span style="color: #000000">))</span><span style="color: #008000">#</span><span style="color: #008000">&nbsp;factor&nbsp;=&nbsp;5,&nbsp;number&nbsp;=&nbsp;4</span></div><br />输出：<br />&gt;&gt;&gt; <br />10<br />12<br />20<img src ="http://www.cppblog.com/unixfy/aggbug/200336.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/unixfy/" target="_blank">unixfy</a> 2013-05-16 21:18 <a href="http://www.cppblog.com/unixfy/archive/2013/05/16/200336.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item></channel></rss>