{"id":998767,"date":"2024-12-27T09:35:50","date_gmt":"2024-12-27T01:35:50","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/998767.html"},"modified":"2024-12-27T09:35:53","modified_gmt":"2024-12-27T01:35:53","slug":"python%e5%86%85%e7%bd%ae%e5%87%bd%e6%95%b0%e5%a6%82%e4%bd%95%e9%87%8d%e8%bd%bd","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/998767.html","title":{"rendered":"python\u5185\u7f6e\u51fd\u6570\u5982\u4f55\u91cd\u8f7d"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25074043\/045d3716-6d65-492d-9782-afd125dd786f.webp\" alt=\"python\u5185\u7f6e\u51fd\u6570\u5982\u4f55\u91cd\u8f7d\" \/><\/p>\n<p><p> \u5728Python\u4e2d\uff0c\u5185\u7f6e\u51fd\u6570\u662f\u7531Python\u89e3\u91ca\u5668\u63d0\u4f9b\u7684\u529f\u80fd\uff0c\u901a\u5e38\u7528\u4e8e\u6267\u884c\u5e38\u89c1\u4efb\u52a1\uff0c\u5982\u6570\u5b66\u8fd0\u7b97\u3001\u7c7b\u578b\u8f6c\u6362\u7b49\u3002<strong>Python\u5185\u7f6e\u51fd\u6570\u4e0d\u80fd\u76f4\u63a5\u91cd\u8f7d\uff0c\u4f46\u53ef\u4ee5\u901a\u8fc7\u5176\u4ed6\u65b9\u6cd5\u5b9e\u73b0\u7c7b\u4f3c\u529f\u80fd\uff0c\u5982\u521b\u5efa\u81ea\u5b9a\u4e49\u51fd\u6570\u3001\u4f7f\u7528\u7c7b\u548c\u9b54\u672f\u65b9\u6cd5\u3001\u6216\u662f\u901a\u8fc7\u6a21\u5757\u548c\u88c5\u9970\u5668\u6269\u5c55\u529f\u80fd<\/strong>\u3002\u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u5c06\u8be6\u7ec6\u63a2\u8ba8\u8fd9\u4e9b\u65b9\u6cd5\u4e2d\u7684\u4e00\u79cd\uff1a\u901a\u8fc7\u521b\u5efa\u81ea\u5b9a\u4e49\u51fd\u6570\u6765\u5b9e\u73b0\u7c7b\u4f3c\u91cd\u8f7d\u7684\u6548\u679c\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u521b\u5efa\u81ea\u5b9a\u4e49\u51fd\u6570<\/h3>\n<\/p>\n<p><p>\u901a\u8fc7\u521b\u5efa\u81ea\u5b9a\u4e49\u51fd\u6570\uff0c\u6211\u4eec\u53ef\u4ee5\u8986\u76d6\u6216\u6269\u5c55\u5185\u7f6e\u51fd\u6570\u7684\u529f\u80fd\u3002\u867d\u7136\u8fd9\u5e76\u4e0d\u662f\u771f\u6b63\u610f\u4e49\u4e0a\u7684\u91cd\u8f7d\uff0c\u4f46\u53ef\u4ee5\u5b9e\u73b0\u7c7b\u4f3c\u7684\u6548\u679c\u3002<\/p>\n<\/p>\n<p><h4>1. \u81ea\u5b9a\u4e49\u51fd\u6570\u6982\u8ff0<\/h4>\n<\/p>\n<p><p>\u81ea\u5b9a\u4e49\u51fd\u6570\u662f\u6307\u7528\u6237\u6839\u636e\u81ea\u5df1\u7684\u9700\u6c42\uff0c\u5b9a\u4e49\u7684\u51fd\u6570\u3002\u4e0e\u5185\u7f6e\u51fd\u6570\u4e0d\u540c\uff0c\u81ea\u5b9a\u4e49\u51fd\u6570\u53ef\u4ee5\u5b9e\u73b0\u7279\u5b9a\u7684\u529f\u80fd\uff0c\u5e76\u80fd\u63a5\u6536\u4e0d\u540c\u7684\u6570\u636e\u7c7b\u578b\u4f5c\u4e3a\u53c2\u6570\u3002<\/p>\n<\/p>\n<p><h4>2. \u793a\u4f8b<\/h4>\n<\/p>\n<p><p>\u5047\u8bbe\u6211\u4eec\u5e0c\u671b\u91cd\u8f7dPython\u7684<code>len()<\/code>\u51fd\u6570\uff0c\u4f7f\u5176\u4e0d\u4ec5\u80fd\u5904\u7406\u5217\u8868\u548c\u5b57\u7b26\u4e32\uff0c\u8fd8\u80fd\u5904\u7406\u81ea\u5b9a\u4e49\u5bf9\u8c61\u3002\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u5b9a\u4e49\u4e00\u4e2a\u65b0\u51fd\u6570\u6765\u5b9e\u73b0\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">class MyCollection:<\/p>\n<p>    def __init__(self, items):<\/p>\n<p>        self.items = items<\/p>\n<p>    def __len__(self):<\/p>\n<p>        return len(self.items)<\/p>\n<p>def custom_len(obj):<\/p>\n<p>    if hasattr(obj, &#39;__len__&#39;):<\/p>\n<p>        return obj.__len__()<\/p>\n<p>    else:<\/p>\n<p>        r<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>se TypeError(f&quot;Object of type &#39;{type(obj).__name__}&#39; has no len()&quot;)<\/p>\n<h2><strong>\u4f7f\u7528\u793a\u4f8b<\/strong><\/h2>\n<p>my_obj = MyCollection([1, 2, 3, 4])<\/p>\n<p>print(custom_len(my_obj))  # \u8f93\u51fa: 4<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e8c\u3001\u4f7f\u7528\u7c7b\u548c\u9b54\u672f\u65b9\u6cd5<\/h3>\n<\/p>\n<p><p>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7\u5b9a\u4e49\u7c7b\u548c\u5b9e\u73b0\u76f8\u5e94\u7684\u9b54\u672f\u65b9\u6cd5\u6765\u91cd\u8f7d\u67d0\u4e9b\u5185\u7f6e\u51fd\u6570\u7684\u884c\u4e3a\u3002<\/p>\n<\/p>\n<p><h4>1. \u9b54\u672f\u65b9\u6cd5\u6982\u8ff0<\/h4>\n<\/p>\n<p><p>\u9b54\u672f\u65b9\u6cd5\u662f\u4ee5\u53cc\u4e0b\u5212\u7ebf\u5f00\u5934\u548c\u7ed3\u5c3e\u7684\u65b9\u6cd5\uff0c\u4f8b\u5982<code>__len__<\/code>\u3001<code>__add__<\/code>\u7b49\u3002\u8fd9\u4e9b\u65b9\u6cd5\u5141\u8bb8\u6211\u4eec\u5b9a\u4e49\u5bf9\u8c61\u5728\u7279\u5b9a\u60c5\u51b5\u4e0b\u7684\u884c\u4e3a\u3002<\/p>\n<\/p>\n<p><h4>2. \u793a\u4f8b<\/h4>\n<\/p>\n<p><p>\u5047\u8bbe\u6211\u4eec\u9700\u8981\u91cd\u8f7d\u52a0\u6cd5\u8fd0\u7b97\u7b26<code>+<\/code>\uff0c\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u5b9a\u4e49<code>__add__<\/code>\u65b9\u6cd5\u6765\u5b9e\u73b0\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">class Vector:<\/p>\n<p>    def __init__(self, x, y):<\/p>\n<p>        self.x = x<\/p>\n<p>        self.y = y<\/p>\n<p>    def __add__(self, other):<\/p>\n<p>        return Vector(self.x + other.x, self.y + other.y)<\/p>\n<p>    def __repr__(self):<\/p>\n<p>        return f&quot;Vector({self.x}, {self.y})&quot;<\/p>\n<h2><strong>\u4f7f\u7528\u793a\u4f8b<\/strong><\/h2>\n<p>v1 = Vector(2, 3)<\/p>\n<p>v2 = Vector(4, 5)<\/p>\n<p>print(v1 + v2)  # \u8f93\u51fa: Vector(6, 8)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e09\u3001\u901a\u8fc7\u6a21\u5757\u548c\u88c5\u9970\u5668\u6269\u5c55\u529f\u80fd<\/h3>\n<\/p>\n<p><p>\u53ef\u4ee5\u901a\u8fc7\u5bfc\u5165\u6a21\u5757\u6216\u4f7f\u7528\u88c5\u9970\u5668\u6765\u6269\u5c55\u5185\u7f6e\u51fd\u6570\u7684\u529f\u80fd\uff0c\u800c\u4e0d\u662f\u76f4\u63a5\u91cd\u8f7d\u5b83\u4eec\u3002<\/p>\n<\/p>\n<p><h4>1. \u6a21\u5757\u6269\u5c55<\/h4>\n<\/p>\n<p><p>Python\u6807\u51c6\u5e93\u548c\u7b2c\u4e09\u65b9\u5e93\u63d0\u4f9b\u4e86\u8bb8\u591a\u6a21\u5757\uff0c\u53ef\u4ee5\u7528\u6765\u6269\u5c55\u5185\u7f6e\u51fd\u6570\u7684\u529f\u80fd\u3002\u4f8b\u5982\uff0c<code>functools<\/code>\u6a21\u5757\u4e2d\u7684<code>partial<\/code>\u51fd\u6570\u53ef\u4ee5\u7528\u4e8e\u9884\u914d\u7f6e\u51fd\u6570\u53c2\u6570\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from functools import partial<\/p>\n<p>def multiply(x, y):<\/p>\n<p>    return x * y<\/p>\n<p>double = partial(multiply, 2)<\/p>\n<p>print(double(5))  # \u8f93\u51fa: 10<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2. \u88c5\u9970\u5668\u6269\u5c55<\/h4>\n<\/p>\n<p><p>\u88c5\u9970\u5668\u662f\u4e00\u79cd\u7528\u4e8e\u4fee\u6539\u51fd\u6570\u6216\u7c7b\u884c\u4e3a\u7684\u8bbe\u8ba1\u6a21\u5f0f\u3002\u901a\u8fc7\u81ea\u5b9a\u4e49\u88c5\u9970\u5668\uff0c\u6211\u4eec\u53ef\u4ee5\u5728\u4e0d\u4fee\u6539\u539f\u51fd\u6570\u4ee3\u7801\u7684\u60c5\u51b5\u4e0b\uff0c\u6dfb\u52a0\u65b0\u7684\u529f\u80fd\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def debug(func):<\/p>\n<p>    def wrapper(*args, kwargs):<\/p>\n<p>        result = func(*args, kwargs)<\/p>\n<p>        print(f&quot;Calling {func.__name__} with {args}, {kwargs} returned {result}&quot;)<\/p>\n<p>        return result<\/p>\n<p>    return wrapper<\/p>\n<p>@debug<\/p>\n<p>def add(x, y):<\/p>\n<p>    return x + y<\/p>\n<h2><strong>\u4f7f\u7528\u793a\u4f8b<\/strong><\/h2>\n<p>add(3, 4)  # \u8f93\u51fa: Calling add with (3, 4), {} returned 7<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u56db\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u867d\u7136Python\u5185\u7f6e\u51fd\u6570\u4e0d\u80fd\u76f4\u63a5\u91cd\u8f7d\uff0c\u4f46\u901a\u8fc7<strong>\u521b\u5efa\u81ea\u5b9a\u4e49\u51fd\u6570\u3001\u4f7f\u7528\u7c7b\u548c\u9b54\u672f\u65b9\u6cd5\u3001\u4ee5\u53ca\u901a\u8fc7\u6a21\u5757\u548c\u88c5\u9970\u5668\u6269\u5c55\u529f\u80fd<\/strong>\uff0c\u6211\u4eec\u53ef\u4ee5\u5b9e\u73b0\u7c7b\u4f3c\u91cd\u8f7d\u7684\u6548\u679c\u3002\u8fd9\u4e0d\u4ec5\u589e\u5f3a\u4e86\u4ee3\u7801\u7684\u7075\u6d3b\u6027\u548c\u53ef\u8bfb\u6027\uff0c\u8fd8\u80fd\u66f4\u597d\u5730\u6ee1\u8db3\u7279\u5b9a\u7684\u9700\u6c42\u3002\u5728\u4f7f\u7528\u8fd9\u4e9b\u6280\u672f\u65f6\uff0c\u52a1\u5fc5\u6ce8\u610f\u4ee3\u7801\u7684\u53ef\u7ef4\u62a4\u6027\u548c\u53ef\u8bfb\u6027\uff0c\u907f\u514d\u8fc7\u5ea6\u590d\u6742\u5316\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>1. \u4ec0\u4e48\u662fPython\u5185\u7f6e\u51fd\u6570\u91cd\u8f7d\uff1f<\/strong><br \/>Python\u5185\u7f6e\u51fd\u6570\u91cd\u8f7d\u662f\u6307\u5728\u81ea\u5b9a\u4e49\u51fd\u6570\u4e2d\u8986\u76d6\u6216\u6269\u5c55Python\u5185\u7f6e\u51fd\u6570\u7684\u529f\u80fd\uff0c\u4ee5\u4fbf\u6839\u636e\u7279\u5b9a\u7684\u9700\u6c42\u63d0\u4f9b\u4e0d\u540c\u7684\u5b9e\u73b0\u65b9\u5f0f\u3002\u901a\u8fc7\u91cd\u8f7d\uff0c\u53ef\u4ee5\u5728\u4e0d\u540c\u7684\u4e0a\u4e0b\u6587\u4e2d\u4f7f\u7528\u76f8\u540c\u7684\u51fd\u6570\u540d\uff0c\u63d0\u5347\u4ee3\u7801\u7684\u7075\u6d3b\u6027\u548c\u53ef\u8bfb\u6027\u3002<\/p>\n<p><strong>2. \u5982\u4f55\u5728Python\u4e2d\u5b9e\u73b0\u5185\u7f6e\u51fd\u6570\u7684\u91cd\u8f7d\uff1f<\/strong><br \/>\u5b9e\u73b0Python\u5185\u7f6e\u51fd\u6570\u7684\u91cd\u8f7d\u901a\u5e38\u9700\u8981\u5b9a\u4e49\u4e00\u4e2a\u65b0\u7684\u51fd\u6570\uff0c\u5e76\u786e\u4fdd\u65b0\u7684\u5b9e\u73b0\u4e0e\u5185\u7f6e\u51fd\u6570\u7684\u53c2\u6570\u548c\u8fd4\u56de\u503c\u7c7b\u578b\u76f8\u5339\u914d\u3002\u53ef\u4ee5\u4f7f\u7528\u6761\u4ef6\u8bed\u53e5\u6216\u591a\u6001\u6765\u5904\u7406\u4e0d\u540c\u7c7b\u578b\u7684\u8f93\u5165\uff0c\u4ece\u800c\u5b9e\u73b0\u51fd\u6570\u7684\u591a\u79cd\u884c\u4e3a\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u521b\u5efa\u4e00\u4e2a\u81ea\u5b9a\u4e49\u7684<code>len()<\/code>\u51fd\u6570\uff0c\u6765\u8ba1\u7b97\u4e0d\u540c\u6570\u636e\u7ed3\u6784\u7684\u957f\u5ea6\u3002<\/p>\n<p><strong>3. \u91cd\u8f7d\u5185\u7f6e\u51fd\u6570\u662f\u5426\u4f1a\u5f71\u54cd\u4ee3\u7801\u7684\u53ef\u7ef4\u62a4\u6027\uff1f<\/strong><br \/>\u91cd\u8f7d\u5185\u7f6e\u51fd\u6570\u53ef\u80fd\u4f1a\u5bfc\u81f4\u4ee3\u7801\u7684\u53ef\u7ef4\u62a4\u6027\u964d\u4f4e\uff0c\u7279\u522b\u662f\u5728\u56e2\u961f\u5408\u4f5c\u4e2d\uff0c\u56e0\u4e3a\u5176\u4ed6\u5f00\u53d1\u8005\u53ef\u80fd\u4f1a\u5bf9\u91cd\u8f7d\u7684\u51fd\u6570\u4ea7\u751f\u56f0\u60d1\u3002\u56e0\u6b64\uff0c\u5728\u91cd\u8f7d\u5185\u7f6e\u51fd\u6570\u65f6\uff0c\u5efa\u8bae\u63d0\u4f9b\u6e05\u6670\u7684\u6587\u6863\u548c\u793a\u4f8b\uff0c\u786e\u4fdd\u4ee3\u7801\u7684\u53ef\u8bfb\u6027\u548c\u53ef\u7406\u89e3\u6027\uff0c\u4ee5\u4fbf\u5176\u4ed6\u5f00\u53d1\u8005\u80fd\u591f\u5feb\u901f\u638c\u63e1\u5176\u7528\u6cd5\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\uff0c\u5185\u7f6e\u51fd\u6570\u662f\u7531Python\u89e3\u91ca\u5668\u63d0\u4f9b\u7684\u529f\u80fd\uff0c\u901a\u5e38\u7528\u4e8e\u6267\u884c\u5e38\u89c1\u4efb\u52a1\uff0c\u5982\u6570\u5b66\u8fd0\u7b97\u3001\u7c7b\u578b\u8f6c\u6362\u7b49\u3002Py [&hellip;]","protected":false},"author":3,"featured_media":998777,"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\/998767"}],"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=998767"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/998767\/revisions"}],"predecessor-version":[{"id":998780,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/998767\/revisions\/998780"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/998777"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=998767"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=998767"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=998767"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}