{"id":3098,"date":"2025-05-31T16:39:01","date_gmt":"2025-06-04T08:56:01","guid":{"rendered":"https:\/\/badgameshow.com\/steven\/?p=3098"},"modified":"2025-06-04T16:39:01","modified_gmt":"2025-06-04T08:56:01","slug":"python%e4%b8%ad%e7%9a%84sizeof%e5%87%bd%e6%95%b8","status":"publish","type":"post","link":"https:\/\/badgameshow.com\/steven\/python\/python%e4%b8%ad%e7%9a%84sizeof%e5%87%bd%e6%95%b8\/","title":{"rendered":"2025 \u6700\u65b0 Python \u7a0b\u5f0f\u6559\u5b78\uff1a\u6df1\u5165\u4e86\u89e3 sizeof() \u51fd\u6578\u7684\u4f7f\u7528\u8207\u61c9\u7528"},"content":{"rendered":"<p><meta name=\"keywords\" content=\"Python, sizeof(), Python \u6559\u5b78, sizeof \u51fd\u6578\"><\/p>\n<h1>2025 \u6700\u65b0 Python \u4e2d\u7684 sizeof() \u51fd\u5f0f<\/h1>\n<p>\u5728 Python \u4e2d\uff0c`sizeof()` \u51fd\u6578\u53ef\u7528\u65bc\u7372\u53d6\u7269\u4ef6\u7684\u5927\u5c0f\uff0c\u9019\u662f\u4e00\u500b\u975e\u5e38\u6709\u7528\u7684\u5167\u5efa\u51fd\u6578\uff0c\u53ef\u4ee5\u5e6b\u52a9\u958b\u767c\u8005\u66f4\u597d\u5730\u4e86\u89e3\u4ed6\u5011\u7684\u6578\u64da\u7d50\u69cb\u3002\u96a8\u8457 Python \u8a9e\u8a00\u7684\u6f14\u9032\uff0c\u7406\u89e3\u5982\u4f55\u6709\u6548\u5229\u7528\u9019\u500b\u51fd\u6578\u8b8a\u5f97\u5c24\u70ba\u91cd\u8981\u3002<\/p>\n<h2>sizeof() \u51fd\u5f0f\u7684\u4f7f\u7528\u65b9\u6cd5<\/h2>\n<p>`sizeof()` \u51fd\u6578\u7684\u4f7f\u7528\u65b9\u6cd5\u975e\u5e38\u7c21\u55ae\uff0c\u60a8\u53ea\u9700\u50b3\u5165\u4e00\u500b\u7269\u4ef6\uff0c\u4fbf\u80fd\u7372\u5f97\u5176\u5927\u5c0f\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u793a\u4f8b\uff1a<\/p>\n<p>&#8220;`python<br \/>\nimport sys  # \u78ba\u4fdd\u5c0e\u5165 sys \u6a21\u7d44\u4ee5\u4f7f\u7528 sizeof \u51fd\u6578<\/p>\n<p># \u53d6\u5f97\u4e00\u500b\u6574\u6578\u7684\u5927\u5c0f<br \/>\nprint(sys.getsizeof(1))<\/p>\n<p># \u53d6\u5f97\u4e00\u500b\u5b57\u4e32\u7684\u5927\u5c0f<br \/>\nprint(sys.getsizeof(&#8216;Hello World&#8217;))<\/p>\n<p># \u53d6\u5f97\u4e00\u500b\u5217\u8868\u7684\u5927\u5c0f<br \/>\nprint(sys.getsizeof([1, 2, 3]))<br \/>\n&#8220;`<\/p>\n<p>\u4e0a\u8ff0\u7a0b\u5f0f\u78bc\u7684\u8f38\u51fa\u7d50\u679c\u5982\u4e0b\uff1a<\/p>\n<p>&#8220;`<br \/>\n28<br \/>\n60<br \/>\n64<br \/>\n&#8220;`<\/p>\n<p>\u6ce8\u610f\uff1a\u5728 Python \u4e2d\uff0c\u6574\u6578\u548c\u5b57\u4e32\u7684\u5927\u5c0f\u53ef\u80fd\u6703\u56e0\u7248\u672c\u548c\u5e73\u53f0\u800c\u7570\u3002<\/p>\n<h2>sizeof() \u51fd\u5f0f\u7684\u512a\u9ede<\/h2>\n<p>\u4f7f\u7528 `sizeof()` \u51fd\u6578\u7684\u4e3b\u8981\u512a\u9ede\u5728\u65bc\u5b83\u53ef\u4ee5\u8f15\u9b06\u5730\u7372\u53d6\u7269\u4ef6\u7684\u5927\u5c0f\uff0c\u800c\u7121\u9700\u984d\u5916\u7684\u6a21\u7d44\u6216\u624b\u52d5\u8a08\u7b97\u3002\u9019\u4f7f\u5f97\u958b\u767c\u904e\u7a0b\u66f4\u70ba\u9ad8\u6548\uff0c\u5c24\u5176\u662f\u5728\u512a\u5316\u5167\u5b58\u4f7f\u7528\u6642\u3002<\/p>\n<h2>sizeof() \u51fd\u5f0f\u7684\u7f3a\u9ede<\/h2>\n<p>\u5118\u7ba1 `sizeof()` \u51fd\u6578\u7c21\u55ae\u6613\u7528\uff0c\u4f46\u5b83\u6709\u5176\u9650\u5236\u3002\u4f8b\u5982\uff0c\u8a72\u51fd\u6578\u50c5\u80fd\u7372\u53d6\u7269\u4ef6\u7684\u5927\u5c0f\uff0c\u800c\u7121\u6cd5\u986f\u793a\u7269\u4ef6\u7684\u5167\u5bb9\u3002\u4ee5\u4e0b\u662f\u4e00\u500b\u793a\u4f8b\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}))<br \/>\n&#8220;`<\/p>\n<p>\u9019\u6bb5\u7a0b\u5f0f\u78bc\u7684\u8f38\u51fa\u7d50\u679c\u70ba\uff1a<\/p>\n<p>&#8220;`<br \/>\n240<br \/>\n&#8220;`<\/p>\n<p>\u5982\u60a8\u6240\u898b\uff0c`sizeof()` \u50c5\u8fd4\u56de\u5b57\u5178\u7684\u5927\u5c0f\uff0c\u800c\u7121\u6cd5\u63d0\u4f9b\u5b57\u5178\u5167\u90e8\u7684\u7d50\u69cb\u6216\u5167\u5bb9\u3002<\/p>\n<h2>\u6700\u4f73\u5be6\u8e10\u8207\u5ef6\u4f38\u61c9\u7528<\/h2>\n<p>\u5728\u4f7f\u7528 `sizeof()` \u51fd\u6578\u6642\uff0c\u5efa\u8b70\u8207\u5176\u4ed6\u5167\u5b58\u7ba1\u7406\u5de5\u5177\u7d50\u5408\u4f7f\u7528\uff0c\u4f8b\u5982 `gc` \u6a21\u7d44\uff0c\u53ef\u4ee5\u5e6b\u52a9\u60a8\u66f4\u597d\u5730\u7ba1\u7406\u5167\u5b58\u3002\u6b64\u5916\uff0c\u82e5\u9700\u8981\u7372\u53d6\u66f4\u8a73\u7d30\u7684\u7269\u4ef6\u8cc7\u8a0a\uff0c\u53ef\u4ee5\u8003\u616e\u4f7f\u7528 `pprint` \u6a21\u7d44\u4f86\u7f8e\u5316\u8f38\u51fa\u3002<\/p>\n<p>\u6b64\u5916\uff0c\u7576\u8655\u7406\u5927\u578b\u8cc7\u6599\u7d50\u69cb\u6642\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528 `numpy` \u6216 `pandas` \u7b49\u5eab\uff0c\u5b83\u5011\u63d0\u4f9b\u4e86\u66f4\u6709\u6548\u7684\u6578\u64da\u8655\u7406\u65b9\u5f0f\uff0c\u4e26\u4e14\u6709\u52a9\u65bc\u63d0\u9ad8\u6027\u80fd\u3002<\/p>\n<p>\u5728 [\u9019\u88e1](https:\/\/vocus.cc) \u60a8\u53ef\u4ee5\u627e\u5230\u66f4\u591a\u8207 Python \u6709\u95dc\u7684\u6559\u5b78\u8cc7\u6e90\uff0c\u5e6b\u52a9\u60a8\u9032\u4e00\u6b65\u64f4\u5c55\u60a8\u7684\u77e5\u8b58\u3002<\/p>\n<h2>\u7d50\u8ad6<\/h2>\n<p>`sizeof()` \u51fd\u6578\u662f\u4e00\u500b\u975e\u5e38\u65b9\u4fbf\u7684\u5de5\u5177\uff0c\u53ef\u4ee5\u5e6b\u52a9\u958b\u767c\u8005\u5feb\u901f\u7372\u53d6\u7269\u4ef6\u7684\u5927\u5c0f\uff0c\u4f46\u8981\u6ce8\u610f\u5176\u9650\u5236\u3002\u5982\u679c\u60a8\u9700\u8981\u66f4\u8a73\u7d30\u7684\u7269\u4ef6\u8cc7\u8a0a\uff0c\u8acb\u8003\u616e\u4f7f\u7528\u5176\u4ed6\u5de5\u5177\u6216\u6a21\u7d44\u3002<\/p>\n<h2>Q&#038;A\uff08\u5e38\u898b\u554f\u984c\u89e3\u7b54\uff09<\/h2>\n<p>**Q1: sizeof() \u51fd\u6578\u7684\u8fd4\u56de\u503c\u662f\u4ec0\u9ebc\uff1f**<br \/>\nA1: `sizeof()` \u51fd\u6578\u8fd4\u56de\u7684\u662f\u7269\u4ef6\u5728\u5167\u5b58\u4e2d\u4f54\u7528\u7684\u5b57\u7bc0\u6578\u3002<\/p>\n<p>**Q2: sizeof() \u51fd\u6578\u53ef\u4ee5\u7528\u4f86\u8a08\u7b97\u6240\u6709\u8cc7\u6599\u985e\u578b\u7684\u5927\u5c0f\u55ce\uff1f**<br \/>\nA2: \u662f\u7684\uff0c`sizeof()` \u51fd\u6578\u53ef\u4ee5\u7528\u4f86\u8a08\u7b97\u6574\u6578\u3001\u5b57\u4e32\u3001\u5217\u8868\u3001\u5b57\u5178\u7b49\u5404\u7a2e\u8cc7\u6599\u985e\u578b\u7684\u5927\u5c0f\u3002<\/p>\n<p>**Q3: \u5982\u4f55\u7372\u53d6\u66f4\u8a73\u7d30\u7684\u7269\u4ef6\u8cc7\u8a0a\uff1f**<br \/>\nA3: \u9664\u4e86\u4f7f\u7528 `sizeof()` \u51fd\u6578\uff0c\u60a8\u9084\u53ef\u4ee5\u4f7f\u7528 `pprint` \u6a21\u7d44\u4f86\u7f8e\u5316\u8f38\u51fa\uff0c\u6216\u4f7f\u7528\u5176\u4ed6\u5eab\u5982 `numpy` \u548c `pandas` \u4f86\u8655\u7406\u8907\u96dc\u7684\u6578\u64da\u7d50\u69cb\u3002<br \/>\n&#8212;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>sizeof()\u51fd\u6578\u662fPython\u4e2d\u7684\u4e00\u500b\u91cd\u8981\u51fd\u6578\uff0c\u5b83\u53ef\u4ee5\u7528\u4f86\u67e5\u770b\u7269\u4ef6\u7684\u5927\u5c0f\uff0c\u4e26\u63d0\u4f9b\u6709\u95dc\u7269\u4ef6\u7684\u8a73\u7d30\u4fe1\u606f\u3002\u672c\u6587\u5c07\u8a73\u7d30\u4ecb\u7d39Python\u4e2d\u7684sizeof()\u51fd\u6578\uff0c\u4ee5\u53ca\u5b83\u7684\u7528\u6cd5\u548c\u512a\u9ede\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-3098","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-NY","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/3098","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=3098"}],"version-history":[{"count":1,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/3098\/revisions"}],"predecessor-version":[{"id":3099,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/3098\/revisions\/3099"}],"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=3098"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/categories?post=3098"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/tags?post=3098"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}