{"id":1154476,"date":"2025-01-13T17:51:39","date_gmt":"2025-01-13T09:51:39","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1154476.html"},"modified":"2025-01-13T17:51:41","modified_gmt":"2025-01-13T09:51:41","slug":"python%e5%a6%82%e4%bd%95%e8%ae%a1%e7%ae%97byte%e9%95%bf%e5%ba%a6","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1154476.html","title":{"rendered":"python\u5982\u4f55\u8ba1\u7b97byte\u957f\u5ea6"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25184100\/116b8d8e-5459-4d59-ab3e-e700b87388e3.webp\" alt=\"python\u5982\u4f55\u8ba1\u7b97byte\u957f\u5ea6\" \/><\/p>\n<p><p> <strong>\u5728Python\u4e2d\uff0c\u8ba1\u7b97\u5b57\u8282\u957f\u5ea6\u7684\u65b9\u6cd5\u6709\u591a\u79cd\uff0c\u901a\u5e38\u4f7f\u7528len()\u51fd\u6570\u3001sys.getsizeof()\u65b9\u6cd5\u3001encode()\u65b9\u6cd5\u3002<\/strong> \u6bd4\u5982\uff0clen()\u51fd\u6570\u53ef\u4ee5\u76f4\u63a5\u8ba1\u7b97\u5b57\u7b26\u4e32\u7684\u5b57\u8282\u957f\u5ea6\uff0csys.getsizeof()\u65b9\u6cd5\u53ef\u4ee5\u8ba1\u7b97\u5bf9\u8c61\u7684\u5185\u5b58\u5360\u7528\uff0cencode()\u65b9\u6cd5\u5219\u53ef\u4ee5\u5c06\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u5b57\u8282\u5bf9\u8c61\u5e76\u8ba1\u7b97\u5176\u957f\u5ea6\u3002\u4f7f\u7528len()\u51fd\u6570\u6765\u8ba1\u7b97\u5b57\u8282\u957f\u5ea6\u662f\u975e\u5e38\u5e38\u89c1\u4e14\u7b80\u5355\u7684\u4e00\u79cd\u65b9\u5f0f\uff0c\u5b83\u76f4\u63a5\u8fd4\u56de\u5b57\u7b26\u4e32\u7684\u5b57\u8282\u6570\u3002<\/p>\n<\/p>\n<p><p><strong>\u8be6\u7ec6\u63cf\u8ff0\uff1a<\/strong>len()\u51fd\u6570\u662fPython\u5185\u7f6e\u7684\u51fd\u6570\uff0c\u53ef\u4ee5\u76f4\u63a5\u7528\u4e8e\u8ba1\u7b97\u5b57\u7b26\u4e32\u7684\u5b57\u8282\u957f\u5ea6\u3002\u5bf9\u4e8e\u5b57\u7b26\u4e32\uff0clen()\u51fd\u6570\u8fd4\u56de\u7684\u662f\u5b57\u7b26\u6570\uff0c\u800c\u5bf9\u4e8e\u5b57\u8282\u5bf9\u8c61\uff0clen()\u51fd\u6570\u8fd4\u56de\u7684\u662f\u5b57\u8282\u6570\u3002\u56e0\u6b64\uff0c\u5f53\u9700\u8981\u8ba1\u7b97\u5b57\u7b26\u4e32\u7684\u5b57\u8282\u957f\u5ea6\u65f6\uff0c\u9996\u5148\u9700\u8981\u5c06\u5b57\u7b26\u4e32\u7f16\u7801\u4e3a\u5b57\u8282\u5bf9\u8c61\uff0c\u7136\u540e\u4f7f\u7528len()\u51fd\u6570\u3002\u4f8b\u5982\uff0c\u4f7f\u7528UTF-8\u7f16\u7801\u65b9\u5f0f\u53ef\u4ee5\u5c06\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u5b57\u8282\u5bf9\u8c61\uff0c\u5e76\u8ba1\u7b97\u5176\u5b57\u8282\u957f\u5ea6\uff0c\u5982<code>len(my_string.encode(&#39;utf-8&#39;))<\/code>\u3002<\/p>\n<\/p>\n<p><p>\u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u8fd9\u4e9b\u65b9\u6cd5\u53ca\u5176\u5e94\u7528\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001len()\u51fd\u6570<\/h3>\n<\/p>\n<p><p>len()\u51fd\u6570\u662fPython\u5185\u7f6e\u7684\u51fd\u6570\uff0c\u53ef\u4ee5\u76f4\u63a5\u7528\u4e8e\u8ba1\u7b97\u5b57\u7b26\u4e32\u7684\u5b57\u8282\u957f\u5ea6\u3002\u5bf9\u4e8e\u5b57\u7b26\u4e32\uff0clen()\u51fd\u6570\u8fd4\u56de\u7684\u662f\u5b57\u7b26\u6570\uff0c\u800c\u5bf9\u4e8e\u5b57\u8282\u5bf9\u8c61\uff0clen()\u51fd\u6570\u8fd4\u56de\u7684\u662f\u5b57\u8282\u6570\u3002\u56e0\u6b64\uff0c\u5f53\u9700\u8981\u8ba1\u7b97\u5b57\u7b26\u4e32\u7684\u5b57\u8282\u957f\u5ea6\u65f6\uff0c\u9996\u5148\u9700\u8981\u5c06\u5b57\u7b26\u4e32\u7f16\u7801\u4e3a\u5b57\u8282\u5bf9\u8c61\uff0c\u7136\u540e\u4f7f\u7528len()\u51fd\u6570\u3002<\/p>\n<\/p>\n<p><h4>1.1 \u5b57\u7b26\u4e32\u7684\u5b57\u8282\u957f\u5ea6\u8ba1\u7b97<\/h4>\n<\/p>\n<p><p>\u5bf9\u4e8e\u5b57\u7b26\u4e32\uff0c\u53ef\u4ee5\u4f7f\u7528encode()\u65b9\u6cd5\u5c06\u5176\u8f6c\u6362\u4e3a\u5b57\u8282\u5bf9\u8c61\uff0c\u518d\u4f7f\u7528len()\u51fd\u6570\u8ba1\u7b97\u5b57\u8282\u957f\u5ea6\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">my_string = &quot;Hello, World!&quot;<\/p>\n<p>byte_length = len(my_string.encode(&#39;utf-8&#39;))<\/p>\n<p>print(f&quot;Byte length of the string: {byte_length}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c<code>my_string.encode(&#39;utf-8&#39;)<\/code>\u5c06\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3aUTF-8\u7f16\u7801\u7684\u5b57\u8282\u5bf9\u8c61\uff0c\u7136\u540e<code>len()<\/code>\u51fd\u6570\u8ba1\u7b97\u5176\u5b57\u8282\u957f\u5ea6\u3002<\/p>\n<\/p>\n<p><h4>1.2 \u5b57\u8282\u5bf9\u8c61\u7684\u5b57\u8282\u957f\u5ea6\u8ba1\u7b97<\/h4>\n<\/p>\n<p><p>\u5bf9\u4e8e\u5b57\u8282\u5bf9\u8c61\uff0clen()\u51fd\u6570\u53ef\u4ee5\u76f4\u63a5\u8ba1\u7b97\u5176\u5b57\u8282\u957f\u5ea6\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">my_bytes = b&quot;Hello, World!&quot;<\/p>\n<p>byte_length = len(my_bytes)<\/p>\n<p>print(f&quot;Byte length of the byte object: {byte_length}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c<code>len(my_bytes)<\/code>\u76f4\u63a5\u8ba1\u7b97\u5b57\u8282\u5bf9\u8c61\u7684\u5b57\u8282\u957f\u5ea6\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001sys.getsizeof()\u65b9\u6cd5<\/h3>\n<\/p>\n<p><p>sys.getsizeof()\u65b9\u6cd5\u7528\u4e8e\u8ba1\u7b97\u5bf9\u8c61\u7684\u5185\u5b58\u5360\u7528\u3002\u9700\u8981\u6ce8\u610f\u7684\u662f\uff0csys.getsizeof()\u65b9\u6cd5\u8ba1\u7b97\u7684\u662f\u5bf9\u8c61\u7684\u603b\u5185\u5b58\u5360\u7528\uff0c\u800c\u4e0d\u4ec5\u4ec5\u662f\u6570\u636e\u90e8\u5206\u7684\u5185\u5b58\u5360\u7528\u3002<\/p>\n<\/p>\n<p><h4>2.1 \u5b57\u7b26\u4e32\u7684\u5185\u5b58\u5360\u7528\u8ba1\u7b97<\/h4>\n<\/p>\n<p><p>\u5bf9\u4e8e\u5b57\u7b26\u4e32\uff0c\u53ef\u4ee5\u4f7f\u7528sys.getsizeof()\u65b9\u6cd5\u8ba1\u7b97\u5176\u5185\u5b58\u5360\u7528\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import sys<\/p>\n<p>my_string = &quot;Hello, World!&quot;<\/p>\n<p>memory_size = sys.getsizeof(my_string)<\/p>\n<p>print(f&quot;Memory size of the string: {memory_size}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0csys.getsizeof(my_string)\u8ba1\u7b97\u5b57\u7b26\u4e32\u5bf9\u8c61\u7684\u5185\u5b58\u5360\u7528\u3002<\/p>\n<\/p>\n<p><h4>2.2 \u5b57\u8282\u5bf9\u8c61\u7684\u5185\u5b58\u5360\u7528\u8ba1\u7b97<\/h4>\n<\/p>\n<p><p>\u5bf9\u4e8e\u5b57\u8282\u5bf9\u8c61\uff0c\u53ef\u4ee5\u4f7f\u7528sys.getsizeof()\u65b9\u6cd5\u8ba1\u7b97\u5176\u5185\u5b58\u5360\u7528\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import sys<\/p>\n<p>my_bytes = b&quot;Hello, World!&quot;<\/p>\n<p>memory_size = sys.getsizeof(my_bytes)<\/p>\n<p>print(f&quot;Memory size of the byte object: {memory_size}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0csys.getsizeof(my_bytes)\u8ba1\u7b97\u5b57\u8282\u5bf9\u8c61\u7684\u5185\u5b58\u5360\u7528\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001encode()\u65b9\u6cd5<\/h3>\n<\/p>\n<p><p>encode()\u65b9\u6cd5\u7528\u4e8e\u5c06\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u5b57\u8282\u5bf9\u8c61\u3002\u4e0d\u540c\u7684\u7f16\u7801\u65b9\u5f0f\u4f1a\u4ea7\u751f\u4e0d\u540c\u7684\u5b57\u8282\u957f\u5ea6\u3002\u4f8b\u5982\uff0cUTF-8\u7f16\u7801\u548cUTF-16\u7f16\u7801\u4f1a\u4ea7\u751f\u4e0d\u540c\u7684\u5b57\u8282\u957f\u5ea6\u3002<\/p>\n<\/p>\n<p><h4>3.1 UTF-8\u7f16\u7801<\/h4>\n<\/p>\n<p><p>UTF-8\u7f16\u7801\u662f\u4e00\u79cd\u5e38\u89c1\u7684\u7f16\u7801\u65b9\u5f0f\uff0c\u53ef\u4ee5\u5c06\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u5b57\u8282\u5bf9\u8c61\uff0c\u5e76\u8ba1\u7b97\u5176\u5b57\u8282\u957f\u5ea6\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">my_string = &quot;Hello, World!&quot;<\/p>\n<p>utf8_bytes = my_string.encode(&#39;utf-8&#39;)<\/p>\n<p>byte_length = len(utf8_bytes)<\/p>\n<p>print(f&quot;Byte length of the string (UTF-8): {byte_length}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0cmy_string.encode(&#39;utf-8&#39;)\u5c06\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3aUTF-8\u7f16\u7801\u7684\u5b57\u8282\u5bf9\u8c61\uff0c\u7136\u540elen(utf8_bytes)\u8ba1\u7b97\u5176\u5b57\u8282\u957f\u5ea6\u3002<\/p>\n<\/p>\n<p><h4>3.2 UTF-16\u7f16\u7801<\/h4>\n<\/p>\n<p><p>UTF-16\u7f16\u7801\u662f\u53e6\u4e00\u79cd\u5e38\u89c1\u7684\u7f16\u7801\u65b9\u5f0f\uff0c\u53ef\u4ee5\u5c06\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u5b57\u8282\u5bf9\u8c61\uff0c\u5e76\u8ba1\u7b97\u5176\u5b57\u8282\u957f\u5ea6\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">my_string = &quot;Hello, World!&quot;<\/p>\n<p>utf16_bytes = my_string.encode(&#39;utf-16&#39;)<\/p>\n<p>byte_length = len(utf16_bytes)<\/p>\n<p>print(f&quot;Byte length of the string (UTF-16): {byte_length}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0cmy_string.encode(&#39;utf-16&#39;)\u5c06\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3aUTF-16\u7f16\u7801\u7684\u5b57\u8282\u5bf9\u8c61\uff0c\u7136\u540elen(utf16_bytes)\u8ba1\u7b97\u5176\u5b57\u8282\u957f\u5ea6\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001\u5176\u4ed6\u7f16\u7801\u65b9\u5f0f<\/h3>\n<\/p>\n<p><p>\u9664\u4e86UTF-8\u548cUTF-16\u7f16\u7801\uff0c\u8fd8\u53ef\u4ee5\u4f7f\u7528\u5176\u4ed6\u7f16\u7801\u65b9\u5f0f\u5c06\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u5b57\u8282\u5bf9\u8c61\uff0c\u5e76\u8ba1\u7b97\u5176\u5b57\u8282\u957f\u5ea6\u3002\u4f8b\u5982\uff0cASCII\u7f16\u7801\u548cGBK\u7f16\u7801\u3002<\/p>\n<\/p>\n<p><h4>4.1 ASCII\u7f16\u7801<\/h4>\n<\/p>\n<p><p>ASCII\u7f16\u7801\u662f\u4e00\u79cd\u57fa\u672c\u7684\u7f16\u7801\u65b9\u5f0f\uff0c\u53ef\u4ee5\u5c06\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u5b57\u8282\u5bf9\u8c61\uff0c\u5e76\u8ba1\u7b97\u5176\u5b57\u8282\u957f\u5ea6\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">my_string = &quot;Hello, World!&quot;<\/p>\n<p>ascii_bytes = my_string.encode(&#39;ascii&#39;)<\/p>\n<p>byte_length = len(ascii_bytes)<\/p>\n<p>print(f&quot;Byte length of the string (ASCII): {byte_length}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0cmy_string.encode(&#39;ascii&#39;)\u5c06\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3aASCII\u7f16\u7801\u7684\u5b57\u8282\u5bf9\u8c61\uff0c\u7136\u540elen(ascii_bytes)\u8ba1\u7b97\u5176\u5b57\u8282\u957f\u5ea6\u3002<\/p>\n<\/p>\n<p><h4>4.2 GBK\u7f16\u7801<\/h4>\n<\/p>\n<p><p>GBK\u7f16\u7801\u662f\u4e00\u79cd\u5e38\u89c1\u7684\u4e2d\u6587\u7f16\u7801\u65b9\u5f0f\uff0c\u53ef\u4ee5\u5c06\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u5b57\u8282\u5bf9\u8c61\uff0c\u5e76\u8ba1\u7b97\u5176\u5b57\u8282\u957f\u5ea6\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">my_string = &quot;\u4f60\u597d\uff0c\u4e16\u754c\uff01&quot;<\/p>\n<p>gbk_bytes = my_string.encode(&#39;gbk&#39;)<\/p>\n<p>byte_length = len(gbk_bytes)<\/p>\n<p>print(f&quot;Byte length of the string (GBK): {byte_length}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0cmy_string.encode(&#39;gbk&#39;)\u5c06\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3aGBK\u7f16\u7801\u7684\u5b57\u8282\u5bf9\u8c61\uff0c\u7136\u540elen(gbk_bytes)\u8ba1\u7b97\u5176\u5b57\u8282\u957f\u5ea6\u3002<\/p>\n<\/p>\n<p><h3>\u4e94\u3001\u5904\u7406\u4e0d\u540c\u7c7b\u578b\u7684\u6570\u636e<\/h3>\n<\/p>\n<p><p>\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u53ef\u80fd\u9700\u8981\u5904\u7406\u4e0d\u540c\u7c7b\u578b\u7684\u6570\u636e\u5e76\u8ba1\u7b97\u5176\u5b57\u8282\u957f\u5ea6\u3002\u4f8b\u5982\uff0c\u5904\u7406\u5217\u8868\u3001\u5b57\u5178\u548c\u81ea\u5b9a\u4e49\u5bf9\u8c61\u3002<\/p>\n<\/p>\n<p><h4>5.1 \u5217\u8868\u7684\u5b57\u8282\u957f\u5ea6\u8ba1\u7b97<\/h4>\n<\/p>\n<p><p>\u5bf9\u4e8e\u5217\u8868\uff0c\u53ef\u4ee5\u8ba1\u7b97\u5176\u6bcf\u4e2a\u5143\u7d20\u7684\u5b57\u8282\u957f\u5ea6\uff0c\u5e76\u6c42\u548c\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">my_list = [&quot;Hello&quot;, &quot;World&quot;, &quot;!&quot;]<\/p>\n<p>total_byte_length = sum(len(item.encode(&#39;utf-8&#39;)) for item in my_list)<\/p>\n<p>print(f&quot;Total byte length of the list: {total_byte_length}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u4f7f\u7528\u751f\u6210\u5668\u8868\u8fbe\u5f0f\u5c06\u5217\u8868\u4e2d\u7684\u6bcf\u4e2a\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3aUTF-8\u7f16\u7801\u7684\u5b57\u8282\u5bf9\u8c61\uff0c\u5e76\u8ba1\u7b97\u5176\u5b57\u8282\u957f\u5ea6\uff0c\u7136\u540e\u4f7f\u7528sum()\u51fd\u6570\u6c42\u548c\u3002<\/p>\n<\/p>\n<p><h4>5.2 \u5b57\u5178\u7684\u5b57\u8282\u957f\u5ea6\u8ba1\u7b97<\/h4>\n<\/p>\n<p><p>\u5bf9\u4e8e\u5b57\u5178\uff0c\u53ef\u4ee5\u8ba1\u7b97\u5176\u6bcf\u4e2a\u952e\u503c\u5bf9\u7684\u5b57\u8282\u957f\u5ea6\uff0c\u5e76\u6c42\u548c\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">my_dict = {&quot;key1&quot;: &quot;value1&quot;, &quot;key2&quot;: &quot;value2&quot;}<\/p>\n<p>total_byte_length = sum(len(key.encode(&#39;utf-8&#39;)) + len(value.encode(&#39;utf-8&#39;)) for key, value in my_dict.items())<\/p>\n<p>print(f&quot;Total byte length of the dictionary: {total_byte_length}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u4f7f\u7528\u751f\u6210\u5668\u8868\u8fbe\u5f0f\u5c06\u5b57\u5178\u4e2d\u7684\u6bcf\u4e2a\u952e\u548c\u503c\u8f6c\u6362\u4e3aUTF-8\u7f16\u7801\u7684\u5b57\u8282\u5bf9\u8c61\uff0c\u5e76\u8ba1\u7b97\u5176\u5b57\u8282\u957f\u5ea6\uff0c\u7136\u540e\u4f7f\u7528sum()\u51fd\u6570\u6c42\u548c\u3002<\/p>\n<\/p>\n<p><h4>5.3 \u81ea\u5b9a\u4e49\u5bf9\u8c61\u7684\u5b57\u8282\u957f\u5ea6\u8ba1\u7b97<\/h4>\n<\/p>\n<p><p>\u5bf9\u4e8e\u81ea\u5b9a\u4e49\u5bf9\u8c61\uff0c\u53ef\u4ee5\u5b9a\u4e49\u4e00\u4e2a\u65b9\u6cd5\u6765\u8ba1\u7b97\u5176\u5c5e\u6027\u7684\u5b57\u8282\u957f\u5ea6\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">class MyObject:<\/p>\n<p>    def __init__(self, name, value):<\/p>\n<p>        self.name = name<\/p>\n<p>        self.value = value<\/p>\n<p>    def byte_length(self):<\/p>\n<p>        return len(self.name.encode(&#39;utf-8&#39;)) + len(self.value.encode(&#39;utf-8&#39;))<\/p>\n<p>obj = MyObject(&quot;name&quot;, &quot;value&quot;)<\/p>\n<p>byte_length = obj.byte_length()<\/p>\n<p>print(f&quot;Byte length of the custom object: {byte_length}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u81ea\u5b9a\u4e49\u5bf9\u8c61MyObject\u5b9a\u4e49\u4e86\u4e00\u4e2abyte_length()\u65b9\u6cd5\u6765\u8ba1\u7b97\u5176\u5c5e\u6027name\u548cvalue\u7684\u5b57\u8282\u957f\u5ea6\u3002<\/p>\n<\/p>\n<p><h3>\u516d\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u5728Python\u4e2d\uff0c\u8ba1\u7b97\u5b57\u8282\u957f\u5ea6\u7684\u65b9\u6cd5\u6709\u591a\u79cd\uff0c\u901a\u5e38\u4f7f\u7528len()\u51fd\u6570\u3001sys.getsizeof()\u65b9\u6cd5\u3001encode()\u65b9\u6cd5\u3002len()\u51fd\u6570\u53ef\u4ee5\u76f4\u63a5\u8ba1\u7b97\u5b57\u7b26\u4e32\u548c\u5b57\u8282\u5bf9\u8c61\u7684\u5b57\u8282\u957f\u5ea6\uff0csys.getsizeof()\u65b9\u6cd5\u53ef\u4ee5\u8ba1\u7b97\u5bf9\u8c61\u7684\u5185\u5b58\u5360\u7528\uff0cencode()\u65b9\u6cd5\u53ef\u4ee5\u5c06\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u5b57\u8282\u5bf9\u8c61\u5e76\u8ba1\u7b97\u5176\u5b57\u8282\u957f\u5ea6\u3002\u6839\u636e\u5b9e\u9645\u9700\u6c42\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\uff0c\u53ef\u4ee5\u6709\u6548\u5730\u8ba1\u7b97\u4e0d\u540c\u7c7b\u578b\u6570\u636e\u7684\u5b57\u8282\u957f\u5ea6\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u8ba1\u7b97\u5b57\u7b26\u4e32\u7684\u5b57\u8282\u957f\u5ea6\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528<code>encode()<\/code>\u65b9\u6cd5\u5c06\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u5b57\u8282\uff0c\u7136\u540e\u4f7f\u7528<code>len()<\/code>\u51fd\u6570\u8ba1\u7b97\u5176\u5b57\u8282\u957f\u5ea6\u3002\u5177\u4f53\u793a\u4f8b\u5982\u4e0b\uff1a<\/p>\n<pre><code class=\"language-python\">text = &quot;Hello, World!&quot;\nbyte_length = len(text.encode(&#39;utf-8&#39;))\nprint(byte_length)  # \u8f93\u51fa\u5b57\u8282\u957f\u5ea6\n<\/code><\/pre>\n<p>\u8fd9\u91cc\uff0c<code>utf-8<\/code>\u662f\u5e38\u7528\u7684\u7f16\u7801\u65b9\u5f0f\uff0c\u53ef\u4ee5\u6839\u636e\u9700\u8981\u9009\u62e9\u5176\u4ed6\u7f16\u7801\uff0c\u5982<code>ascii<\/code>\u6216<code>utf-16<\/code>\u3002<\/p>\n<p><strong>Python\u4e2d\u5982\u4f55\u8ba1\u7b97\u6587\u4ef6\u7684\u5b57\u8282\u5927\u5c0f\uff1f<\/strong><br \/>\u8981\u8ba1\u7b97\u6587\u4ef6\u7684\u5b57\u8282\u5927\u5c0f\uff0c\u53ef\u4ee5\u4f7f\u7528Python\u7684\u5185\u7f6e<code>os<\/code>\u6a21\u5757\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u793a\u4f8b\uff1a<\/p>\n<pre><code class=\"language-python\">import os\n\nfile_path = &#39;example.txt&#39;\nfile_size = os.path.getsize(file_path)\nprint(file_size)  # \u8f93\u51fa\u6587\u4ef6\u7684\u5b57\u8282\u5927\u5c0f\n<\/code><\/pre>\n<p>\u8fd9\u6bb5\u4ee3\u7801\u4f1a\u8fd4\u56de\u6307\u5b9a\u6587\u4ef6\u7684\u5b57\u8282\u5927\u5c0f\uff0c\u9002\u5408\u7528\u4e8e\u6587\u4ef6\u7ba1\u7406\u548c\u5206\u6790\u3002<\/p>\n<p><strong>\u5728Python\u4e2d\u5982\u4f55\u8ba1\u7b97\u5217\u8868\u4e2d\u6bcf\u4e2a\u5143\u7d20\u7684\u5b57\u8282\u957f\u5ea6\uff1f<\/strong><br \/>\u53ef\u4ee5\u4f7f\u7528\u5217\u8868\u63a8\u5bfc\u5f0f\u7ed3\u5408<code>sys.getsizeof()<\/code>\u6765\u8ba1\u7b97\u5217\u8868\u4e2d\u6bcf\u4e2a\u5143\u7d20\u7684\u5b57\u8282\u957f\u5ea6\u3002\u4f8b\u5982\uff1a<\/p>\n<pre><code class=\"language-python\">import sys\n\nmy_list = [&#39;apple&#39;, &#39;banana&#39;, &#39;cherry&#39;]\nbyte_lengths = [sys.getsizeof(item) for item in my_list]\nprint(byte_lengths)  # \u8f93\u51fa\u6bcf\u4e2a\u5143\u7d20\u7684\u5b57\u8282\u957f\u5ea6\n<\/code><\/pre>\n<p>\u8fd9\u79cd\u65b9\u6cd5\u53ef\u4ee5\u5e2e\u52a9\u4f60\u5206\u6790\u5217\u8868\u4e2d\u5bf9\u8c61\u7684\u5185\u5b58\u4f7f\u7528\u60c5\u51b5\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\uff0c\u8ba1\u7b97\u5b57\u8282\u957f\u5ea6\u7684\u65b9\u6cd5\u6709\u591a\u79cd\uff0c\u901a\u5e38\u4f7f\u7528len()\u51fd\u6570\u3001sys.getsizeof()\u65b9\u6cd5\u3001enc [&hellip;]","protected":false},"author":3,"featured_media":1154484,"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\/1154476"}],"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=1154476"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1154476\/revisions"}],"predecessor-version":[{"id":1154485,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1154476\/revisions\/1154485"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1154484"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1154476"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1154476"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1154476"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}