{"id":3370,"date":"2025-06-01T15:21:16","date_gmt":"2025-06-04T08:05:16","guid":{"rendered":"https:\/\/badgameshow.com\/steven\/?p=3370"},"modified":"2025-06-04T15:21:16","modified_gmt":"2025-06-04T08:05:16","slug":"python%e4%b8%ad%e7%9a%84methods%e5%87%bd%e6%95%b8%e4%bb%8b%e7%b4%b9-2","status":"publish","type":"post","link":"https:\/\/badgameshow.com\/steven\/python\/python%e4%b8%ad%e7%9a%84methods%e5%87%bd%e6%95%b8%e4%bb%8b%e7%b4%b9-2\/","title":{"rendered":"2025 \u6700\u65b0\u7248 Python \u4e2d\u7684 `methods()` \u51fd\u5f0f\u8a73\u7d30\u4ecb\u7d39"},"content":{"rendered":"<p><meta name=\"keywords\" content=\"Python, methods(), Python \u6559\u5b78, Python \u51fd\u5f0f\"><\/p>\n<h1>2025 \u6700\u65b0\u7248 Python \u4e2d\u7684 `methods()` \u51fd\u5f0f\u8a73\u7d30\u4ecb\u7d39<\/h1>\n<p>Python \u662f\u4e00\u7a2e\u975e\u5e38\u6d41\u884c\u4e14\u5f37\u5927\u7684\u7a0b\u5f0f\u8a9e\u8a00\uff0c\u5b83\u63d0\u4f9b\u4e86\u8a31\u591a\u6709\u7528\u7684\u51fd\u5f0f\u548c\u65b9\u6cd5\uff0c\u5176\u4e2d\u4e4b\u4e00\u5c31\u662f `methods()` \u51fd\u5f0f\u3002\u9019\u500b\u51fd\u5f0f\u53ef\u4ee5\u5e6b\u52a9\u958b\u767c\u8005\u5feb\u901f\u67e5\u770b\u7269\u4ef6\u7684\u53ef\u7528\u65b9\u6cd5\u53ca\u5176\u5c6c\u6027\uff0c\u5f9e\u800c\u63d0\u9ad8\u958b\u767c\u6548\u7387\u3002<\/p>\n<h2>\u4ec0\u9ebc\u662f `methods()` \u51fd\u5f0f\uff1f<\/h2>\n<p>`methods()` \u51fd\u5f0f\u4e26\u4e0d\u662f Python \u7684\u5167\u5efa\u51fd\u5f0f\uff0c\u800c\u662f\u901a\u904e\u4f7f\u7528 `dir()` \u51fd\u5f0f\u4f86\u5be6\u73fe\u7684\u3002\u900f\u904e\u9019\u500b\u51fd\u5f0f\uff0c\u6211\u5011\u53ef\u4ee5\u7372\u5f97\u4e00\u500b\u7269\u4ef6\u7684\u6240\u6709\u65b9\u6cd5\u548c\u5c6c\u6027\uff0c\u9019\u5c0d\u65bc\u958b\u767c\u8005\u5728\u9032\u884c\u8abf\u8a66\u6216\u5b78\u7fd2\u65b0\u7269\u4ef6\u6642\u975e\u5e38\u6709\u5e6b\u52a9\u3002<\/p>\n<h2>\u5982\u4f55\u4f7f\u7528 `methods()` \u51fd\u5f0f<\/h2>\n<p>\u4e0b\u9762\u662f\u5982\u4f55\u4f7f\u7528 `dir()` \u51fd\u5f0f\u4f86\u6a21\u64ec `methods()` \u51fd\u5f0f\u7684\u884c\u70ba\u7684\u793a\u7bc4\uff1a<\/p>\n<p>&#8220;`python<br \/>\n# \u67e5\u770b\u5b57\u4e32\u7269\u4ef6\u7684\u65b9\u6cd5<br \/>\nstr_obj = &#8220;Hello World&#8221;<br \/>\nmethods = [method for method in dir(str_obj) if callable(getattr(str_obj, method))]\nprint(methods)<br \/>\n&#8220;`<\/p>\n<p>\u57f7\u884c\u4ee5\u4e0a\u7a0b\u5f0f\u78bc\u5f8c\uff0c\u5c07\u5f97\u5230\u4e00\u500b\u5217\u8868\uff0c\u8a72\u5217\u8868\u4e2d\u5305\u542b\u4e86\u5b57\u4e32\u7269\u4ef6\u7684\u6240\u6709\u53ef\u8abf\u7528\u65b9\u6cd5\uff0c\u4f8b\u5982\uff1a<\/p>\n<p>&#8220;`python<br \/>\n[&#8216;capitalize&#8217;, &#8216;casefold&#8217;, &#8216;center&#8217;, &#8216;count&#8217;, &#8216;encode&#8217;, &#8216;endswith&#8217;, &#8216;expandtabs&#8217;, &#8216;find&#8217;, &#8216;format&#8217;,<br \/>\n &#8216;format_map&#8217;, &#8216;index&#8217;, &#8216;isalnum&#8217;, &#8216;isalpha&#8217;, &#8216;isdecimal&#8217;, &#8216;isdigit&#8217;, &#8216;isidentifier&#8217;, &#8216;islower&#8217;,<br \/>\n &#8216;isnumeric&#8217;, &#8216;isprintable&#8217;, &#8216;isspace&#8217;, &#8216;istitle&#8217;, &#8216;isupper&#8217;, &#8216;join&#8217;, &#8216;ljust&#8217;, &#8216;lower&#8217;, &#8216;lstrip&#8217;,<br \/>\n &#8216;maketrans&#8217;, &#8216;partition&#8217;, &#8216;replace&#8217;, &#8216;rfind&#8217;, &#8216;rindex&#8217;, &#8216;rjust&#8217;, &#8216;rpartition&#8217;, &#8216;rsplit&#8217;, &#8216;rstrip&#8217;,<br \/>\n &#8216;split&#8217;, &#8216;splitlines&#8217;, &#8216;startswith&#8217;, &#8216;strip&#8217;, &#8216;swapcase&#8217;, &#8216;title&#8217;, &#8216;translate&#8217;, &#8216;upper&#8217;, &#8216;zfill&#8217;]\n&#8220;`<\/p>\n<h2>\u67e5\u770b\u7269\u4ef6\u5c6c\u6027<\/h2>\n<p>`methods()` \u51fd\u5f0f\u7684\u53e6\u4e00\u500b\u7528\u9014\u662f\u67e5\u770b\u7269\u4ef6\u7684\u5c6c\u6027\u3002\u4ee5\u4e0b\u662f\u5982\u4f55\u67e5\u770b\u5b57\u5178\u7269\u4ef6\u7684\u5c6c\u6027\uff1a<\/p>\n<p>&#8220;`python<br \/>\n# \u67e5\u770b\u5b57\u5178\u7269\u4ef6\u7684\u5c6c\u6027<br \/>\ndict_obj = {&#8216;name&#8217;: &#8216;John&#8217;, &#8216;age&#8217;: 20}<br \/>\nattributes = [attr for attr in dir(dict_obj) if not callable(getattr(dict_obj, attr))]\nprint(attributes)<br \/>\n&#8220;`<\/p>\n<p>\u57f7\u884c\u4ee5\u4e0a\u7a0b\u5f0f\u78bc\u5f8c\uff0c\u5c07\u5f97\u5230\u5b57\u5178\u7269\u4ef6\u7684\u6240\u6709\u5c6c\u6027\uff0c\u4f8b\u5982\uff1a<\/p>\n<p>&#8220;`python<br \/>\n[&#8216;__class__&#8217;, &#8216;__delattr__&#8217;, &#8216;__dir__&#8217;, &#8216;__doc__&#8217;, &#8216;__eq__&#8217;, &#8216;__format__&#8217;, &#8216;__ge__&#8217;, &#8216;__getattribute__&#8217;,<br \/>\n &#8216;__getitem__&#8217;, &#8216;__gt__&#8217;, &#8216;__init__&#8217;, &#8216;__init_subclass__&#8217;, &#8216;__iter__&#8217;, &#8216;__le__&#8217;, &#8216;__lt__&#8217;, &#8216;__ne__&#8217;,<br \/>\n &#8216;__new__&#8217;, &#8216;__reduce__&#8217;, &#8216;__reduce_ex__&#8217;, &#8216;__repr__&#8217;, &#8216;__setattr__&#8217;, &#8216;__setitem__&#8217;, &#8216;__sizeof__&#8217;,<br \/>\n &#8216;__str__&#8217;, &#8216;__subclasshook__&#8217;, &#8216;clear&#8217;, &#8216;copy&#8217;, &#8216;fromkeys&#8217;, &#8216;get&#8217;, &#8216;items&#8217;, &#8216;keys&#8217;, &#8216;pop&#8217;,<br \/>\n &#8216;popitem&#8217;, &#8216;setdefault&#8217;, &#8216;update&#8217;, &#8216;values&#8217;]\n&#8220;`<\/p>\n<h2>\u7e3d\u7d50<\/h2>\n<p>`methods()` \u51fd\u5f0f\u662f Python \u4e2d\u4e00\u500b\u975e\u5e38\u6709\u7528\u7684\u5de5\u5177\uff0c\u5b83\u80fd\u5e6b\u52a9\u958b\u767c\u8005\u5feb\u901f\u4e86\u89e3\u7269\u4ef6\u7684\u53ef\u7528\u65b9\u6cd5\u8207\u5c6c\u6027\uff0c\u5f9e\u800c\u63d0\u5347\u958b\u767c\u6548\u7387\u3002\u7121\u8ad6\u662f\u65b0\u624b\u9084\u662f\u7d93\u9a57\u8c50\u5bcc\u7684\u958b\u767c\u8005\uff0c\u90fd\u80fd\u5f9e\u4e2d\u53d7\u76ca\u3002<\/p>\n<p>\u5982\u679c\u4f60\u60f3\u6df1\u5165\u5b78\u7fd2 Python \u7684\u5176\u4ed6\u7279\u6027\u6216\u6280\u5de7\uff0c\u53ef\u4ee5\u53c3\u8003 [\u9019\u7bc7\u6587\u7ae0](https:\/\/vocus.cc\/article\/5f8d0e8ff60b5c001c1c6c88)\uff0c\u9032\u4e00\u6b65\u63d0\u5347\u4f60\u7684 Python \u6280\u80fd\u3002<\/p>\n<h2>\u5e38\u898b\u554f\u984c\u89e3\u7b54 (Q&#038;A)<\/h2>\n<p>**Q1: `methods()` \u51fd\u5f0f\u548c `dir()` \u51fd\u5f0f\u6709\u4ec0\u9ebc\u5340\u5225\uff1f**<br \/>\nA1: `methods()` \u51fd\u5f0f\u4e26\u4e0d\u662f Python \u7684\u5167\u5efa\u51fd\u5f0f\uff0c\u800c\u6211\u5011\u901a\u5e38\u4f7f\u7528 `dir()` \u51fd\u5f0f\u4f86\u7372\u5f97\u7269\u4ef6\u7684\u6240\u6709\u65b9\u6cd5\u548c\u5c6c\u6027\u3002`dir()` \u51fd\u5f0f\u6703\u8fd4\u56de\u7269\u4ef6\u7684\u5168\u90e8\u5c6c\u6027\u548c\u65b9\u6cd5\uff0c\u800c\u6211\u5011\u53ef\u4ee5\u901a\u904e\u904e\u6ffe\u4f86\u7372\u5f97\u53ef\u8abf\u7528\u7684\u65b9\u6cd5\u3002<\/p>\n<p>**Q2: \u5982\u4f55\u67e5\u770b\u81ea\u5b9a\u7fa9\u985e\u5225\u7684\u65b9\u6cd5\u8207\u5c6c\u6027\uff1f**<br \/>\nA2: \u4f60\u53ef\u4ee5\u4f7f\u7528 `dir()` \u51fd\u5f0f\u67e5\u770b\u81ea\u5b9a\u7fa9\u985e\u5225\u7684\u5be6\u4f8b\u65b9\u6cd5\u53ca\u5c6c\u6027\uff0c\u65b9\u6cd5\u8207\u67e5\u770b\u5167\u5efa\u985e\u5225\u7684\u65b9\u5f0f\u76f8\u540c\u3002\u53ea\u9700\u5c07\u985e\u5225\u7684\u5be6\u4f8b\u4f5c\u70ba\u53c3\u6578\u50b3\u5165 `dir()` \u51fd\u5f0f\u5373\u53ef\u3002<\/p>\n<p>**Q3: \u6709\u54ea\u4e9b\u65b9\u6cd5\u53ef\u4ee5\u52a0\u901f Python \u958b\u767c\u6548\u7387\uff1f**<br \/>\nA3: \u9664\u4e86\u4f7f\u7528 `methods()` \u51fd\u5f0f\uff0c\u9084\u53ef\u4ee5\u5229\u7528 Python \u7684 IDE \u63d2\u4ef6\u3001\u8abf\u8a66\u5de5\u5177\u3001\u6587\u6a94\u751f\u6210\u5de5\u5177\u7b49\u4f86\u63d0\u5347\u958b\u767c\u6548\u7387\uff0c\u4e26\u4e14\u591a\u52a0\u7df4\u7fd2\u8207\u53c3\u8003\u793e\u7fa4\u8cc7\u6e90\u4e5f\u662f\u975e\u5e38\u91cd\u8981\u7684\u3002<\/p>\n<p>&#8212;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Methods()\u51fd\u6578\u662fPython\u4e2d\u7684\u4e00\u7a2e\u5e38\u7528\u51fd\u6578\uff0c\u53ef\u4ee5\u7528\u4f86\u67e5\u770b\u7269\u4ef6\u7684\u65b9\u6cd5\uff0c\u4ee5\u53ca\u67e5\u770b\u7269\u4ef6\u7684\u5c6c\u6027\u3002\u672c\u6587\u5c07\u4ecb\u7d39Methods()\u51fd\u6578\u7684\u4f7f\u7528\u65b9\u6cd5\uff0c\u4ee5\u53ca\u5982\u4f55\u4f7f\u7528\u5b83\u4f86\u67e5\u770b\u7269\u4ef6\u7684\u5c6c\u6027\u548c\u65b9\u6cd5\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-3370","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-Sm","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/3370","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=3370"}],"version-history":[{"count":1,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/3370\/revisions"}],"predecessor-version":[{"id":3371,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/3370\/revisions\/3371"}],"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=3370"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/categories?post=3370"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/tags?post=3370"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}