{"id":1095469,"date":"2025-01-08T14:51:25","date_gmt":"2025-01-08T06:51:25","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1095469.html"},"modified":"2025-01-08T14:51:27","modified_gmt":"2025-01-08T06:51:27","slug":"python%e7%b1%bb%e4%b8%ad%e5%a6%82%e4%bd%95%e8%b0%83%e7%94%a8%e5%a4%96%e9%83%a8%e5%87%bd%e6%95%b0%e8%b0%83%e7%94%a8-2","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1095469.html","title":{"rendered":"python\u7c7b\u4e2d\u5982\u4f55\u8c03\u7528\u5916\u90e8\u51fd\u6570\u8c03\u7528"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/24211102\/daddcb84-0e3c-446e-9a1a-6900ee73826d.webp\" alt=\"python\u7c7b\u4e2d\u5982\u4f55\u8c03\u7528\u5916\u90e8\u51fd\u6570\u8c03\u7528\" \/><\/p>\n<p><p> <strong>\u5728Python\u7c7b\u4e2d\u8c03\u7528\u5916\u90e8\u51fd\u6570\u7684\u65b9\u6cd5\u6709\u5f88\u591a\u79cd\uff0c\u5305\u62ec\u76f4\u63a5\u8c03\u7528\u3001\u901a\u8fc7\u5bf9\u8c61\u8c03\u7528\u3001\u4f7f\u7528\u88c5\u9970\u5668\u7b49\u3002\u5173\u952e\u70b9\u5305\u62ec\u76f4\u63a5\u8c03\u7528\u3001\u5b9e\u4f8b\u5316\u5bf9\u8c61\u3001\u5bfc\u5165\u6a21\u5757\u3002<\/strong> \u4e0b\u9762\u6211\u4eec\u5c06\u8be6\u7ec6\u5c55\u5f00\u5176\u4e2d\u4e00\u79cd\u65b9\u6cd5\uff1a<\/p>\n<\/p>\n<p><p>\u76f4\u63a5\u8c03\u7528\uff1a\u5728Python\u7c7b\u4e2d\uff0c\u53ef\u4ee5\u76f4\u63a5\u901a\u8fc7\u51fd\u6570\u540d\u6765\u8c03\u7528\u8be5\u51fd\u6570\uff0c\u4f46\u524d\u63d0\u662f\u51fd\u6570\u5728\u7c7b\u7684\u4f5c\u7528\u57df\u8303\u56f4\u5185\u3002\u8fd9\u6837\u505a\u7684\u597d\u5904\u662f\u7b80\u4fbf\u5feb\u6377\uff0c\u9002\u5408\u51fd\u6570\u903b\u8f91\u7b80\u5355\u4e14\u4e0e\u7c7b\u6709\u7d27\u5bc6\u5173\u8054\u7684\u60c5\u51b5\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u5b9a\u4e49\u4e00\u4e2a\u5916\u90e8\u51fd\u6570<\/p>\n<p>def external_function():<\/p>\n<p>    print(&quot;This is an external function&quot;)<\/p>\n<h2><strong>\u5b9a\u4e49\u4e00\u4e2a\u7c7b<\/strong><\/h2>\n<p>class MyClass:<\/p>\n<p>    def call_external(self):<\/p>\n<p>        # \u76f4\u63a5\u8c03\u7528\u5916\u90e8\u51fd\u6570<\/p>\n<p>        external_function()<\/p>\n<h2><strong>\u521b\u5efa\u7c7b\u7684\u5b9e\u4f8b<\/strong><\/h2>\n<p>obj = MyClass()<\/p>\n<h2><strong>\u8c03\u7528\u65b9\u6cd5<\/strong><\/h2>\n<p>obj.call_external()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e00\u3001\u76f4\u63a5\u8c03\u7528\u5916\u90e8\u51fd\u6570<\/h3>\n<\/p>\n<p><p>\u5f53\u5916\u90e8\u51fd\u6570\u548c\u7c7b\u5728\u540c\u4e00\u4e2a\u6a21\u5757\u4e2d\u5b9a\u4e49\u65f6\uff0c\u6700\u7b80\u5355\u7684\u65b9\u6cd5\u5c31\u662f\u76f4\u63a5\u8c03\u7528\u8be5\u51fd\u6570\u3002\u8fd9\u79cd\u65b9\u6cd5\u5bf9\u4e8e\u7b80\u5355\u7684\u9879\u76ee\u6216\u5c0f\u578b\u811a\u672c\u975e\u5e38\u6709\u6548\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u5b9a\u4e49\u5916\u90e8\u51fd\u6570<\/p>\n<p>def greet(name):<\/p>\n<p>    return f&quot;Hello, {name}!&quot;<\/p>\n<h2><strong>\u5b9a\u4e49\u7c7b<\/strong><\/h2>\n<p>class Greeter:<\/p>\n<p>    def __init__(self, name):<\/p>\n<p>        self.name = name<\/p>\n<p>    def greet_person(self):<\/p>\n<p>        # \u76f4\u63a5\u8c03\u7528\u5916\u90e8\u51fd\u6570<\/p>\n<p>        greeting = greet(self.name)<\/p>\n<p>        print(greeting)<\/p>\n<h2><strong>\u521b\u5efa\u7c7b\u7684\u5b9e\u4f8b<\/strong><\/h2>\n<p>person = Greeter(&quot;John&quot;)<\/p>\n<h2><strong>\u8c03\u7528\u7c7b\u65b9\u6cd5<\/strong><\/h2>\n<p>person.greet_person()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u5916\u90e8\u51fd\u6570<code>greet<\/code>\u88ab\u7c7b<code>Greeter<\/code>\u4e2d\u7684\u65b9\u6cd5<code>greet_person<\/code>\u76f4\u63a5\u8c03\u7528\uff0c\u51fd\u6570<code>greet<\/code>\u63a5\u6536\u4e00\u4e2a\u53c2\u6570\u5e76\u8fd4\u56de\u4e00\u4e2a\u5b57\u7b26\u4e32\u95ee\u5019\u8bed\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u901a\u8fc7\u5bf9\u8c61\u8c03\u7528\u5916\u90e8\u51fd\u6570<\/h3>\n<\/p>\n<p><p>\u53e6\u4e00\u79cd\u8c03\u7528\u5916\u90e8\u51fd\u6570\u7684\u65b9\u6cd5\u662f\u5c06\u51fd\u6570\u4f5c\u4e3a\u5bf9\u8c61\u4f20\u9012\u7ed9\u7c7b\u3002\u8fd9\u6837\u505a\u7684\u597d\u5904\u662f\u53ef\u4ee5\u7075\u6d3b\u5730\u66ff\u6362\u51fd\u6570\uff0c\u5b9e\u73b0\u66f4\u9ad8\u7684\u4ee3\u7801\u590d\u7528\u6027\u548c\u53ef\u6d4b\u8bd5\u6027\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u5b9a\u4e49\u5916\u90e8\u51fd\u6570<\/p>\n<p>def greet(name):<\/p>\n<p>    return f&quot;Hello, {name}!&quot;<\/p>\n<h2><strong>\u5b9a\u4e49\u7c7b<\/strong><\/h2>\n<p>class Greeter:<\/p>\n<p>    def __init__(self, name, greet_func):<\/p>\n<p>        self.name = name<\/p>\n<p>        self.greet_func = greet_func<\/p>\n<p>    def greet_person(self):<\/p>\n<p>        # \u8c03\u7528\u4f20\u5165\u7684\u51fd\u6570<\/p>\n<p>        greeting = self.greet_func(self.name)<\/p>\n<p>        print(greeting)<\/p>\n<h2><strong>\u521b\u5efa\u7c7b\u7684\u5b9e\u4f8b<\/strong><\/h2>\n<p>person = Greeter(&quot;Jane&quot;, greet)<\/p>\n<h2><strong>\u8c03\u7528\u7c7b\u65b9\u6cd5<\/strong><\/h2>\n<p>person.greet_person()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u5916\u90e8\u51fd\u6570<code>greet<\/code>\u4f5c\u4e3a\u53c2\u6570\u4f20\u9012\u7ed9<code>Greeter<\/code>\u7c7b\u7684\u6784\u9020\u51fd\u6570\uff0c\u7136\u540e\u5728\u7c7b\u65b9\u6cd5<code>greet_person<\/code>\u4e2d\u8c03\u7528\u3002\u8fd9\u79cd\u65b9\u6cd5\u63d0\u9ad8\u4e86\u4ee3\u7801\u7684\u7075\u6d3b\u6027\uff0c\u5141\u8bb8\u5728\u8fd0\u884c\u65f6\u66f4\u6539\u51fd\u6570\u7684\u884c\u4e3a\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001\u901a\u8fc7\u6a21\u5757\u5bfc\u5165\u8c03\u7528\u5916\u90e8\u51fd\u6570<\/h3>\n<\/p>\n<p><p>\u5bf9\u4e8e\u66f4\u590d\u6742\u7684\u9879\u76ee\uff0c\u901a\u5e38\u5c06\u5916\u90e8\u51fd\u6570\u653e\u5728\u5355\u72ec\u7684\u6a21\u5757\u4e2d\u8fdb\u884c\u7ba1\u7406\u3002\u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c\u53ef\u4ee5\u4f7f\u7528Python\u7684<code>import<\/code>\u8bed\u53e5\u5bfc\u5165\u51fd\u6570\uff0c\u5e76\u5728\u7c7b\u4e2d\u8c03\u7528\u3002<\/p>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u521b\u5efa\u4e00\u4e2a\u540d\u4e3a<code>utils.py<\/code>\u7684\u6a21\u5757\u6587\u4ef6\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># utils.py<\/p>\n<p>def greet(name):<\/p>\n<p>    return f&quot;Hello, {name}!&quot;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u7136\u540e\uff0c\u5728\u4e3b\u6a21\u5757\u4e2d\u5bfc\u5165\u5e76\u8c03\u7528\u8fd9\u4e2a\u51fd\u6570\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># m<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>n.py<\/p>\n<p>from utils import greet<\/p>\n<p>class Greeter:<\/p>\n<p>    def __init__(self, name):<\/p>\n<p>        self.name = name<\/p>\n<p>    def greet_person(self):<\/p>\n<p>        # \u8c03\u7528\u5bfc\u5165\u7684\u51fd\u6570<\/p>\n<p>        greeting = greet(self.name)<\/p>\n<p>        print(greeting)<\/p>\n<h2><strong>\u521b\u5efa\u7c7b\u7684\u5b9e\u4f8b<\/strong><\/h2>\n<p>person = Greeter(&quot;Alice&quot;)<\/p>\n<h2><strong>\u8c03\u7528\u7c7b\u65b9\u6cd5<\/strong><\/h2>\n<p>person.greet_person()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u5c06\u51fd\u6570\u653e\u5728\u5355\u72ec\u7684\u6a21\u5757\u4e2d\uff0c\u53ef\u4ee5\u66f4\u597d\u5730\u7ec4\u7ec7\u4ee3\u7801\uff0c\u63d0\u9ad8\u5176\u53ef\u7ef4\u62a4\u6027\u3002<code>main.py<\/code>\u6587\u4ef6\u901a\u8fc7<code>from utils import greet<\/code>\u8bed\u53e5\u5bfc\u5165<code>greet<\/code>\u51fd\u6570\uff0c\u5e76\u5728\u7c7b\u4e2d\u8c03\u7528\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001\u4f7f\u7528\u88c5\u9970\u5668\u8c03\u7528\u5916\u90e8\u51fd\u6570<\/h3>\n<\/p>\n<p><p>\u88c5\u9970\u5668\u662f\u4e00\u79cd\u7279\u6b8a\u7684\u51fd\u6570\uff0c\u5b83\u53ef\u4ee5\u4fee\u6539\u5176\u4ed6\u51fd\u6570\u6216\u65b9\u6cd5\u7684\u884c\u4e3a\u3002\u5728\u7c7b\u4e2d\u4f7f\u7528\u88c5\u9970\u5668\u53ef\u4ee5\u4e3a\u65b9\u6cd5\u6dfb\u52a0\u989d\u5916\u7684\u529f\u80fd\uff0c\u4f8b\u5982\u65e5\u5fd7\u8bb0\u5f55\u3001\u6743\u9650\u68c0\u67e5\u7b49\u3002<\/p>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u5b9a\u4e49\u4e00\u4e2a\u88c5\u9970\u5668\u51fd\u6570\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def log_decorator(func):<\/p>\n<p>    def wrapper(*args, kwargs):<\/p>\n<p>        print(f&quot;Calling function {func.__name__}&quot;)<\/p>\n<p>        result = func(*args, kwargs)<\/p>\n<p>        print(f&quot;Function {func.__name__} finished&quot;)<\/p>\n<p>        return result<\/p>\n<p>    return wrapper<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u7136\u540e\uff0c\u5728\u7c7b\u4e2d\u4f7f\u7528\u8fd9\u4e2a\u88c5\u9970\u5668\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">class Greeter:<\/p>\n<p>    def __init__(self, name):<\/p>\n<p>        self.name = name<\/p>\n<p>    @log_decorator<\/p>\n<p>    def greet_person(self):<\/p>\n<p>        print(f&quot;Hello, {self.name}!&quot;)<\/p>\n<h2><strong>\u521b\u5efa\u7c7b\u7684\u5b9e\u4f8b<\/strong><\/h2>\n<p>person = Greeter(&quot;Bob&quot;)<\/p>\n<h2><strong>\u8c03\u7528\u7c7b\u65b9\u6cd5<\/strong><\/h2>\n<p>person.greet_person()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u88c5\u9970\u5668<code>log_decorator<\/code>\u88ab\u5e94\u7528\u4e8e<code>greet_person<\/code>\u65b9\u6cd5\u3002\u88c5\u9970\u5668\u5728\u65b9\u6cd5\u8c03\u7528\u524d\u540e\u5206\u522b\u6253\u5370\u65e5\u5fd7\u4fe1\u606f\u3002\u901a\u8fc7\u4f7f\u7528\u88c5\u9970\u5668\uff0c\u53ef\u4ee5\u5728\u4e0d\u4fee\u6539\u539f\u59cb\u65b9\u6cd5\u7684\u60c5\u51b5\u4e0b\u6dfb\u52a0\u989d\u5916\u7684\u529f\u80fd\u3002<\/p>\n<\/p>\n<p><h3>\u4e94\u3001\u901a\u8fc7\u7c7b\u65b9\u6cd5\u8c03\u7528\u5916\u90e8\u51fd\u6570<\/h3>\n<\/p>\n<p><p>\u5982\u679c\u5916\u90e8\u51fd\u6570\u4e0e\u7c7b\u6709\u8f83\u5f3a\u7684\u8026\u5408\u6027\uff0c\u53ef\u4ee5\u5c06\u5176\u5c01\u88c5\u4e3a\u7c7b\u65b9\u6cd5\u3002\u8fd9\u6837\u505a\u7684\u597d\u5904\u662f\uff0c\u51fd\u6570\u53ef\u4ee5\u8bbf\u95ee\u7c7b\u7684\u5c5e\u6027\u548c\u65b9\u6cd5\uff0c\u4ece\u800c\u5b9e\u73b0\u66f4\u590d\u6742\u7684\u903b\u8f91\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">class Greeter:<\/p>\n<p>    def __init__(self, name):<\/p>\n<p>        self.name = name<\/p>\n<p>    @classmethod<\/p>\n<p>    def greet(cls, name):<\/p>\n<p>        return f&quot;Hello, {name}!&quot;<\/p>\n<p>    def greet_person(self):<\/p>\n<p>        # \u8c03\u7528\u7c7b\u65b9\u6cd5<\/p>\n<p>        greeting = self.greet(self.name)<\/p>\n<p>        print(greeting)<\/p>\n<h2><strong>\u521b\u5efa\u7c7b\u7684\u5b9e\u4f8b<\/strong><\/h2>\n<p>person = Greeter(&quot;Dave&quot;)<\/p>\n<h2><strong>\u8c03\u7528\u7c7b\u65b9\u6cd5<\/strong><\/h2>\n<p>person.greet_person()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c<code>greet<\/code>\u65b9\u6cd5\u88ab\u5b9a\u4e49\u4e3a\u7c7b\u65b9\u6cd5\uff0c\u5e76\u901a\u8fc7<code>@classmethod<\/code>\u88c5\u9970\u5668\u8fdb\u884c\u4fee\u9970\u3002\u7c7b\u65b9\u6cd5\u53ef\u4ee5\u901a\u8fc7\u7c7b\u540d\u6216\u5b9e\u4f8b\u540d\u8fdb\u884c\u8c03\u7528\u3002\u5728<code>greet_person<\/code>\u65b9\u6cd5\u4e2d\uff0c\u7c7b\u65b9\u6cd5<code>greet<\/code>\u88ab\u8c03\u7528\u5e76\u751f\u6210\u95ee\u5019\u8bed\u3002<\/p>\n<\/p>\n<p><h3>\u516d\u3001\u901a\u8fc7\u9759\u6001\u65b9\u6cd5\u8c03\u7528\u5916\u90e8\u51fd\u6570<\/h3>\n<\/p>\n<p><p>\u9759\u6001\u65b9\u6cd5\u7c7b\u4f3c\u4e8e\u7c7b\u65b9\u6cd5\uff0c\u4f46\u5b83\u4eec\u4e0d\u4f9d\u8d56\u4e8e\u7c7b\u6216\u5b9e\u4f8b\u7684\u72b6\u6001\u3002\u9759\u6001\u65b9\u6cd5\u901a\u8fc7<code>@staticmethod<\/code>\u88c5\u9970\u5668\u8fdb\u884c\u4fee\u9970\uff0c\u9002\u5408\u5b9e\u73b0\u4e0e\u7c7b\u903b\u8f91\u7d27\u5bc6\u76f8\u5173\u7684\u72ec\u7acb\u529f\u80fd\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">class Greeter:<\/p>\n<p>    def __init__(self, name):<\/p>\n<p>        self.name = name<\/p>\n<p>    @staticmethod<\/p>\n<p>    def greet(name):<\/p>\n<p>        return f&quot;Hello, {name}!&quot;<\/p>\n<p>    def greet_person(self):<\/p>\n<p>        # \u8c03\u7528\u9759\u6001\u65b9\u6cd5<\/p>\n<p>        greeting = self.greet(self.name)<\/p>\n<p>        print(greeting)<\/p>\n<h2><strong>\u521b\u5efa\u7c7b\u7684\u5b9e\u4f8b<\/strong><\/h2>\n<p>person = Greeter(&quot;Eve&quot;)<\/p>\n<h2><strong>\u8c03\u7528\u7c7b\u65b9\u6cd5<\/strong><\/h2>\n<p>person.greet_person()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c<code>greet<\/code>\u65b9\u6cd5\u88ab\u5b9a\u4e49\u4e3a\u9759\u6001\u65b9\u6cd5\uff0c\u5e76\u901a\u8fc7<code>@staticmethod<\/code>\u88c5\u9970\u5668\u8fdb\u884c\u4fee\u9970\u3002\u9759\u6001\u65b9\u6cd5\u53ef\u4ee5\u76f4\u63a5\u901a\u8fc7\u7c7b\u540d\u6216\u5b9e\u4f8b\u540d\u8fdb\u884c\u8c03\u7528\u3002\u5728<code>greet_person<\/code>\u65b9\u6cd5\u4e2d\uff0c\u9759\u6001\u65b9\u6cd5<code>greet<\/code>\u88ab\u8c03\u7528\u5e76\u751f\u6210\u95ee\u5019\u8bed\u3002<\/p>\n<\/p>\n<p><h3>\u4e03\u3001\u901a\u8fc7\u7ee7\u627f\u8c03\u7528\u5916\u90e8\u51fd\u6570<\/h3>\n<\/p>\n<p><p>\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0c\u53ef\u4ee5\u901a\u8fc7\u7ee7\u627f\u7236\u7c7b\u7684\u65b9\u5f0f\u6765\u8c03\u7528\u5916\u90e8\u51fd\u6570\u3002\u8fd9\u79cd\u65b9\u6cd5\u9002\u7528\u4e8e\u51fd\u6570\u5728\u591a\u4e2a\u7c7b\u4e2d\u5171\u4eab\u7684\u60c5\u51b5\u3002<\/p>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u5b9a\u4e49\u4e00\u4e2a\u57fa\u7c7b\uff0c\u5e76\u5728\u5176\u4e2d\u5b9a\u4e49\u5916\u90e8\u51fd\u6570\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">class BaseGreeter:<\/p>\n<p>    def greet(self, name):<\/p>\n<p>        return f&quot;Hello, {name}!&quot;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u7136\u540e\uff0c\u5b9a\u4e49\u4e00\u4e2a\u5b50\u7c7b\uff0c\u5e76\u5728\u5176\u4e2d\u8c03\u7528\u57fa\u7c7b\u7684\u65b9\u6cd5\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">class Greeter(BaseGreeter):<\/p>\n<p>    def __init__(self, name):<\/p>\n<p>        self.name = name<\/p>\n<p>    def greet_person(self):<\/p>\n<p>        # \u8c03\u7528\u57fa\u7c7b\u65b9\u6cd5<\/p>\n<p>        greeting = self.greet(self.name)<\/p>\n<p>        print(greeting)<\/p>\n<h2><strong>\u521b\u5efa\u5b50\u7c7b\u7684\u5b9e\u4f8b<\/strong><\/h2>\n<p>person = Greeter(&quot;Frank&quot;)<\/p>\n<h2><strong>\u8c03\u7528\u5b50\u7c7b\u65b9\u6cd5<\/strong><\/h2>\n<p>person.greet_person()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c<code>BaseGreeter<\/code>\u7c7b\u5b9a\u4e49\u4e86<code>greet<\/code>\u65b9\u6cd5\u3002<code>Greeter<\/code>\u7c7b\u7ee7\u627f\u81ea<code>BaseGreeter<\/code>\uff0c\u5e76\u5728<code>greet_person<\/code>\u65b9\u6cd5\u4e2d\u8c03\u7528<code>greet<\/code>\u65b9\u6cd5\u3002\u8fd9\u79cd\u65b9\u6cd5\u6709\u52a9\u4e8e\u51cf\u5c11\u4ee3\u7801\u91cd\u590d\uff0c\u63d0\u9ad8\u4ee3\u7801\u7684\u53ef\u7ef4\u62a4\u6027\u3002<\/p>\n<\/p>\n<p><h3>\u516b\u3001\u901a\u8fc7\u95ed\u5305\u8c03\u7528\u5916\u90e8\u51fd\u6570<\/h3>\n<\/p>\n<p><p>\u95ed\u5305\u662f\u4e00\u79cd\u7279\u6b8a\u7684\u51fd\u6570\uff0c\u5b83\u53ef\u4ee5\u6355\u83b7\u548c\u4fdd\u5b58\u5176\u6240\u5728\u4f5c\u7528\u57df\u7684\u53d8\u91cf\u3002\u5728\u7c7b\u4e2d\u4f7f\u7528\u95ed\u5305\uff0c\u53ef\u4ee5\u5b9e\u73b0\u66f4\u52a0\u7075\u6d3b\u548c\u52a8\u6001\u7684\u51fd\u6570\u8c03\u7528\u3002<\/p>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u5b9a\u4e49\u4e00\u4e2a\u8fd4\u56de\u95ed\u5305\u7684\u51fd\u6570\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def make_greeter(name):<\/p>\n<p>    def greet():<\/p>\n<p>        return f&quot;Hello, {name}!&quot;<\/p>\n<p>    return greet<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u7136\u540e\uff0c\u5728\u7c7b\u4e2d\u4f7f\u7528\u8fd9\u4e2a\u95ed\u5305\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">class Greeter:<\/p>\n<p>    def __init__(self, name):<\/p>\n<p>        self.greet_func = make_greeter(name)<\/p>\n<p>    def greet_person(self):<\/p>\n<p>        # \u8c03\u7528\u95ed\u5305<\/p>\n<p>        greeting = self.greet_func()<\/p>\n<p>        print(greeting)<\/p>\n<h2><strong>\u521b\u5efa\u7c7b\u7684\u5b9e\u4f8b<\/strong><\/h2>\n<p>person = Greeter(&quot;Grace&quot;)<\/p>\n<h2><strong>\u8c03\u7528\u7c7b\u65b9\u6cd5<\/strong><\/h2>\n<p>person.greet_person()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c<code>make_greeter<\/code>\u51fd\u6570\u8fd4\u56de\u4e00\u4e2a\u95ed\u5305<code>greet<\/code>\uff0c\u8be5\u95ed\u5305\u6355\u83b7\u4e86<code>name<\/code>\u53d8\u91cf\u3002<code>Greeter<\/code>\u7c7b\u5728\u6784\u9020\u51fd\u6570\u4e2d\u8c03\u7528<code>make_greeter<\/code>\u51fd\u6570\uff0c\u5e76\u5c06\u8fd4\u56de\u7684\u95ed\u5305\u8d4b\u503c\u7ed9<code>greet_func<\/code>\u5c5e\u6027\u3002\u5728<code>greet_person<\/code>\u65b9\u6cd5\u4e2d\uff0c\u8c03\u7528<code>greet_func<\/code>\u95ed\u5305\u5e76\u751f\u6210\u95ee\u5019\u8bed\u3002<\/p>\n<\/p>\n<p><p>\u901a\u8fc7\u8fd9\u79cd\u65b9\u6cd5\uff0c\u53ef\u4ee5\u5728\u7c7b\u4e2d\u5b9e\u73b0\u66f4\u52a0\u7075\u6d3b\u548c\u52a8\u6001\u7684\u51fd\u6570\u8c03\u7528\u3002<\/p>\n<\/p>\n<p><h3>\u4e5d\u3001\u901a\u8fc7\u63a5\u53e3\u8c03\u7528\u5916\u90e8\u51fd\u6570<\/h3>\n<\/p>\n<p><p>\u5728\u67d0\u4e9b\u590d\u6742\u7684\u9879\u76ee\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u63a5\u53e3\u6765\u5b9a\u4e49\u5916\u90e8\u51fd\u6570\u7684\u884c\u4e3a\u3002\u8fd9\u79cd\u65b9\u6cd5\u9002\u7528\u4e8e\u9700\u8981\u4e25\u683c\u63a7\u5236\u51fd\u6570\u884c\u4e3a\u548c\u5b9e\u73b0\u7684\u60c5\u51b5\u3002<\/p>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u5b9a\u4e49\u4e00\u4e2a\u63a5\u53e3\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from abc import ABC, abstractmethod<\/p>\n<p>class GreeterInterface(ABC):<\/p>\n<p>    @abstractmethod<\/p>\n<p>    def greet(self, name):<\/p>\n<p>        pass<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u7136\u540e\uff0c\u5b9e\u73b0\u8fd9\u4e2a\u63a5\u53e3\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">class GreeterImplementation(GreeterInterface):<\/p>\n<p>    def greet(self, name):<\/p>\n<p>        return f&quot;Hello, {name}!&quot;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u6700\u540e\uff0c\u5728\u7c7b\u4e2d\u4f7f\u7528\u63a5\u53e3\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">class Greeter:<\/p>\n<p>    def __init__(self, name, greeter):<\/p>\n<p>        self.name = name<\/p>\n<p>        self.greeter = greeter<\/p>\n<p>    def greet_person(self):<\/p>\n<p>        # \u8c03\u7528\u63a5\u53e3\u65b9\u6cd5<\/p>\n<p>        greeting = self.greeter.greet(self.name)<\/p>\n<p>        print(greeting)<\/p>\n<h2><strong>\u521b\u5efa\u63a5\u53e3\u5b9e\u73b0\u7684\u5b9e\u4f8b<\/strong><\/h2>\n<p>greeter_impl = GreeterImplementation()<\/p>\n<h2><strong>\u521b\u5efa\u7c7b\u7684\u5b9e\u4f8b<\/strong><\/h2>\n<p>person = Greeter(&quot;Hank&quot;, greeter_impl)<\/p>\n<h2><strong>\u8c03\u7528\u7c7b\u65b9\u6cd5<\/strong><\/h2>\n<p>person.greet_person()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c<code>GreeterInterface<\/code>\u5b9a\u4e49\u4e86<code>greet<\/code>\u65b9\u6cd5\u7684\u63a5\u53e3\uff0c<code>GreeterImplementation<\/code>\u5b9e\u73b0\u4e86\u8fd9\u4e2a\u63a5\u53e3\u3002<code>Greeter<\/code>\u7c7b\u63a5\u53d7\u4e00\u4e2a\u5b9e\u73b0\u4e86<code>GreeterInterface<\/code>\u7684\u5bf9\u8c61\uff0c\u5e76\u5728<code>greet_person<\/code>\u65b9\u6cd5\u4e2d\u8c03\u7528<code>greet<\/code>\u65b9\u6cd5\u3002\u8fd9\u79cd\u65b9\u6cd5\u6709\u52a9\u4e8e\u5b9e\u73b0\u66f4\u9ad8\u7684\u4ee3\u7801\u62bd\u8c61\u548c\u7075\u6d3b\u6027\u3002<\/p>\n<\/p>\n<p><h3>\u5341\u3001\u901a\u8fc7\u4e8b\u4ef6\u9a71\u52a8\u8c03\u7528\u5916\u90e8\u51fd\u6570<\/h3>\n<\/p>\n<p><p>\u4e8b\u4ef6\u9a71\u52a8\u7f16\u7a0b\u662f\u4e00\u79cd\u5e38\u89c1\u7684\u7f16\u7a0b\u8303\u5f0f\uff0c\u7279\u522b\u9002\u7528\u4e8eGUI\u5e94\u7528\u548c\u5f02\u6b65\u7f16\u7a0b\u3002\u5728\u4e8b\u4ef6\u9a71\u52a8\u7f16\u7a0b\u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7\u4e8b\u4ef6\u89e6\u53d1\u5668\u8c03\u7528\u5916\u90e8\u51fd\u6570\u3002<\/p>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u5b9a\u4e49\u4e00\u4e2a\u4e8b\u4ef6\u89e6\u53d1\u5668\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">class EventTrigger:<\/p>\n<p>    def __init__(self):<\/p>\n<p>        self._listeners = []<\/p>\n<p>    def add_listener(self, listener):<\/p>\n<p>        self._listeners.append(listener)<\/p>\n<p>    def trigger_event(self, *args, kwargs):<\/p>\n<p>        for listener in self._listeners:<\/p>\n<p>            listener(*args, kwargs)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u7136\u540e\uff0c\u5728\u7c7b\u4e2d\u4f7f\u7528\u8fd9\u4e2a\u89e6\u53d1\u5668\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">class Greeter:<\/p>\n<p>    def __init__(self, name):<\/p>\n<p>        self.name = name<\/p>\n<p>        self.event_trigger = EventTrigger()<\/p>\n<p>        self.event_trigger.add_listener(self.greet_person)<\/p>\n<p>    def greet_person(self):<\/p>\n<p>        print(f&quot;Hello, {self.name}!&quot;)<\/p>\n<h2><strong>\u521b\u5efa\u7c7b\u7684\u5b9e\u4f8b<\/strong><\/h2>\n<p>person = Greeter(&quot;Ivy&quot;)<\/p>\n<h2><strong>\u89e6\u53d1\u4e8b\u4ef6<\/strong><\/h2>\n<p>person.event_trigger.trigger_event()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c<code>EventTrigger<\/code>\u7c7b\u5b9a\u4e49\u4e86\u4e8b\u4ef6\u89e6\u53d1\u5668\u7684\u884c\u4e3a\uff0c\u5305\u62ec\u6dfb\u52a0\u76d1\u542c\u5668\u548c\u89e6\u53d1\u4e8b\u4ef6\u3002<code>Greeter<\/code>\u7c7b\u521b\u5efa\u4e86\u4e00\u4e2a<code>EventTrigger<\/code>\u5b9e\u4f8b\uff0c\u5e76\u5c06<code>greet_person<\/code>\u65b9\u6cd5\u6dfb\u52a0\u4e3a\u4e8b\u4ef6\u76d1\u542c\u5668\u3002\u5f53\u4e8b\u4ef6\u89e6\u53d1\u5668\u89e6\u53d1\u4e8b\u4ef6\u65f6\uff0c\u6240\u6709\u6dfb\u52a0\u7684\u76d1\u542c\u5668\u90fd\u4f1a\u88ab\u8c03\u7528\u3002<\/p>\n<\/p>\n<p><p>\u901a\u8fc7\u8fd9\u79cd\u65b9\u6cd5\uff0c\u53ef\u4ee5\u5b9e\u73b0\u57fa\u4e8e\u4e8b\u4ef6\u7684\u5916\u90e8\u51fd\u6570\u8c03\u7528\uff0c\u63d0\u9ad8\u4ee3\u7801\u7684\u54cd\u5e94\u6027\u548c\u7075\u6d3b\u6027\u3002<\/p>\n<\/p>\n<p><p>\u603b\u7ed3\uff1a<\/p>\n<\/p>\n<p><p>\u901a\u8fc7\u4e0a\u8ff0\u65b9\u6cd5\uff0c\u53ef\u4ee5\u5728Python\u7c7b\u4e2d\u7075\u6d3b\u5730\u8c03\u7528\u5916\u90e8\u51fd\u6570\u3002\u6bcf\u79cd\u65b9\u6cd5\u90fd\u6709\u5176\u9002\u7528\u7684\u573a\u666f\u548c\u4f18\u52bf\uff0c\u5f00\u53d1\u8005\u53ef\u4ee5\u6839\u636e\u5177\u4f53\u9700\u6c42\u9009\u62e9\u6700\u5408\u9002\u7684\u65b9\u6cd5\u3002\u65e0\u8bba\u662f\u76f4\u63a5\u8c03\u7528\u3001\u901a\u8fc7\u5bf9\u8c61\u4f20\u9012\u3001\u4f7f\u7528\u88c5\u9970\u5668\uff0c\u8fd8\u662f\u901a\u8fc7\u7ee7\u627f\u3001\u95ed\u5305\u3001\u63a5\u53e3\u3001\u4e8b\u4ef6\u9a71\u52a8\u7b49\u65b9\u5f0f\uff0c\u5408\u7406\u4f7f\u7528\u8fd9\u4e9b\u6280\u672f\u53ef\u4ee5\u663e\u8457\u63d0\u9ad8\u4ee3\u7801\u7684\u53ef\u7ef4\u62a4\u6027\u548c\u590d\u7528\u6027\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u7c7b\u4e2d\u8c03\u7528\u5916\u90e8\u51fd\u6570\uff1f<\/strong><\/p>\n<p>\u5728Python\u7c7b\u4e2d\u8c03\u7528\u5916\u90e8\u51fd\u6570\u975e\u5e38\u7b80\u5355\u3002\u60a8\u53ea\u9700\u786e\u4fdd\u5728\u7c7b\u7684\u5b9a\u4e49\u4e4b\u524d\u5df2\u7ecf\u5b9a\u4e49\u4e86\u6240\u9700\u7684\u5916\u90e8\u51fd\u6570\u3002\u7136\u540e\uff0c\u60a8\u53ef\u4ee5\u5728\u7c7b\u7684\u65b9\u6cd5\u4e2d\u76f4\u63a5\u4f7f\u7528\u8be5\u51fd\u6570\u3002\u4f8b\u5982\uff0c\u60a8\u53ef\u4ee5\u5c06\u5916\u90e8\u51fd\u6570\u653e\u5728\u540c\u4e00\u4e2a\u6587\u4ef6\u4e2d\uff0c\u6216\u8005\u4ece\u5176\u4ed6\u6a21\u5757\u5bfc\u5165\u5b83\u3002<\/p>\n<p><strong>\u5728\u7c7b\u65b9\u6cd5\u4e2d\u4f20\u9012\u53c2\u6570\u7ed9\u5916\u90e8\u51fd\u6570\u6709\u4ec0\u4e48\u6ce8\u610f\u4e8b\u9879\uff1f<\/strong><\/p>\n<p>\u5728\u8c03\u7528\u5916\u90e8\u51fd\u6570\u65f6\uff0c\u786e\u4fdd\u4f20\u9012\u6b63\u786e\u7684\u53c2\u6570\u7c7b\u578b\u662f\u81f3\u5173\u91cd\u8981\u7684\u3002\u5982\u679c\u5916\u90e8\u51fd\u6570\u671f\u671b\u7279\u5b9a\u7c7b\u578b\u7684\u53c2\u6570\uff0c\u60a8\u9700\u8981\u5728\u7c7b\u7684\u65b9\u6cd5\u4e2d\u8fdb\u884c\u76f8\u5e94\u7684\u8f6c\u6362\u3002\u6b64\u5916\uff0c\u6ce8\u610f\u5916\u90e8\u51fd\u6570\u7684\u8fd4\u56de\u503c\uff0c\u5982\u679c\u9700\u8981\uff0c\u53ef\u4ee5\u5c06\u8fd4\u56de\u503c\u5b58\u50a8\u5728\u7c7b\u7684\u5c5e\u6027\u4e2d\u4ee5\u4f9b\u540e\u7eed\u4f7f\u7528\u3002<\/p>\n<p><strong>\u662f\u5426\u53ef\u4ee5\u5728\u7c7b\u4e2d\u5b9a\u4e49\u548c\u4f7f\u7528\u591a\u4e2a\u5916\u90e8\u51fd\u6570\uff1f<\/strong><\/p>\n<p>\u53ef\u4ee5\u3002\u5728Python\u7c7b\u4e2d\uff0c\u60a8\u53ef\u4ee5\u5b9a\u4e49\u591a\u4e2a\u5916\u90e8\u51fd\u6570\uff0c\u5e76\u5728\u7c7b\u7684\u4e0d\u540c\u65b9\u6cd5\u4e2d\u8c03\u7528\u5b83\u4eec\u3002\u786e\u4fdd\u8fd9\u4e9b\u5916\u90e8\u51fd\u6570\u7684\u540d\u79f0\u4e0d\u4f1a\u4e0e\u7c7b\u4e2d\u7684\u65b9\u6cd5\u6216\u5c5e\u6027\u51b2\u7a81\uff0c\u4ee5\u907f\u514d\u6f5c\u5728\u7684\u547d\u540d\u51b2\u7a81\u3002\u901a\u8fc7\u5408\u7406\u7684\u7ec4\u7ec7\u548c\u547d\u540d\uff0c\u53ef\u4ee5\u6709\u6548\u5730\u7ba1\u7406\u591a\u4e2a\u5916\u90e8\u51fd\u6570\u7684\u8c03\u7528\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u7c7b\u4e2d\u8c03\u7528\u5916\u90e8\u51fd\u6570\u7684\u65b9\u6cd5\u6709\u5f88\u591a\u79cd\uff0c\u5305\u62ec\u76f4\u63a5\u8c03\u7528\u3001\u901a\u8fc7\u5bf9\u8c61\u8c03\u7528\u3001\u4f7f\u7528\u88c5\u9970\u5668\u7b49\u3002\u5173\u952e\u70b9\u5305\u62ec\u76f4\u63a5\u8c03\u7528\u3001\u5b9e [&hellip;]","protected":false},"author":3,"featured_media":1095474,"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\/1095469"}],"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=1095469"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1095469\/revisions"}],"predecessor-version":[{"id":1095479,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1095469\/revisions\/1095479"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1095474"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1095469"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1095469"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1095469"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}