{"id":1057282,"date":"2024-12-31T15:06:35","date_gmt":"2024-12-31T07:06:35","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1057282.html"},"modified":"2024-12-31T15:06:37","modified_gmt":"2024-12-31T07:06:37","slug":"python%e5%a6%82%e4%bd%95%e6%9e%84%e5%bb%ba%e5%af%b9%e8%b1%a1%e5%8f%af%e4%bb%a5%e4%b8%8b%e6%a0%87%e8%ae%bf%e9%97%ae","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1057282.html","title":{"rendered":"python\u5982\u4f55\u6784\u5efa\u5bf9\u8c61\u53ef\u4ee5\u4e0b\u6807\u8bbf\u95ee"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-docs.pingcode.com\/wp-content\/uploads\/2024\/12\/cbbba4cd-f42e-4303-b851-4d3fd7868020.webp?x-oss-process=image\/auto-orient,1\/format,webp\" alt=\"python\u5982\u4f55\u6784\u5efa\u5bf9\u8c61\u53ef\u4ee5\u4e0b\u6807\u8bbf\u95ee\" \/><\/p>\n<p><p> <strong>\u4f7f\u7528<code>__getitem__<\/code>\u548c<code>__setitem__<\/code>\u65b9\u6cd5\u3001\u5b9e\u73b0\u81ea\u5b9a\u4e49\u7c7b\u3001\u4e0e\u5217\u8868\u548c\u5b57\u5178\u7684\u7075\u6d3b\u6027\u3001\u652f\u6301\u5207\u7247\u64cd\u4f5c<\/strong>\u3002Python\u4e2d\u6784\u5efa\u5bf9\u8c61\u53ef\u4ee5\u4e0b\u6807\u8bbf\u95ee\u4e3b\u8981\u6d89\u53ca\u5b9e\u73b0<code>__getitem__<\/code>\u548c<code>__setitem__<\/code>\u65b9\u6cd5\u3002\u901a\u8fc7\u8fd9\u4e9b\u65b9\u6cd5\uff0c\u6211\u4eec\u53ef\u4ee5\u5b9a\u4e49\u5bf9\u8c61\u5728\u4f7f\u7528\u4e0b\u6807\u8bbf\u95ee\u65f6\u7684\u884c\u4e3a\u3002<code>__getitem__<\/code>\u7528\u4e8e\u83b7\u53d6\u5bf9\u8c61\u7684\u5143\u7d20\uff0c<code>__setitem__<\/code>\u7528\u4e8e\u8bbe\u7f6e\u5bf9\u8c61\u7684\u5143\u7d20\u3002\u6211\u4eec\u53ef\u4ee5\u501f\u9274\u5217\u8868\u548c\u5b57\u5178\u7684\u7075\u6d3b\u6027\uff0c\u751a\u81f3\u652f\u6301\u5207\u7247\u64cd\u4f5c\uff0c\u4f7f\u6211\u4eec\u7684\u5bf9\u8c61\u66f4\u52a0\u7075\u6d3b\u548c\u5f3a\u5927\u3002\u4e0b\u9762\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u5982\u4f55\u5b9e\u73b0\u8fd9\u4e9b\u529f\u80fd\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u4f7f\u7528<code>__getitem__<\/code>\u65b9\u6cd5<\/h3>\n<\/p>\n<p><p><code>__getitem__<\/code>\u65b9\u6cd5\u662fPython\u4e2d\u7684\u9b54\u6cd5\u65b9\u6cd5\uff0c\u7528\u4e8e\u5b9a\u4e49\u5bf9\u8c61\u5728\u4e0b\u6807\u8bbf\u95ee\u65f6\u7684\u884c\u4e3a\u3002\u8fd9\u4e2a\u65b9\u6cd5\u63a5\u53d7\u4e00\u4e2a\u53c2\u6570\uff0c\u53ef\u4ee5\u662f\u6574\u6570\u3001\u5b57\u7b26\u4e32\u6216\u5176\u4ed6\u7c7b\u578b\uff0c\u5177\u4f53\u53d6\u51b3\u4e8e\u4e0b\u6807\u8bbf\u95ee\u7684\u65b9\u5f0f\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">class MyCollection:<\/p>\n<p>    def __init__(self, data):<\/p>\n<p>        self.data = data<\/p>\n<p>    def __getitem__(self, index):<\/p>\n<p>        return self.data[index]<\/p>\n<h2><strong>\u793a\u4f8b<\/strong><\/h2>\n<p>collection = MyCollection([1, 2, 3, 4, 5])<\/p>\n<p>print(collection[2])  # \u8f93\u51fa: 3<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u5b9a\u4e49\u4e86\u4e00\u4e2a\u540d\u4e3a<code>MyCollection<\/code>\u7684\u7c7b\uff0c\u5e76\u5b9e\u73b0\u4e86<code>__getitem__<\/code>\u65b9\u6cd5\uff0c\u4ee5\u4fbf\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u4e0b\u6807\u8bbf\u95ee\u5bf9\u8c61\u4e2d\u7684\u5143\u7d20\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u4f7f\u7528<code>__setitem__<\/code>\u65b9\u6cd5<\/h3>\n<\/p>\n<p><p><code>__setitem__<\/code>\u65b9\u6cd5\u7528\u4e8e\u5b9a\u4e49\u5bf9\u8c61\u5728\u4f7f\u7528\u4e0b\u6807\u8bbe\u7f6e\u503c\u65f6\u7684\u884c\u4e3a\u3002\u8fd9\u4e2a\u65b9\u6cd5\u63a5\u53d7\u4e24\u4e2a\u53c2\u6570\uff1a\u4e0b\u6807\u548c\u8981\u8bbe\u7f6e\u7684\u503c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">class MyCollection:<\/p>\n<p>    def __init__(self, data):<\/p>\n<p>        self.data = data<\/p>\n<p>    def __getitem__(self, index):<\/p>\n<p>        return self.data[index]<\/p>\n<p>    def __setitem__(self, index, value):<\/p>\n<p>        self.data[index] = value<\/p>\n<h2><strong>\u793a\u4f8b<\/strong><\/h2>\n<p>collection = MyCollection([1, 2, 3, 4, 5])<\/p>\n<p>collection[2] = 10<\/p>\n<p>print(collection[2])  # \u8f93\u51fa: 10<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u6269\u5c55\u4e86<code>MyCollection<\/code>\u7c7b\uff0c\u6dfb\u52a0\u4e86<code>__setitem__<\/code>\u65b9\u6cd5\uff0c\u4ece\u800c\u5141\u8bb8\u4f7f\u7528\u4e0b\u6807\u6765\u8bbe\u7f6e\u5bf9\u8c61\u4e2d\u7684\u5143\u7d20\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001\u5b9e\u73b0\u81ea\u5b9a\u4e49\u7c7b<\/h3>\n<\/p>\n<p><p>\u4e3a\u4e86\u5b9e\u73b0\u66f4\u590d\u6742\u7684\u4e0b\u6807\u8bbf\u95ee\u884c\u4e3a\uff0c\u6211\u4eec\u53ef\u4ee5\u521b\u5efa\u4e00\u4e2a\u81ea\u5b9a\u4e49\u7c7b\uff0c\u5e76\u5728\u5176\u4e2d\u5b9e\u73b0<code>__getitem__<\/code>\u548c<code>__setitem__<\/code>\u65b9\u6cd5\u3002\u8fd9\u6837\u53ef\u4ee5\u66f4\u7075\u6d3b\u5730\u63a7\u5236\u5bf9\u8c61\u7684\u8bbf\u95ee\u548c\u4fee\u6539\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">class Matrix:<\/p>\n<p>    def __init__(self, rows, cols):<\/p>\n<p>        self.rows = rows<\/p>\n<p>        self.cols = cols<\/p>\n<p>        self.data = [[0 for _ in range(cols)] for _ in range(rows)]<\/p>\n<p>    def __getitem__(self, index):<\/p>\n<p>        row, col = index<\/p>\n<p>        return self.data[row][col]<\/p>\n<p>    def __setitem__(self, index, value):<\/p>\n<p>        row, col = index<\/p>\n<p>        self.data[row][col] = value<\/p>\n<h2><strong>\u793a\u4f8b<\/strong><\/h2>\n<p>matrix = Matrix(3, 3)<\/p>\n<p>matrix[1, 1] = 5<\/p>\n<p>print(matrix[1, 1])  # \u8f93\u51fa: 5<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u5b9a\u4e49\u4e86\u4e00\u4e2a<code>Matrix<\/code>\u7c7b\uff0c\u7528\u4e8e\u8868\u793a\u4e00\u4e2a\u4e8c\u7ef4\u77e9\u9635\u3002\u6211\u4eec\u901a\u8fc7\u5b9e\u73b0<code>__getitem__<\/code>\u548c<code>__setitem__<\/code>\u65b9\u6cd5\uff0c\u4f7f\u5f97\u53ef\u4ee5\u4f7f\u7528\u4e0b\u6807\u8bbf\u95ee\u548c\u4fee\u6539\u77e9\u9635\u4e2d\u7684\u5143\u7d20\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001\u652f\u6301\u5207\u7247\u64cd\u4f5c<\/h3>\n<\/p>\n<p><p>\u4e3a\u4e86\u4f7f\u6211\u4eec\u7684\u5bf9\u8c61\u66f4\u5f3a\u5927\uff0c\u6211\u4eec\u8fd8\u53ef\u4ee5\u652f\u6301\u5207\u7247\u64cd\u4f5c\u3002\u5b9e\u73b0\u5207\u7247\u64cd\u4f5c\u9700\u8981\u5728<code>__getitem__<\/code>\u65b9\u6cd5\u4e2d\u5904\u7406<code>slice<\/code>\u5bf9\u8c61\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">class MyCollection:<\/p>\n<p>    def __init__(self, data):<\/p>\n<p>        self.data = data<\/p>\n<p>    def __getitem__(self, index):<\/p>\n<p>        if isinstance(index, slice):<\/p>\n<p>            return self.data[index]<\/p>\n<p>        return self.data[index]<\/p>\n<p>    def __setitem__(self, index, value):<\/p>\n<p>        self.data[index] = value<\/p>\n<h2><strong>\u793a\u4f8b<\/strong><\/h2>\n<p>collection = MyCollection([1, 2, 3, 4, 5])<\/p>\n<p>print(collection[1:4])  # \u8f93\u51fa: [2, 3, 4]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u4fee\u6539\u4e86<code>__getitem__<\/code>\u65b9\u6cd5\uff0c\u4f7f\u5176\u80fd\u591f\u5904\u7406<code>slice<\/code>\u5bf9\u8c61\uff0c\u4ece\u800c\u652f\u6301\u5207\u7247\u64cd\u4f5c\u3002<\/p>\n<\/p>\n<p><h3>\u4e94\u3001\u7ed3\u5408\u5b57\u5178\u7684\u7075\u6d3b\u6027<\/h3>\n<\/p>\n<p><p>\u9664\u4e86\u5217\u8868\uff0c\u6211\u4eec\u8fd8\u53ef\u4ee5\u7ed3\u5408\u5b57\u5178\u7684\u7075\u6d3b\u6027\uff0c\u5141\u8bb8\u4f7f\u7528\u5b57\u7b26\u4e32\u4f5c\u4e3a\u4e0b\u6807\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">class MyDict:<\/p>\n<p>    def __init__(self):<\/p>\n<p>        self.data = {}<\/p>\n<p>    def __getitem__(self, key):<\/p>\n<p>        return self.data[key]<\/p>\n<p>    def __setitem__(self, key, value):<\/p>\n<p>        self.data[key] = value<\/p>\n<h2><strong>\u793a\u4f8b<\/strong><\/h2>\n<p>my_dict = MyDict()<\/p>\n<p>my_dict[&#39;name&#39;] = &#39;Alice&#39;<\/p>\n<p>print(my_dict[&#39;name&#39;])  # \u8f93\u51fa: Alice<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u5b9a\u4e49\u4e86\u4e00\u4e2a<code>MyDict<\/code>\u7c7b\uff0c\u901a\u8fc7\u5b9e\u73b0<code>__getitem__<\/code>\u548c<code>__setitem__<\/code>\u65b9\u6cd5\uff0c\u4f7f\u5176\u53ef\u4ee5\u50cf\u5b57\u5178\u4e00\u6837\u4f7f\u7528\u5b57\u7b26\u4e32\u4e0b\u6807\u8fdb\u884c\u8bbf\u95ee\u548c\u4fee\u6539\u3002<\/p>\n<\/p>\n<p><h3>\u516d\u3001\u7ed3\u5408\u5217\u8868\u548c\u5b57\u5178\u7684\u529f\u80fd<\/h3>\n<\/p>\n<p><p>\u6211\u4eec\u8fd8\u53ef\u4ee5\u521b\u5efa\u4e00\u4e2a\u81ea\u5b9a\u4e49\u7c7b\uff0c\u7ed3\u5408\u5217\u8868\u548c\u5b57\u5178\u7684\u529f\u80fd\uff0c\u652f\u6301\u591a\u79cd\u4e0b\u6807\u8bbf\u95ee\u65b9\u5f0f\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">class HybridCollection:<\/p>\n<p>    def __init__(self):<\/p>\n<p>        self.data = []<\/p>\n<p>    def add_item(self, item):<\/p>\n<p>        self.data.append(item)<\/p>\n<p>    def __getitem__(self, index):<\/p>\n<p>        if isinstance(index, int):<\/p>\n<p>            return self.data[index]<\/p>\n<p>        elif isinstance(index, str):<\/p>\n<p>            return {item[&#39;key&#39;]: item[&#39;value&#39;] for item in self.data}[index]<\/p>\n<p>    def __setitem__(self, index, value):<\/p>\n<p>        if isinstance(index, int):<\/p>\n<p>            self.data[index] = value<\/p>\n<p>        elif isinstance(index, str):<\/p>\n<p>            for item in self.data:<\/p>\n<p>                if item[&#39;key&#39;] == index:<\/p>\n<p>                    item[&#39;value&#39;] = value<\/p>\n<p>                    break<\/p>\n<h2><strong>\u793a\u4f8b<\/strong><\/h2>\n<p>collection = HybridCollection()<\/p>\n<p>collection.add_item({&#39;key&#39;: &#39;name&#39;, &#39;value&#39;: &#39;Alice&#39;})<\/p>\n<p>collection.add_item({&#39;key&#39;: &#39;age&#39;, &#39;value&#39;: 25})<\/p>\n<p>print(collection[0])  # \u8f93\u51fa: {&#39;key&#39;: &#39;name&#39;, &#39;value&#39;: &#39;Alice&#39;}<\/p>\n<p>print(collection[&#39;name&#39;])  # \u8f93\u51fa: Alice<\/p>\n<p>collection[&#39;name&#39;] = &#39;Bob&#39;<\/p>\n<p>print(collection[&#39;name&#39;])  # \u8f93\u51fa: Bob<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u5b9a\u4e49\u4e86\u4e00\u4e2a<code>HybridCollection<\/code>\u7c7b\uff0c\u901a\u8fc7\u5b9e\u73b0<code>__getitem__<\/code>\u548c<code>__setitem__<\/code>\u65b9\u6cd5\uff0c\u4f7f\u5176\u65e2\u53ef\u4ee5\u4f7f\u7528\u6574\u6570\u4e0b\u6807\u8bbf\u95ee\uff0c\u4e5f\u53ef\u4ee5\u4f7f\u7528\u5b57\u7b26\u4e32\u4e0b\u6807\u8bbf\u95ee\u3002<\/p>\n<\/p>\n<p><h3>\u4e03\u3001\u652f\u6301\u591a\u7ef4\u6570\u7ec4\u4e0b\u6807\u8bbf\u95ee<\/h3>\n<\/p>\n<p><p>\u5bf9\u4e8e\u591a\u7ef4\u6570\u7ec4\uff0c\u6211\u4eec\u53ef\u4ee5\u8fdb\u4e00\u6b65\u6269\u5c55<code>__getitem__<\/code>\u548c<code>__setitem__<\/code>\u65b9\u6cd5\uff0c\u4f7f\u5176\u652f\u6301\u66f4\u590d\u6742\u7684\u4e0b\u6807\u8bbf\u95ee\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">class MultiDimArray:<\/p>\n<p>    def __init__(self, shape):<\/p>\n<p>        self.shape = shape<\/p>\n<p>        self.data = self._create_array(shape)<\/p>\n<p>    def _create_array(self, shape):<\/p>\n<p>        if len(shape) == 1:<\/p>\n<p>            return [0] * shape[0]<\/p>\n<p>        return [self._create_array(shape[1:]) for _ in range(shape[0])]<\/p>\n<p>    def _get_element(self, indexes, data):<\/p>\n<p>        if len(indexes) == 1:<\/p>\n<p>            return data[indexes[0]]<\/p>\n<p>        return self._get_element(indexes[1:], data[indexes[0]])<\/p>\n<p>    def _set_element(self, indexes, data, value):<\/p>\n<p>        if len(indexes) == 1:<\/p>\n<p>            data[indexes[0]] = value<\/p>\n<p>        else:<\/p>\n<p>            self._set_element(indexes[1:], data[indexes[0]], value)<\/p>\n<p>    def __getitem__(self, indexes):<\/p>\n<p>        if not isinstance(indexes, tuple):<\/p>\n<p>            indexes = (indexes,)<\/p>\n<p>        return self._get_element(indexes, self.data)<\/p>\n<p>    def __setitem__(self, indexes, value):<\/p>\n<p>        if not isinstance(indexes, tuple):<\/p>\n<p>            indexes = (indexes,)<\/p>\n<p>        self._set_element(indexes, self.data, value)<\/p>\n<h2><strong>\u793a\u4f8b<\/strong><\/h2>\n<p>array = MultiDimArray((3, 3, 3))<\/p>\n<p>array[1, 1, 1] = 42<\/p>\n<p>print(array[1, 1, 1])  # \u8f93\u51fa: 42<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u5b9a\u4e49\u4e86\u4e00\u4e2a<code>MultiDimArray<\/code>\u7c7b\uff0c\u901a\u8fc7\u9012\u5f52\u7684\u65b9\u6cd5\u521b\u5efa\u591a\u7ef4\u6570\u7ec4\uff0c\u5e76\u5b9e\u73b0\u4e86\u652f\u6301\u591a\u7ef4\u6570\u7ec4\u4e0b\u6807\u8bbf\u95ee\u7684<code>__getitem__<\/code>\u548c<code>__setitem__<\/code>\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><h3>\u516b\u3001\u7ed3\u5408\u9ed8\u8ba4\u503c\u529f\u80fd<\/h3>\n<\/p>\n<p><p>\u6709\u65f6\u5019\u6211\u4eec\u5e0c\u671b\u5728\u8bbf\u95ee\u672a\u8bbe\u7f6e\u7684\u4e0b\u6807\u65f6\u8fd4\u56de\u4e00\u4e2a\u9ed8\u8ba4\u503c\uff0c\u8fd9\u53ef\u4ee5\u901a\u8fc7\u5b9e\u73b0\u4e00\u4e2a\u5e26\u6709\u9ed8\u8ba4\u503c\u529f\u80fd\u7684\u81ea\u5b9a\u4e49\u7c7b\u6765\u5b9e\u73b0\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">class DefaultDict:<\/p>\n<p>    def __init__(self, default_factory):<\/p>\n<p>        self.data = {}<\/p>\n<p>        self.default_factory = default_factory<\/p>\n<p>    def __getitem__(self, key):<\/p>\n<p>        if key in self.data:<\/p>\n<p>            return self.data[key]<\/p>\n<p>        return self.default_factory()<\/p>\n<p>    def __setitem__(self, key, value):<\/p>\n<p>        self.data[key] = value<\/p>\n<h2><strong>\u793a\u4f8b<\/strong><\/h2>\n<p>default_dict = DefaultDict(lambda: &#39;default&#39;)<\/p>\n<p>print(default_dict[&#39;missing&#39;])  # \u8f93\u51fa: default<\/p>\n<p>default_dict[&#39;name&#39;] = &#39;Alice&#39;<\/p>\n<p>print(default_dict[&#39;name&#39;])  # \u8f93\u51fa: Alice<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u5b9a\u4e49\u4e86\u4e00\u4e2a<code>DefaultDict<\/code>\u7c7b\uff0c\u5141\u8bb8\u5728\u8bbf\u95ee\u672a\u8bbe\u7f6e\u7684\u4e0b\u6807\u65f6\u8fd4\u56de\u4e00\u4e2a\u9ed8\u8ba4\u503c\u3002<\/p>\n<\/p>\n<p><p>\u901a\u8fc7\u4e0a\u8ff0\u793a\u4f8b\u548c\u8be6\u7ec6\u89e3\u91ca\uff0c\u6211\u4eec\u53ef\u4ee5\u770b\u5230\u5982\u4f55\u5728Python\u4e2d\u6784\u5efa\u5bf9\u8c61\u53ef\u4ee5\u4e0b\u6807\u8bbf\u95ee\u3002\u5b9e\u73b0\u8fd9\u4e9b\u529f\u80fd\u4e0d\u4ec5\u4f7f\u6211\u4eec\u7684\u5bf9\u8c61\u66f4\u7075\u6d3b\uff0c\u8fd8\u80fd\u63d0\u5347\u4ee3\u7801\u7684\u53ef\u8bfb\u6027\u548c\u53ef\u7ef4\u62a4\u6027\u3002\u901a\u8fc7\u7ed3\u5408<code>__getitem__<\/code>\u548c<code>__setitem__<\/code>\u65b9\u6cd5\uff0c\u6211\u4eec\u80fd\u591f\u8f7b\u677e\u5730\u5b9e\u73b0\u591a\u79cd\u4e0b\u6807\u8bbf\u95ee\u65b9\u5f0f\uff0c\u6ee1\u8db3\u4e0d\u540c\u573a\u666f\u7684\u9700\u6c42\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u5b9e\u73b0\u5bf9\u8c61\u7684\u4e0b\u6807\u8bbf\u95ee\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u8981\u8ba9\u81ea\u5b9a\u4e49\u5bf9\u8c61\u652f\u6301\u4e0b\u6807\u8bbf\u95ee\uff0c\u53ef\u4ee5\u901a\u8fc7\u5b9e\u73b0<code>__getitem__<\/code>\u548c<code>__setitem__<\/code>\u65b9\u6cd5\u6765\u5b8c\u6210\u3002\u8fd9\u4e24\u4e2a\u65b9\u6cd5\u5206\u522b\u7528\u4e8e\u83b7\u53d6\u548c\u8bbe\u7f6e\u5bf9\u8c61\u7684\u5143\u7d20\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u793a\u4f8b\uff1a<\/p>\n<pre><code class=\"language-python\">class MyList:\n    def __init__(self, data):\n        self.data = data\n\n    def __getitem__(self, index):\n        return self.data[index]\n\n    def __setitem__(self, index, value):\n        self.data[index] = value\n\n# \u4f7f\u7528\u793a\u4f8b\nmy_list = MyList([1, 2, 3])\nprint(my_list[0])  # \u8f93\u51fa: 1\nmy_list[1] = 5\nprint(my_list[1])  # \u8f93\u51fa: 5\n<\/code><\/pre>\n<p><strong>\u4f7f\u7528\u81ea\u5b9a\u4e49\u5bf9\u8c61\u65f6\uff0c\u6709\u54ea\u4e9b\u5e38\u89c1\u7684\u4f7f\u7528\u573a\u666f\uff1f<\/strong><br \/>\u81ea\u5b9a\u4e49\u5bf9\u8c61\u53ef\u4ee5\u5728\u8bb8\u591a\u573a\u5408\u4e2d\u4f7f\u7528\uff0c\u7279\u522b\u662f\u5728\u9700\u8981\u5c01\u88c5\u6570\u636e\u548c\u884c\u4e3a\u65f6\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u7528\u81ea\u5b9a\u4e49\u5bf9\u8c61\u6765\u8868\u793a\u6570\u636e\u6a21\u578b\uff0c\u63d0\u4f9b\u66f4\u9ad8\u5c42\u6b21\u7684\u62bd\u8c61\uff1b\u5728\u6570\u636e\u5904\u7406\u548c\u5206\u6790\u8fc7\u7a0b\u4e2d\uff0c\u80fd\u591f\u5c06\u590d\u6742\u7684\u6570\u636e\u7ed3\u6784\u5c01\u88c5\u5728\u5bf9\u8c61\u4e2d\uff0c\u65b9\u4fbf\u540e\u7eed\u64cd\u4f5c\u3002\u4f7f\u7528\u4e0b\u6807\u8bbf\u95ee\u7684\u5bf9\u8c61\u5728\u5b9e\u73b0\u7c7b\u4f3c\u6570\u7ec4\u6216\u5217\u8868\u7684\u529f\u80fd\u65f6\u4e5f\u975e\u5e38\u6709\u7528\uff0c\u6bd4\u5982\u5728\u5b9e\u73b0\u77e9\u9635\u6216\u56fe\u5f62\u6570\u636e\u7ed3\u6784\u65f6\u3002<\/p>\n<p><strong>\u5982\u4f55\u5904\u7406\u5bf9\u8c61\u4e0b\u6807\u8bbf\u95ee\u4e2d\u7684\u5f02\u5e38\u60c5\u51b5\uff1f<\/strong><br \/>\u5728\u5b9e\u73b0\u4e0b\u6807\u8bbf\u95ee\u65f6\uff0c\u5fc5\u987b\u8003\u8651\u5230\u53ef\u80fd\u51fa\u73b0\u7684\u5f02\u5e38\u60c5\u51b5\uff0c\u6bd4\u5982\u7d22\u5f15\u8d8a\u754c\u6216\u7c7b\u578b\u9519\u8bef\u3002\u5728<code>__getitem__<\/code>\u548c<code>__setitem__<\/code>\u65b9\u6cd5\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u5f02\u5e38\u5904\u7406\u673a\u5236\u6765\u6355\u83b7\u9519\u8bef\u5e76\u7ed9\u51fa\u53cb\u597d\u7684\u63d0\u793a\u3002\u4f8b\u5982\uff1a<\/p>\n<pre><code class=\"language-python\">def __getitem__(self, index):\n    try:\n        return self.data[index]\n    except IndexError:\n        print(&quot;\u7d22\u5f15\u8d85\u51fa\u8303\u56f4&quot;)\n    except TypeError:\n        print(&quot;\u7d22\u5f15\u5fc5\u987b\u662f\u6574\u6570&quot;)\n<\/code><\/pre>\n<p>\u901a\u8fc7\u8fd9\u79cd\u65b9\u5f0f\uff0c\u7528\u6237\u5728\u4f7f\u7528\u5bf9\u8c61\u65f6\u53ef\u4ee5\u83b7\u5f97\u66f4\u597d\u7684\u4f53\u9a8c\uff0c\u907f\u514d\u7a0b\u5e8f\u56e0\u5f02\u5e38\u800c\u5d29\u6e83\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u4f7f\u7528__getitem__\u548c__setitem__\u65b9\u6cd5\u3001\u5b9e\u73b0\u81ea\u5b9a\u4e49\u7c7b\u3001\u4e0e\u5217\u8868\u548c\u5b57\u5178\u7684\u7075\u6d3b\u6027\u3001\u652f\u6301\u5207\u7247\u64cd\u4f5c\u3002Py [&hellip;]","protected":false},"author":3,"featured_media":1057288,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[37],"tags":[],"acf":[],"_links":{"self":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1057282"}],"collection":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/comments?post=1057282"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1057282\/revisions"}],"predecessor-version":[{"id":1057290,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1057282\/revisions\/1057290"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1057288"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1057282"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1057282"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1057282"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}