{"id":203,"date":"2011-05-26T19:33:59","date_gmt":"2011-05-27T03:33:59","guid":{"rendered":"http:\/\/zhanxw.com\/blog\/?p=203"},"modified":"2011-05-26T20:04:00","modified_gmt":"2011-05-27T04:04:00","slug":"%e5%a6%82%e4%bd%95%e6%89%a9%e5%b1%95python%e7%9a%84%e5%ae%b9%e5%99%a8%e7%b1%bb","status":"publish","type":"post","link":"https:\/\/zhanxw.com\/blog\/2011\/05\/%e5%a6%82%e4%bd%95%e6%89%a9%e5%b1%95python%e7%9a%84%e5%ae%b9%e5%99%a8%e7%b1%bb\/","title":{"rendered":"\u5982\u4f55\u6269\u5c55Python\u7684\u5bb9\u5668\u7c7b"},"content":{"rendered":"<p>\u5982\u4f55\u6269\u5c55Python\u7684\u5bb9\u5668\u7c7b<br \/>\nHow to extend Python container class (using some idiom)<\/p>\n<p>\u672c\u6587\u5047\u8bbe\u5df2\u7ecf\u6709\u4e00\u4e2aC++\u8bed\u8a00\u5199\u7684array\u7c7b\u578b\u7684\u6570\u636e\u7ed3\u6784\uff0c\u53ef\u4ee5\u7528v.getBase(unsigned int i) \u6765\u5f97\u5230v \u6570\u7ec4\u5728\u4e0b\u6807i\u7684\u6570\u503c\u3002\u6211\u4eec\u60f3\u5229\u7528Python\u7075\u6d3b\u7684slice\u529f\u80fd\uff0c\u6bd4\u59821:10, 1:10:2, -10:-5\u7b49\u65b9\u5f0f\u6765\u6307\u5b9a\u4e0d\u540c\u7684\u4e0b\u6807\u3002\u8fd9\u79cd\u7075\u6d3b\u7684\u4e0b\u6807\u5728Python\u4e2d\u53ef\u4ee5\u6709\u4e09\u79cd\u5f62\u5f0f\uff1a<\/p>\n<p>1. \u6574\u6570\uff1a v[1]<br \/>\n2. slice \u5bf9\u8c61\uff1a v[1:10]<br \/>\n3. tuple \u5bf9\u8c61\uff1a v[1:10, 20:30]<\/p>\n<p>\u8fd9\u4e09\u79cd\u5bf9\u8c61\u90fd\u4f1a\u88ab\u4f20\u5230__getitem__(self, key)\u7684key\u53c2\u6570\u4e2d\u3002\u901a\u8fc7\u53c2\u8003\u30101\u3011\uff0c\u30102\u3011\uff0c\u6211\u53d1\u73b0\u4e0b\u9762\u7684\u4ee3\u7801\u53ef\u4ee5\u7b80\u6d01\u7684\u5904\u7406\u4e0a\u8ff0\u6240\u6709\u60c5\u51b5\uff1a<\/p>\n<pre class=\"brush: python; title: ; notranslate\" title=\"\"> \r\nclass ContainerClass:\r\n    def __getitem__(self, key):\r\n        if isinstance(key, int):\r\n            return chr(v.getBase(self.obj, key))\r\n        elif isinstance(key, slice):\r\n            return ''.join(&#x5B;self&#x5B;x] for x in xrange(*key.indices(len(self)))])\r\n        elif isinstance(key, tuple):\r\n            return ''.join(&#x5B;self&#x5B;i] for i in key])\r\n<\/pre>\n<p>\u6ce8\u610f\uff1a<br \/>\n\u8fd9\u91cc\u53ea\u662f\u4ee3\u7801\u7247\u6bb5\u3002\u5168\u90e8\u4ee3\u7801\u89c1\u53e6\u4e00\u7247Blog\uff1a<a href=\"http:\/\/zhanxw.com\/blog\/2011\/05\/%e5%a6%82%e4%bd%95%e5%9c%a8python%e4%b8%ad%e8%b0%83%e7%94%a8cc%e4%bb%a3%e7%a0%81\/\">\u5982\u4f55\u5728Python\u4e2d\u8c03\u7528C\/C++\u4ee3\u7801<\/a>\u3002<\/p>\n<p>\u30101\u3011Python Data Model:<br \/>\nhttp:\/\/docs.python.org\/reference\/datamodel.html<br \/>\n\u30102\u3011Python in a nut shell:<br \/>\nhttp:\/\/books.google.com\/books?id=JnR9hQA3SncC&#038;pg=PA110&#038;lpg=PA110&#038;dq=python+slice+object+idiom&#038;source=bl&#038;ots=Jb1XIv_71t&#038;sig=-_NHkwycfC8yipkc4Tl_e4sruKc&#038;hl=en&#038;ei=uRXfTcr5Jsro0QHa4sG5Cg&#038;sa=X&#038;oi=book_result&#038;ct=result&#038;resnum=10&#038;ved=0CF0Q6AEwCQ#v=onepage&#038;q=python%20slice%20object%20idiom&#038;f=false<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5982\u4f55\u6269\u5c55Python\u7684\u5bb9\u5668\u7c7b How to extend Python container class (using some idiom) \u672c\u6587\u5047\u8bbe\u5df2\u7ecf\u6709\u4e00\u4e2aC++\u8bed\u8a00\u5199\u7684array\u7c7b\u578b\u7684\u6570\u636e\u7ed3\u6784\uff0c\u53ef\u4ee5\u7528v.getBase(unsigned int i) \u6765\u5f97\u5230v \u6570\u7ec4\u5728\u4e0b\u6807i\u7684\u6570\u503c\u3002\u6211\u4eec\u60f3\u5229\u7528Python\u7075\u6d3b\u7684slice\u529f\u80fd\uff0c\u6bd4\u59821:10, 1:10:2, -10:-5\u7b49\u65b9\u5f0f\u6765\u6307\u5b9a\u4e0d\u540c\u7684\u4e0b\u6807\u3002\u8fd9\u79cd\u7075\u6d3b\u7684\u4e0b\u6807\u5728Python\u4e2d\u53ef\u4ee5\u6709\u4e09\u79cd\u5f62\u5f0f\uff1a 1. \u6574\u6570\uff1a v[1] 2. slice \u5bf9\u8c61\uff1a v[1:10] 3. tuple \u5bf9\u8c61\uff1a v[1:10, 20:30] \u8fd9\u4e09\u79cd\u5bf9\u8c61\u90fd\u4f1a\u88ab\u4f20\u5230__getitem__(self, key)\u7684key\u53c2\u6570\u4e2d\u3002\u901a\u8fc7\u53c2\u8003\u30101\u3011\uff0c\u30102\u3011\uff0c\u6211\u53d1\u73b0\u4e0b\u9762\u7684\u4ee3\u7801\u53ef\u4ee5\u7b80\u6d01\u7684\u5904\u7406\u4e0a\u8ff0\u6240\u6709\u60c5\u51b5\uff1a class ContainerClass: def __getitem__(self, key): if isinstance(key, int): return chr(v.getBase(self.obj, key)) elif isinstance(key, slice): return &#8221;.join(&#x5B;self&#x5B;x] for x in xrange(*key.indices(len(self)))]) elif isinstance(key, tuple): return [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4],"tags":[54,53],"class_list":["post-203","post","type-post","status-publish","format-standard","hentry","category-code","tag-container","tag-python"],"_links":{"self":[{"href":"https:\/\/zhanxw.com\/blog\/wp-json\/wp\/v2\/posts\/203","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/zhanxw.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/zhanxw.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/zhanxw.com\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/zhanxw.com\/blog\/wp-json\/wp\/v2\/comments?post=203"}],"version-history":[{"count":0,"href":"https:\/\/zhanxw.com\/blog\/wp-json\/wp\/v2\/posts\/203\/revisions"}],"wp:attachment":[{"href":"https:\/\/zhanxw.com\/blog\/wp-json\/wp\/v2\/media?parent=203"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zhanxw.com\/blog\/wp-json\/wp\/v2\/categories?post=203"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zhanxw.com\/blog\/wp-json\/wp\/v2\/tags?post=203"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}