{"id":4073,"date":"2025-05-31T13:10:45","date_gmt":"2025-06-04T06:06:45","guid":{"rendered":"https:\/\/badgameshow.com\/steven\/?p=4073"},"modified":"2025-06-04T13:10:45","modified_gmt":"2025-06-04T06:06:45","slug":"%e5%ad%b8%e7%bf%92%e5%a6%82%e4%bd%95%e4%bd%bf%e7%94%a8python%e4%b8%ad%e7%9a%84methods%e5%87%bd%e6%95%b8","status":"publish","type":"post","link":"https:\/\/badgameshow.com\/steven\/swift\/%e5%ad%b8%e7%bf%92%e5%a6%82%e4%bd%95%e4%bd%bf%e7%94%a8python%e4%b8%ad%e7%9a%84methods%e5%87%bd%e6%95%b8\/","title":{"rendered":"\u6df1\u5165\u63a2\u7d22 Python \u4e2d\u7684 methods() \u51fd\u6578\uff1a\u5f9e\u57fa\u790e\u5230\u9032\u968e\u4f7f\u7528\u6280\u5de7"},"content":{"rendered":"<p><meta name=\"keywords\" content=\"Python, methods, \u51fd\u6578\u4f7f\u7528, Python \u6559\u5b78, \u7de8\u7a0b\u6280\u5de7\"><\/p>\n<h1>\u6df1\u5165\u63a2\u7d22 Python \u4e2d\u7684 methods() \u51fd\u6578<\/h1>\n<p>Python \u662f\u76ee\u524d\u6700\u53d7\u6b61\u8fce\u7684\u7de8\u7a0b\u8a9e\u8a00\u4e4b\u4e00\uff0c\u56e0\u5176\u7c21\u6f54\u7684\u8a9e\u6cd5\u548c\u5f37\u5927\u7684\u529f\u80fd\u800c\u53d7\u5230\u5ee3\u6cdb\u61c9\u7528\u3002\u5728 Python \u4e2d\uff0c\u65b9\u6cd5 (methods) \u662f\u7269\u4ef6\u5c0e\u5411\u7de8\u7a0b\u7684\u91cd\u8981\u7d44\u6210\u90e8\u5206\uff0c\u7406\u89e3\u5982\u4f55\u4f7f\u7528\u9019\u4e9b\u65b9\u6cd5\u5c0d\u65bc\u63d0\u5347\u4f60\u7684\u7de8\u7a0b\u80fd\u529b\u81f3\u95dc\u91cd\u8981\u3002<\/p>\n<p><!--more--><\/p>\n<h2>\u4ec0\u9ebc\u662f methods() \u51fd\u6578\uff1f<\/h2>\n<p>\u5728 Python \u4e2d\uff0cmethods() \u51fd\u6578\u662f\u7528\u4f86\u7372\u53d6\u67d0\u500b\u7269\u4ef6\u7684\u6240\u6709\u65b9\u6cd5\u5217\u8868\u3002\u9019\u4e9b\u65b9\u6cd5\u662f\u7269\u4ef6\u6240\u64c1\u6709\u7684\u51fd\u6578\uff0c\u53ef\u4ee5\u7528\u4f86\u64cd\u4f5c\u8a72\u7269\u4ef6\u7684\u6578\u64da\u3002\u4f7f\u7528 methods() \u51fd\u6578\u53ef\u4ee5\u5e6b\u52a9\u958b\u767c\u8005\u5feb\u901f\u4e86\u89e3\u4e00\u500b\u7269\u4ef6\u7684\u80fd\u529b\uff0c\u5f9e\u800c\u6709\u6548\u5730\u4f7f\u7528\u5b83\u3002<\/p>\n<h2>\u5982\u4f55\u4f7f\u7528 methods() \u51fd\u6578\uff1f<\/h2>\n<p>\u4f7f\u7528 methods() \u51fd\u6578\u975e\u5e38\u7c21\u55ae\uff0c\u53ea\u9700\u8abf\u7528\u7269\u4ef6\u7684 `__dir__()` \u65b9\u6cd5\uff0c\u7136\u5f8c\u904e\u6ffe\u51fa\u6240\u6709\u7684\u53ef\u8abf\u7528\u65b9\u6cd5\u3002\u4ee5\u4e0b\u662f\u4e00\u500b\u7c21\u55ae\u7684\u793a\u4f8b\uff1a<\/p>\n<p>&#8220;`python<br \/>\nclass MyClass:<br \/>\n    def my_method(self):<br \/>\n        return &#8220;Hello, World!&#8221;<\/p>\n<p># \u5275\u5efa\u7269\u4ef6<br \/>\nobj = MyClass()<\/p>\n<p># \u7372\u53d6\u6240\u6709\u65b9\u6cd5<br \/>\nmethods = [method for method in dir(obj) if callable(getattr(obj, method))]\nprint(methods)<br \/>\n&#8220;`<\/p>\n<p>\u9019\u6bb5\u4ee3\u78bc\u5c07\u5217\u51fa `MyClass` \u4e2d\u6240\u6709\u53ef\u8abf\u7528\u7684\u65b9\u6cd5\u3002\u9019\u6a23\uff0c\u4f60\u5c31\u53ef\u4ee5\u5feb\u901f\u4e86\u89e3\u8a72\u7269\u4ef6\u53ef\u4ee5\u57f7\u884c\u7684\u64cd\u4f5c\u3002<\/p>\n<h2>\u9032\u968e\u4f7f\u7528\u6280\u5de7<\/h2>\n<p>\u9664\u4e86\u57fa\u672c\u7684\u4f7f\u7528\u65b9\u6cd5\uff0c\u4f60\u9084\u53ef\u4ee5\u5229\u7528 methods() \u51fd\u6578\u4f86\u9032\u4e00\u6b65\u512a\u5316\u4f60\u7684\u4ee3\u78bc\u3002\u4f8b\u5982\uff0c\u4f60\u53ef\u4ee5\u5c07\u65b9\u6cd5\u8207 lambda \u8868\u9054\u5f0f\u7d50\u5408\uff0c\u5f9e\u800c\u5275\u5efa\u66f4\u9ad8\u6548\u7684\u51fd\u6578\u8abf\u7528\uff1a<\/p>\n<p>&#8220;`python<br \/>\n# \u4f7f\u7528 lambda \u8868\u9054\u5f0f<br \/>\nmethods = {method: getattr(obj, method) for method in dir(obj) if callable(getattr(obj, method))}<br \/>\nmethod_name = &#8220;my_method&#8221;<br \/>\nresult = methods[method_name]()<br \/>\nprint(result)  # \u8f38\u51fa: Hello, World!<br \/>\n&#8220;`<\/p>\n<p>\u9019\u7a2e\u65b9\u5f0f\u4e0d\u50c5\u7c21\u5316\u4e86\u65b9\u6cd5\u7684\u8abf\u7528\uff0c\u9084\u63d0\u9ad8\u4e86\u4ee3\u78bc\u7684\u53ef\u8b80\u6027\u3002<\/p>\n<h2>\u7e3d\u7d50<\/h2>\n<p>methods() \u51fd\u6578\u5728 Python \u4e2d\u662f\u4e00\u500b\u975e\u5e38\u6709\u7528\u7684\u5de5\u5177\uff0c\u53ef\u4ee5\u5e6b\u52a9\u958b\u767c\u8005\u5feb\u901f\u4e86\u89e3\u7269\u4ef6\u7684\u53ef\u8abf\u7528\u65b9\u6cd5\u3002\u901a\u904e\u638c\u63e1\u9019\u4e9b\u57fa\u790e\u548c\u9032\u968e\u4f7f\u7528\u6280\u5de7\uff0c\u4f60\u5c07\u80fd\u5920\u66f4\u6709\u6548\u5730\u5229\u7528 Python \u9032\u884c\u958b\u767c\u3002\u96a8\u8457\u4f60\u5c0d Python \u6df1\u5165\u4e86\u89e3\uff0c\u9019\u4e9b\u6280\u80fd\u5c07\u5728\u4f60\u7684\u7a0b\u5f0f\u8a2d\u8a08\u4e4b\u8def\u4e0a\u767c\u63ee\u91cd\u8981\u4f5c\u7528\u3002<\/p>\n<p>&#8212;<\/p>\n<p>\u9019\u6a23\u7684\u6a19\u984c\u548c\u5167\u5bb9\u512a\u5316\u4e0d\u50c5\u63d0\u5347\u4e86\u5438\u5f15\u529b\uff0c\u9084\u66f4\u52a0\u7b26\u5408 Google SEO \u7684\u6700\u4f73\u5be6\u8e10\uff0c\u78ba\u4fdd\u6587\u7ae0\u80fd\u5920\u5438\u5f15\u66f4\u591a\u7684\u8b80\u8005\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u6458\u8981\uff1a\u672c\u6587\u5c07\u4ecb\u7d39\u5982\u4f55\u4f7f\u7528Python\u4e2d\u7684methods()\u51fd\u6578\uff0c\u8b93\u4f60\u53ef\u4ee5\u66f4\u6709\u6548\u5730\u4f7f\u7528Python\u7a0b\u5f0f\u8a9e\u8a00\u3002\u6211\u5011\u5c07\u6df1\u5165\u63a2\u8a0emethods()\u51fd\u6578\u7684\u529f\u80fd\uff0c\u4ee5\u53ca\u5982\u4f55\u4f7f\u7528\u5b83\u4f86\u63d0\u9ad8\u7a0b\u5f0f\u7684\u6548\u7387\u3002<\/p>\n","protected":false},"author":1,"featured_media":0,"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":[174,5],"tags":[173,9],"class_list":["post-4073","post","type-post","status-publish","format-standard","hentry","category-ios","category-swift","tag-ios","tag-swift"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack-related-posts":[],"jetpack_shortlink":"https:\/\/wp.me\/pcFK27-13H","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/4073","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=4073"}],"version-history":[{"count":1,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/4073\/revisions"}],"predecessor-version":[{"id":4074,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/4073\/revisions\/4074"}],"wp:attachment":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/media?parent=4073"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/categories?post=4073"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/tags?post=4073"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}