{"id":3516,"date":"2025-06-02T14:37:49","date_gmt":"2025-06-04T07:23:48","guid":{"rendered":"https:\/\/badgameshow.com\/steven\/?p=3516"},"modified":"2025-06-04T14:37:49","modified_gmt":"2025-06-04T07:23:48","slug":"%e4%ba%86%e8%a7%a3python%e4%b8%ad%e7%9a%84sizeof%e5%87%bd%e6%95%b8%e7%9a%84%e5%8a%9f%e8%83%bd%e8%88%87%e6%87%89%e7%94%a8","status":"publish","type":"post","link":"https:\/\/badgameshow.com\/steven\/python\/%e4%ba%86%e8%a7%a3python%e4%b8%ad%e7%9a%84sizeof%e5%87%bd%e6%95%b8%e7%9a%84%e5%8a%9f%e8%83%bd%e8%88%87%e6%87%89%e7%94%a8\/","title":{"rendered":"2025 \u6700\u65b0\u7248 Python sizeof() \u51fd\u6578\uff1a\u529f\u80fd\u3001\u4f7f\u7528\u65b9\u6cd5\u8207\u6700\u4f73\u5be6\u8e10"},"content":{"rendered":"<p><meta name=\"keywords\" content=\"Python, sizeof()\"><\/p>\n<h1>\u6df1\u5165\u4e86\u89e3 Python \u4e2d\u7684 sizeof() \u51fd\u6578<\/h1>\n<p>\u5728 Python \u4e2d\uff0c`sizeof()` \u51fd\u6578\u4e3b\u8981\u7528\u4f86\u7372\u53d6\u7269\u4ef6\u7684\u5927\u5c0f\u3002\u9019\u662f\u4e00\u500b\u5167\u5efa\u51fd\u6578\uff0c\u7121\u9700\u984d\u5916\u7684\u6a21\u7d44\u5c31\u53ef\u4ee5\u4f7f\u7528\u3002\u96d6\u7136\u5b83\u63d0\u4f9b\u4e86\u4e00\u500b\u5feb\u901f\u7684\u65b9\u6cd5\u4f86\u6aa2\u67e5\u7269\u4ef6\u7684\u5927\u5c0f\uff0c\u4f46\u7528\u6236\u61c9\u6ce8\u610f\u5176\u9650\u5236\u548c\u6700\u4f73\u5be6\u8e10\u3002<\/p>\n<h2>sizeof() \u51fd\u6578\u7684\u4f7f\u7528\u65b9\u6cd5<\/h2>\n<p>`sizeof()` \u51fd\u6578\u7684\u4f7f\u7528\u975e\u5e38\u7c21\u55ae\uff0c\u53ea\u9700\u8981\u50b3\u5165\u4e00\u500b\u7269\u4ef6\u5373\u53ef\u7372\u5f97\u5176\u5927\u5c0f\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u793a\u7bc4\uff1a<\/p>\n<p>&#8220;`python<br \/>\nimport sys<\/p>\n<p># \u53d6\u5f97\u4e00\u500b\u6574\u6578\u7684\u5927\u5c0f<br \/>\nprint(sys.getsizeof(1))  # \u8f38\u51fa\uff1a28\uff08\u572864\u4f4d\u7cfb\u7d71\u4e0a\uff09<\/p>\n<p># \u53d6\u5f97\u4e00\u500b\u5b57\u4e32\u7684\u5927\u5c0f<br \/>\nprint(sys.getsizeof(&#8216;Hello World&#8217;))  # \u8f38\u51fa\uff1a60\uff08\u572864\u4f4d\u7cfb\u7d71\u4e0a\uff09<\/p>\n<p># \u53d6\u5f97\u4e00\u500b\u5217\u8868\u7684\u5927\u5c0f<br \/>\nprint(sys.getsizeof([1, 2, 3]))  # \u8f38\u51fa\uff1a88\uff08\u572864\u4f4d\u7cfb\u7d71\u4e0a\uff09<br \/>\n&#8220;`<\/p>\n<p>\u4e0a\u9762\u7684\u7a0b\u5f0f\u78bc\u6703\u5370\u51fa\u5404\u7269\u4ef6\u7684\u5927\u5c0f\u3002<\/p>\n<h2>sizeof() \u51fd\u6578\u7684\u512a\u9ede<\/h2>\n<p>\u4f7f\u7528 `sizeof()` \u51fd\u6578\u7684\u512a\u9ede\u5728\u65bc\u5176\u7c21\u4fbf\u6027\u3002\u7121\u9700\u5f15\u5165\u5176\u4ed6\u6a21\u7d44\uff0c\u4f60\u53ef\u4ee5\u8f15\u9b06\u5730\u7372\u5f97\u7269\u4ef6\u7684\u5927\u5c0f\uff0c\u9019\u5728\u6027\u80fd\u5206\u6790\u548c\u8a18\u61b6\u9ad4\u7ba1\u7406\u6642\u975e\u5e38\u6709\u7528\u3002<\/p>\n<h2>sizeof() \u51fd\u6578\u7684\u7f3a\u9ede<\/h2>\n<p>\u7136\u800c\uff0c`sizeof()` \u51fd\u6578\u4e5f\u6709\u5176\u5c40\u9650\u6027\u3002\u5b83\u53ea\u80fd\u8fd4\u56de\u7269\u4ef6\u7684\u5927\u5c0f\uff0c\u800c\u7121\u6cd5\u63d0\u4f9b\u7269\u4ef6\u7684\u5167\u5bb9\u6216\u7d50\u69cb\u3002\u4f8b\u5982\uff1a<\/p>\n<p>&#8220;`python<br \/>\n# \u53d6\u5f97\u4e00\u500b\u5b57\u5178\u7684\u5927\u5c0f<br \/>\nprint(sys.getsizeof({&#8216;name&#8217;: &#8216;John&#8217;, &#8216;age&#8217;: 20}))  # \u8f38\u51fa\uff1a232\uff08\u572864\u4f4d\u7cfb\u7d71\u4e0a\uff09<br \/>\n&#8220;`<\/p>\n<p>\u9019\u6bb5\u7a0b\u5f0f\u78bc\u50c5\u80fd\u544a\u8a34\u4f60\u5b57\u5178\u7684\u5927\u5c0f\uff0c\u4f46\u7121\u6cd5\u544a\u8a34\u4f60\u5b57\u5178\u5167\u90e8\u7684\u5177\u9ad4\u5167\u5bb9\u3002\u5982\u679c\u4f60\u9700\u8981\u66f4\u6df1\u5165\u7684\u5206\u6790\uff0c\u53ef\u80fd\u9700\u8981\u8003\u616e\u5176\u4ed6\u6a21\u7d44\uff0c\u5982 `pandas` \u6216 `numpy`\u3002<\/p>\n<h2>\u5982\u4f55\u64f4\u5c55 sizeof() \u7684\u61c9\u7528<\/h2>\n<p>\u9664\u4e86 `sizeof()`\uff0c\u4f60\u9084\u53ef\u4ee5\u4f7f\u7528\u5176\u4ed6\u51fd\u6578\u4f86\u7372\u53d6\u66f4\u8a73\u7d30\u7684\u8cc7\u6599\u3002\u4f8b\u5982\uff0c`pandas` \u7684 `DataFrame` \u7269\u4ef6\u53ef\u4ee5\u7528 `memory_usage()` \u65b9\u6cd5\u4f86\u6aa2\u67e5\u8a18\u61b6\u9ad4\u4f7f\u7528\u60c5\u6cc1\uff1a<\/p>\n<p>&#8220;`python<br \/>\nimport pandas as pd<\/p>\n<p>df = pd.DataFrame({&#8216;A&#8217;: [1, 2, 3], &#8216;B&#8217;: [&#8216;a&#8217;, &#8216;b&#8217;, &#8216;c&#8217;]})<br \/>\nprint(df.memory_usage(deep=True))  # \u8f38\u51fa\u6bcf\u4e00\u5217\u7684\u8a18\u61b6\u9ad4\u4f7f\u7528\u60c5\u6cc1<br \/>\n&#8220;`<\/p>\n<p>\u9019\u6a23\u53ef\u4ee5\u8b93\u4f60\u66f4\u5168\u9762\u5730\u4e86\u89e3\u8cc7\u6599\u7d50\u69cb\u7684\u5927\u5c0f\u548c\u5167\u5bb9\u3002<\/p>\n<h2>\u7d50\u8ad6<\/h2>\n<p>\u5728 Python \u4e2d\uff0c`sizeof()` \u51fd\u6578\u662f\u4e00\u500b\u4fbf\u6377\u7684\u5de5\u5177\uff0c\u53ef\u4ee5\u5e6b\u52a9\u6211\u5011\u5feb\u901f\u7372\u5f97\u7269\u4ef6\u7684\u5927\u5c0f\u3002\u4e0d\u904e\uff0c\u5c0d\u65bc\u7269\u4ef6\u5167\u5bb9\u7684\u5206\u6790\uff0c\u5247\u9700\u8981\u501f\u52a9\u5176\u4ed6\u51fd\u6578\u6216\u6a21\u7d44\u3002\u900f\u904e\u4e86\u89e3\u9019\u4e9b\u5de5\u5177\u7684\u512a\u7f3a\u9ede\uff0c\u4f60\u53ef\u4ee5\u66f4\u6709\u6548\u5730\u9032\u884c\u7a0b\u5f0f\u8a2d\u8a08\u8207\u512a\u5316\u3002<\/p>\n<p>\u5982\u9700\u9032\u4e00\u6b65\u4e86\u89e3 Python \u7684\u5176\u4ed6\u529f\u80fd\u8207\u61c9\u7528\uff0c\u5efa\u8b70\u53c3\u8003 [vocus.cc \u7684 Python \u6559\u5b78\u6587\u7ae0](https:\/\/vocus.cc) \u6216 [miner.tw \u7684\u76f8\u95dc\u8cc7\u6e90](https:\/\/miner.tw)\u3002<\/p>\n<h2>\u5e38\u898b\u554f\u984c\u89e3\u7b54\uff08Q&#038;A\uff09<\/h2>\n<h3>Q1: sizeof() \u51fd\u6578\u53ef\u4ee5\u7528\u4f86\u8a08\u7b97\u54ea\u4e9b\u7269\u4ef6\u7684\u5927\u5c0f\uff1f<\/h3>\n<p>A1: `sizeof()` \u51fd\u6578\u53ef\u4ee5\u8a08\u7b97\u5404\u7a2e Python \u7269\u4ef6\u7684\u5927\u5c0f\uff0c\u5305\u62ec\u6574\u6578\u3001\u5b57\u4e32\u3001\u5217\u8868\u3001\u5b57\u5178\u7b49\u3002\u4e0d\u904e\uff0c\u5c0d\u65bc\u8907\u96dc\u7684\u7d50\u69cb\uff0c\u5efa\u8b70\u4f7f\u7528\u5176\u4ed6\u6a21\u7d44\u4f86\u7372\u53d6\u66f4\u8a73\u7d30\u7684\u8cc7\u8a0a\u3002<\/p>\n<h3>Q2: \u70ba\u4ec0\u9ebc\u6211\u5728\u4f7f\u7528 sizeof() \u6642\u5f97\u5230\u7684\u5927\u5c0f\u8207\u9810\u671f\u4e0d\u7b26\uff1f<\/h3>\n<p>A2: `sizeof()` \u8fd4\u56de\u7684\u662f\u7269\u4ef6\u7684\u5167\u90e8\u5927\u5c0f\uff0c\u9019\u53ef\u80fd\u8207\u5176\u5be6\u969b\u6240\u9700\u7684\u8a18\u61b6\u9ad4\u4e0d\u540c\u3002\u4f8b\u5982\uff0c\u5b57\u4e32\u5728\u5167\u90e8\u53ef\u80fd\u5305\u542b\u984d\u5916\u7684\u8a18\u61b6\u9ad4\u5206\u914d\uff0c\u5c0e\u81f4\u6578\u5b57\u504f\u5dee\u3002<\/p>\n<h3>Q3: \u54ea\u4e9b\u6a21\u7d44\u53ef\u4ee5\u5e6b\u52a9\u6211\u66f4\u597d\u5730\u7ba1\u7406\u8a18\u61b6\u9ad4\uff1f<\/h3>\n<p>A3: `sys` \u6a21\u7d44\u63d0\u4f9b\u4e86 `getsizeof()` \u51fd\u6578\uff0c\u6b64\u5916\uff0c`pandas` \u548c `numpy` \u4e5f\u6709\u52a9\u65bc\u66f4\u9ad8\u6548\u5730\u7ba1\u7406\u548c\u5206\u6790\u5927\u6578\u64da\u96c6\u7684\u8a18\u61b6\u9ad4\u4f7f\u7528\u60c5\u6cc1\u3002<\/p>\n<p>&#8212;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u6587\u7ae0\u6458\u8981(Description):\u4e86\u89e3Python\u4e2d\u7684sizeof()\u51fd\u6578\uff0c\u5b83\u53ef\u4ee5\u7528\u4f86\u67e5\u770b\u7269\u4ef6\u7684\u5927\u5c0f\uff0c\u53ef\u4ee5\u7528\u4f86\u6aa2\u67e5\u8a18\u61b6\u9ad4\u4f7f\u7528\u60c5\u6cc1\uff0c\u4e26\u63d0\u4f9b\u66f4\u591a\u7684\u8cc7\u8a0a\uff0c\u4ee5\u6539\u5584\u7a0b\u5f0f\u7684\u6548\u80fd\u3002<\/p>\n","protected":false},"author":1,"featured_media":2518,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[18],"tags":[15],"class_list":["post-3516","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-python","tag-python"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"https:\/\/badgameshow.com\/steven\/wp-content\/uploads\/2022\/12\/DALL\u00b7E-2022-12-28-14.25.55-\u62f7\u8c9d2.png","jetpack-related-posts":[],"jetpack_shortlink":"https:\/\/wp.me\/pcFK27-UI","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/3516","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/comments?post=3516"}],"version-history":[{"count":1,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/3516\/revisions"}],"predecessor-version":[{"id":3517,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/3516\/revisions\/3517"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/media\/2518"}],"wp:attachment":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/media?parent=3516"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/categories?post=3516"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/tags?post=3516"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}