{"id":176428,"date":"2024-05-08T19:10:38","date_gmt":"2024-05-08T11:10:38","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/176428.html"},"modified":"2024-05-08T19:10:44","modified_gmt":"2024-05-08T11:10:44","slug":"python%e4%b8%adlen%e5%87%bd%e6%95%b0%e5%ba%95%e5%b1%82%e6%80%8e%e4%b9%88%e5%86%99%e7%9a%84","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/176428.html","title":{"rendered":"python\u4e2dlen()\u51fd\u6570\u5e95\u5c42\u600e\u4e48\u5199\u7684"},"content":{"rendered":"<p style=\"text-align:center\"><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/27060114\/cf6c6dcb-1331-4128-9e0e-30832d473b7a.webp\" alt=\"python\u4e2dlen()\u51fd\u6570\u5e95\u5c42\u600e\u4e48\u5199\u7684\" \/><\/p>\n<p><p><strong>Python\u4e2d\u7684<code>len()<\/code>\u51fd\u6570\u662f\u901a\u8fc7\u8c03\u7528\u4e00\u4e2a\u5bf9\u8c61\u7684<code>__len__()<\/code>\u65b9\u6cd5\u6765\u83b7\u53d6\u5bf9\u8c61\u957f\u5ea6\u7684\u3001\u5e95\u5c42\u4f9d\u8d56\u4e8eC\u8bed\u8a00\u7684\u5b9e\u73b0<\/strong>\u3002\u5728Python\u7684\u5e95\u5c42\u4ee3\u7801\u4e2d\uff0c\u5373CPython\u7684\u5b9e\u73b0\u4e2d\uff0c<code>len()<\/code>\u51fd\u6570\u4f1a\u9996\u5148\u68c0\u67e5\u5bf9\u8c61\u662f\u5426\u6709\u4e00\u4e2a\u5feb\u901f\u7684\u65b9\u6cd5\u6765\u8fd4\u56de\u957f\u5ea6\u3002\u5982\u679c\u5bf9\u8c61\u662f\u4e00\u4e2a\u5185\u7f6e\u7684\u5e8f\u5217\u7c7b\u578b\uff08\u5982\u5217\u8868\u3001\u5143\u7ec4\u3001\u5b57\u7b26\u4e32\u7b49\uff09\uff0c\u8fd9\u4e00\u8fc7\u7a0b\u975e\u5e38\u76f4\u63a5\u3002\u5177\u4f53\u5730\uff0c<code>len()<\/code>\u4f1a\u68c0\u67e5\u5bf9\u8c61\u7684\u7c7b\u578b\u7ed3\u6784\u4f53\u4e2d\u662f\u5426\u6709\u4e00\u4e2a\u540d\u4e3a<code>tp_as_sequence<\/code>\u7684\u5b57\u6bb5\uff0c\u8fd9\u662f\u4e00\u4e2a\u6307\u5411PySequenceMethods\u7ed3\u6784\u4f53\u7684\u6307\u9488\uff0c\u8be5\u7ed3\u6784\u4f53\u5185\u542b\u4e00\u4e2a\u540d\u4e3a<code>sq_length<\/code>\u7684\u51fd\u6570\u6307\u9488\u3002\u5982\u679c\u8be5\u5b57\u6bb5\u5b58\u5728\u4e14\u975e\u7a7a\uff0cPython\u5c31\u4f1a\u901a\u8fc7\u8be5\u6307\u9488\u8c03\u7528\u5bf9\u5e94\u7684\u51fd\u6570\u6765\u8fd4\u56de\u5bf9\u8c61\u7684\u957f\u5ea6\u3002<\/p>\n<\/p>\n<p><p>\u5c55\u5f00\u6765\u8bf4\uff0c\u5bf9\u4e8e\u5927\u90e8\u5206\u5185\u7f6e\u7c7b\u578b\uff0c<code>len()<\/code>\u51fd\u6570\u7684\u6027\u80fd\u662f\u975e\u5e38\u5feb\u7684\uff0c\u56e0\u4e3a\u8fd9\u4e9b\u7c7b\u578b\u7684\u957f\u5ea6\u4fe1\u606f\u901a\u5e38\u4f1a\u88ab\u76f4\u63a5\u7f13\u5b58\u5728\u5bf9\u8c61\u5185\u90e8\u3002\u5982\uff0c\u5bf9\u4e8e\u5217\u8868\uff0c\u5b83\u7684\u5927\u5c0f\u5b58\u50a8\u5728\u4e00\u4e2a\u53eb<code>PyVarObject<\/code>\u7684\u7ed3\u6784\u4f53\u4e2d\uff0c<code>len()<\/code>\u51fd\u6570\u4f1a\u76f4\u63a5\u8fd4\u56de\u8fd9\u4e2a\u503c\u800c\u4e0d\u7528\u53bb\u6570\u5217\u8868\u4e2d\u6709\u591a\u5c11\u4e2a\u5143\u7d20\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001LEN() \u51fd\u6570\u7684\u5de5\u4f5c\u673a\u5236<\/h3>\n<\/p>\n<p><p>\u5728\u66f4\u6df1\u5165\u7406\u89e3<code>len()<\/code>\u51fd\u6570\u4e4b\u524d\uff0c\u6211\u4eec\u9700\u8981\u4e86\u89e3Python\u4e2d\u7684\u6570\u636e\u6a21\u578b\u3002Python\u662f\u4e00\u95e8\u5178\u578b\u7684\u9762\u5411\u5bf9\u8c61\u8bed\u8a00\uff0c\u5176\u4e2d\u6bcf\u4e2a\u5bf9\u8c61\u90fd\u53ef\u4ee5\u6709\u65b9\u6cd5\u4e0e\u4e4b\u5173\u8054\u3002<code>len()<\/code>\u51fd\u6570\u80cc\u540e\u6b63\u662f\u5229\u7528\u4e86\u8fd9\u4e00\u7279\u6027\u3002\u5f53\u6211\u4eec\u8c03\u7528<code>len(something)<\/code>\u65f6\uff0c\u5b9e\u9645\u53d1\u751f\u7684\u662fPython\u5185\u90e8\u8c03\u7528\u4e86<code>something.__len__()<\/code>\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u5185\u7f6e\u7c7b\u578b\u7684__LEN__() \u5b9e\u73b0<\/h3>\n<\/p>\n<p><p>\u5bf9\u4e8e\u5185\u7f6e\u7c7b\u578b\u5982\u5b57\u7b26\u4e32\uff08<code>str<\/code>\uff09\u3001\u5217\u8868\uff08<code>list<\/code>\uff09\u3001\u5143\u7ec4\uff08<code>tuple<\/code>\uff09\u548c\u5b57\u5178\uff08<code>dict<\/code>\uff09\u7b49\uff0c\u5b83\u4eec\u7684<code>__len__()<\/code>\u65b9\u6cd5\u5b9e\u73b0\u662f\u9ad8\u5ea6\u4f18\u5316\u8fc7\u7684C\u8bed\u8a00\u4ee3\u7801\u3002\u4ee5\u5217\u8868\u4e3a\u4f8b\uff0c\u5217\u8868\u5bf9\u8c61\u672c\u8d28\u4e0a\u662f\u4e00\u4e2a\u52a8\u6001\u6570\u7ec4\uff0c\u5b83\u5728\u5185\u90e8\u6709\u4e00\u4e2a\u6307\u9488\u6307\u5411\u6570\u7ec4\u7684\u521d\u59cb\u4f4d\u7f6e\uff0c\u800c\u5176\u957f\u5ea6\uff08\u5373\u5305\u542b\u5143\u7d20\u7684\u6570\u91cf\uff09\u662f\u76f4\u63a5\u5b58\u50a8\u5728\u5217\u8868\u5bf9\u8c61\u7684\u5185\u5b58\u7ed3\u6784\u4e2d\u7684\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001\u7528\u6237\u81ea\u5b9a\u4e49\u7c7b\u578b\u7684__LEN__() \u5b9e\u73b0<\/h3>\n<\/p>\n<p><p>\u5728\u7528\u6237\u81ea\u5b9a\u4e49\u7684\u7c7b\u578b\u4e2d\uff0c\u4f60\u53ef\u4ee5\u81ea\u884c\u5b9a\u4e49<code>__len__()<\/code>\u65b9\u6cd5\u3002\u4f8b\u5982\uff0c\u4f60\u521b\u5efa\u4e00\u4e2a\u81ea\u5b9a\u4e49\u7684\u96c6\u5408\u7c7b\uff0c\u901a\u8fc7\u5b9e\u73b0<code>__len__()<\/code>\u6765\u8ba9<code>len()<\/code>\u51fd\u6570\u80fd\u8fd4\u56de\u4f60\u7684\u96c6\u5408\u4e2d\u5305\u542b\u7684\u9879\u76ee\u6570\u91cf\u3002\u5982\u679c\u672a\u63d0\u4f9b<code>__len__()<\/code>\u65b9\u6cd5\uff0c\u5c1d\u8bd5\u8c03\u7528<code>len()<\/code>\u5c06\u4f1a\u8fd4\u56de\u4e00\u4e2a\u9519\u8bef\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001LEN() \u51fd\u6570\u7684\u6548\u7387\u548c\u5c40\u9650\u6027<\/h3>\n<\/p>\n<p><p><code>len()<\/code>\u51fd\u6570\u901a\u5e38\u975e\u5e38\u5feb\uff0c\u7279\u522b\u662f\u5bf9\u4e8e\u5185\u7f6e\u7c7b\u578b\uff0c\u5176\u65f6\u95f4\u590d\u6742\u5ea6\u662fO(1)\uff0c\u4e5f\u5c31\u662f\u8bf4\uff0c\u5176\u67e5\u627e\u65f6\u95f4\u4e0d\u4f1a\u968f\u7740\u5143\u7d20\u6570\u91cf\u7684\u589e\u52a0\u800c\u589e\u52a0\u3002\u7136\u800c\uff0c<code>len()<\/code>\u51fd\u6570\u7684\u6548\u7387\u4e5f\u53d6\u51b3\u4e8e<code>__len__()<\/code>\u65b9\u6cd5\u7684\u5b9e\u73b0\u3002\u5982\u679c\u4e00\u4e2a\u7528\u6237\u81ea\u5b9a\u4e49\u5bf9\u8c61\u7684<code>__len__()<\/code>\u5b9e\u73b0\u975e\u5e38\u590d\u6742\uff0c\u90a3\u4e48\u8c03\u7528<code>len()<\/code>\u7684\u4ee3\u4ef7\u4e5f\u4f1a\u76f8\u5e94\u53d8\u9ad8\u3002<\/p>\n<\/p>\n<p><h3>\u4e94\u3001CPYTHON\u4e2dLEN()\u7684\u5b9e\u73b0\u7ec6\u8282<\/h3>\n<\/p>\n<p><p>\u5728CPython\u5b9e\u73b0\u4e2d\uff08\u5373Python\u5b98\u65b9\u5b9e\u73b0\uff09\uff0c<code>len()<\/code>\u662f\u7528C\u8bed\u8a00\u7f16\u5199\u7684\uff0c\u5e76\u4e14\u662f\u76f4\u63a5\u5d4c\u5165\u5230\u89e3\u91ca\u5668\u4e2d\u7684\u3002\u8fd9\u610f\u5473\u7740\uff0c\u5f53\u4f60\u8c03\u7528<code>len(obj)<\/code>\u65f6\uff0c\u4f60\u5b9e\u9645\u4e0a\u662f\u8c03\u7528\u4e86\u4e00\u4e2a\u5185\u7f6e\u7684C\u51fd\u6570\u3002\u5728\u6e90\u7801\u4e2d\uff0c<code>len()<\/code>\u51fd\u6570\u7684\u5b9e\u73b0\u901a\u5e38\u4f1a\u89c2\u5bdf\u5230\u662f\u4e00\u4e2a\u540d\u4e3a<code>PyObject_Size()<\/code> \u6216 <code>PyObject_Length()<\/code>\u7684\u51fd\u6570\u8c03\u7528\u3002<\/p>\n<\/p>\n<p><h3>\u516d\u3001\u7279\u6b8a\u60c5\u51b5\u4e0b\u7684LEN() \u51fd\u6570<\/h3>\n<\/p>\n<p><p>\u5c3d\u7ba1<code>len()<\/code>\u51fd\u6570\u5927\u591a\u6570\u65f6\u5019\u90fd\u53ef\u4ee5\u6b63\u5e38\u5de5\u4f5c\uff0c\u4f46\u4e5f\u6709\u4e9b\u7279\u6b8a\u7684\u60c5\u51b5\u9700\u8981\u6ce8\u610f\u3002\u4f8b\u5982\uff0c\u67d0\u4e9b\u7c7b\u578b\u53ef\u80fd\u4f1a\u7528\u4e00\u4e2a\u8d1f\u503c\u6765\u5b9e\u73b0<code>__len__()<\/code>\u65b9\u6cd5\uff0c\u4f46\u662f<code>len()<\/code>\u51fd\u6570\u8c03\u7528\u65f6\u4f1a\u51fa\u73b0<code>ValueError<\/code>\u9519\u8bef\uff0c\u56e0\u4e3a\u957f\u5ea6\u4e0d\u80fd\u662f\u8d1f\u6570\u3002<\/p>\n<\/p>\n<p><p>\u6b64\u5916\uff0c\u5f53\u4e00\u4e2a\u5bb9\u5668\u7c7b\u5f88\u5e9e\u5927\u65f6\uff0c\u4e14\u96be\u4ee5\u5728\u4e00\u4e2a\u6574\u578b\u53d8\u91cf\u4e2d\u5b58\u50a8\u5176\u957f\u5ea6\u65f6\uff0c<code>__len__()<\/code>\u65b9\u6cd5\u4f1a\u8fd4\u56de<code>OverflowError<\/code>\u3002\u8fd9\u901a\u5e38\u53ea\u4f1a\u5728\u5904\u7406\u6781\u5176\u5927\u7684\u6570\u636e\u96c6\u65f6\u9047\u5230\uff0c\u5927\u591a\u6570\u60c5\u51b5\u4e0b\u4e0d\u662f\u95ee\u9898\u3002<\/p>\n<\/p>\n<p><p>\u603b\u7684\u6765\u8bf4\uff0cPython\u4e2d\u7684<code>len()<\/code>\u51fd\u6570\u662f\u4e00\u79cd\u7b80\u5355\u76f4\u89c2\u4e14\u6548\u7387\u9ad8\u7684\u65b9\u5f0f\u6765\u83b7\u53d6\u6570\u636e\u96c6\u5408\u7684\u5927\u5c0f\uff0c\u5b83\u7684\u5b9e\u73b0\u786e\u4fdd\u4e86\u5728\u5185\u7f6e\u7c7b\u578b\u4e0a\u7684\u8c03\u7528\u901f\u5ea6\uff0c\u540c\u65f6\u4e5f\u63d0\u4f9b\u4e86\u5728\u81ea\u5b9a\u4e49\u7c7b\u578b\u4e0a\u8fdb\u884c\u7075\u6d3b\u5b9e\u73b0\u7684\u80fd\u529b\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p><strong>1. \u4e3a\u4ec0\u4e48len()\u51fd\u6570\u5728Python\u4e2d\u6267\u884c\u901f\u5ea6\u5f88\u5feb\uff1f<\/strong><\/p>\n<p>\u5728Python\u4e2d\uff0clen()\u51fd\u6570\u6267\u884c\u901f\u5ea6\u5f88\u5feb\u4e3b\u8981\u662f\u56e0\u4e3a\u5176\u5e95\u5c42\u5b9e\u73b0\u91c7\u7528\u4e86\u9ad8\u6548\u7684\u7b97\u6cd5\u548c\u6570\u636e\u7ed3\u6784\u3002Python\u4f7f\u7528\u4e86\u4e00\u79cd\u88ab\u79f0\u4e3a\u52a8\u6001\u6570\u7ec4\u7684\u6570\u636e\u7ed3\u6784\u6765\u5b58\u50a8\u53ef\u53d8\u957f\u5ea6\u5bf9\u8c61\u3002\u8fd9\u79cd\u6570\u636e\u7ed3\u6784\u5141\u8bb8\u5728\u5927\u90e8\u5206\u60c5\u51b5\u4e0b\u4ee5\u6052\u5b9a\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u6765\u83b7\u53d6\u5bf9\u8c61\u7684\u957f\u5ea6\u3002\u56e0\u6b64\uff0c\u5f53\u8c03\u7528len()\u51fd\u6570\u65f6\uff0cPython\u4f1a\u5feb\u901f\u5730\u8bbf\u95ee\u52a8\u6001\u6570\u7ec4\u7684\u957f\u5ea6\u5c5e\u6027\uff0c\u4ece\u800c\u5b9e\u73b0\u5feb\u901f\u8fd4\u56de\u7ed3\u679c\u3002<\/p>\n<p><strong>2. len()\u51fd\u6570\u7684\u5e95\u5c42\u5982\u4f55\u5904\u7406\u4e0d\u540c\u7c7b\u578b\u7684\u5bf9\u8c61\uff1f<\/strong><\/p>\n<p>\u5728Python\u4e2d\uff0clen()\u51fd\u6570\u5728\u5e95\u5c42\u5904\u7406\u4e0d\u540c\u7c7b\u578b\u7684\u5bf9\u8c61\u65f6\u4f1a\u6839\u636e\u5bf9\u8c61\u7684\u7279\u6027\u8fdb\u884c\u4e0d\u540c\u7684\u64cd\u4f5c\u3002\u4f8b\u5982\uff0c\u5bf9\u4e8e\u5b57\u7b26\u4e32\uff0clen()\u51fd\u6570\u4f1a\u76f4\u63a5\u8fd4\u56de\u5b57\u7b26\u4e32\u7684\u5b57\u7b26\u6570\uff1b\u5bf9\u4e8e\u5217\u8868\u6216\u5143\u7ec4\uff0clen()\u51fd\u6570\u4f1a\u8fd4\u56de\u5176\u5143\u7d20\u7684\u4e2a\u6570\uff1b\u5bf9\u4e8e\u5b57\u5178\uff0clen()\u51fd\u6570\u4f1a\u8fd4\u56de\u5176\u952e\u503c\u5bf9\u7684\u6570\u91cf\u3002<\/p>\n<p>\u5bf9\u4e8e\u81ea\u5b9a\u4e49\u7684\u5bf9\u8c61\u7c7b\u578b\uff0c\u53ef\u4ee5\u901a\u8fc7\u5b9a\u4e49__len__()\u65b9\u6cd5\u6765\u81ea\u5b9a\u4e49len()\u51fd\u6570\u7684\u884c\u4e3a\u3002\u8be5\u65b9\u6cd5\u5728\u5bf9\u8c61\u88ablen()\u51fd\u6570\u8c03\u7528\u65f6\u4f1a\u88ab\u8c03\u7528\uff0c\u7528\u4e8e\u8fd4\u56de\u5bf9\u8c61\u7684\u957f\u5ea6\u3002<\/p>\n<p><strong>3. len()\u51fd\u6570\u548c\u8fed\u4ee3\u5668\u7684\u5173\u7cfb\u662f\u4ec0\u4e48\uff1f<\/strong><\/p>\n<p>len()\u51fd\u6570\u548c\u8fed\u4ee3\u5668\u5728Python\u4e2d\u6709\u4e00\u5b9a\u7684\u5173\u8054\u3002\u867d\u7136len()\u51fd\u6570\u5728\u5e95\u5c42\u5b9e\u73b0\u4e2d\u4e0d\u4f1a\u76f4\u63a5\u4f7f\u7528\u8fed\u4ee3\u5668\uff0c\u4f46\u662flen()\u51fd\u6570\u5e38\u5e38\u4e0e\u8fed\u4ee3\u5668\u4e00\u8d77\u4f7f\u7528\u3002<\/p>\n<p>\u5f53\u5bf9\u4e00\u4e2a\u53ef\u8fed\u4ee3\u5bf9\u8c61\u4f7f\u7528len()\u51fd\u6570\u65f6\uff0clen()\u51fd\u6570\u4f1a\u81ea\u52a8\u4f7f\u7528\u8fed\u4ee3\u5668\u904d\u5386\u5bf9\u8c61\u7684\u5143\u7d20\uff0c\u5e76\u8ba1\u7b97\u51fa\u5143\u7d20\u7684\u603b\u6570\u3002\u8fd9\u6837\uff0c\u6211\u4eec\u5c31\u53ef\u4ee5\u901a\u8fc7len()\u51fd\u6570\u83b7\u53d6\u53ef\u8fed\u4ee3\u5bf9\u8c61\u7684\u957f\u5ea6\uff0c\u800c\u4e0d\u9700\u8981\u624b\u52a8\u904d\u5386\u5bf9\u8c61\u3002\u8fd9\u79cd\u65b9\u5f0f\u66f4\u52a0\u65b9\u4fbf\u4e14\u6548\u7387\u8f83\u9ad8\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python\u4e2d\u7684len()\u51fd\u6570\u662f\u901a\u8fc7\u8c03\u7528\u4e00\u4e2a\u5bf9\u8c61\u7684__len__()\u65b9\u6cd5\u6765\u83b7\u53d6\u5bf9\u8c61\u957f\u5ea6\u7684\u3001\u5e95\u5c42\u4f9d\u8d56\u4e8eC\u8bed\u8a00\u7684\u5b9e [&hellip;]","protected":false},"author":3,"featured_media":176442,"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\/176428"}],"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=176428"}],"version-history":[{"count":0,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/176428\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/176442"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=176428"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=176428"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=176428"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}