{"id":101500,"date":"2026-09-06T23:25:18","date_gmt":"2026-09-06T15:25:18","guid":{"rendered":"https:\/\/www.wsisp.com\/helps\/101500.html"},"modified":"2026-09-06T23:25:18","modified_gmt":"2026-09-06T15:25:18","slug":"python-%e4%b8%89-%ef%bc%88%e4%b8%89%ef%bc%89-3-%e9%9d%a2%e5%90%91%e5%af%b9%e8%b1%a1%e4%b8%89%e5%a4%a7%e7%89%b9%e5%be%81","status":"publish","type":"post","link":"https:\/\/www.wsisp.com\/helps\/101500.html","title":{"rendered":"Python.\u4e09.\uff08\u4e09\uff09--3. \u9762\u5411\u5bf9\u8c61\u4e09\u5927\u7279\u5f81"},"content":{"rendered":"<h2 style=\"background-color:transparent\">\u4e09\u3001 \u51fd\u6570\u3001\u7c7b\u4e0e\u9762\u5411\u5bf9\u8c61\u7f16\u7a0b (OOP)<\/h2>\n<ul>\n<li>\u6807\u9898&#xff1a;\n<li>\u51fd\u6570\u53c2\u6570\u4e0e\u9ad8\u9636\u51fd\u6570<\/li>\n<li>\u88c5\u9970\u5668\u4e0e\u95ed\u5305<\/li>\n<li>\u9762\u5411\u5bf9\u8c61\u4e09\u5927\u7279\u5f81<\/li>\n<li>\u7c7b\u3001\u5bf9\u8c61\u4e0e\u9b54\u672f\u65b9\u6cd5<\/li>\n<li>\u5f02\u5e38\u5904\u7406<\/li>\n<\/li>\n<\/ul>\n<h2><\/h2>\n<\/p>\n<hr \/>\n<h3 id=\"17.\u4ecb\u7ecd\u4e00\u4e0bPython\u4e2d\u7684\u5c01\u88c5(Encapsulation)\u601d\u60f3\" style=\"background-color:transparent\">\u4ecb\u7ecd\u4e00\u4e0bPython\u4e2d\u7684\u5c01\u88c5(Encapsulation)\u601d\u60f3<\/h3>\n<h4>\u5c01\u88c5 (Encapsulation) \u5728 Python \u4e2d\u7684\u6982\u5ff5<\/h4>\n<p>\u5c01\u88c5\u662f\u9762\u5411\u5bf9\u8c61\u7f16\u7a0b&#xff08;OOP&#xff09;\u7684\u56db\u5927\u57fa\u672c\u539f\u5219\u4e4b\u4e00&#xff0c;\u5176\u4ed6\u4e09\u4e2a\u662f\u7ee7\u627f&#xff08;Inheritance&#xff09;\u3001\u591a\u6001&#xff08;Polymorphism&#xff09;\u548c\u62bd\u8c61&#xff08;Abstraction&#xff09;\u3002\u5c01\u88c5\u7684\u6838\u5fc3\u601d\u60f3\u662f\u5c06\u5bf9\u8c61\u7684\u6570\u636e&#xff08;\u5c5e\u6027&#xff09;\u548c\u884c\u4e3a&#xff08;\u65b9\u6cd5&#xff09;\u6253\u5305\u5728\u4e00\u8d77&#xff0c;\u5e76\u9650\u5236\u5916\u754c\u5bf9\u5b83\u4eec\u7684\u76f4\u63a5\u8bbf\u95ee\u3002\u901a\u8fc7\u5c01\u88c5&#xff0c;\u5f00\u53d1\u4eba\u5458\u53ef\u4ee5\u63a7\u5236\u54ea\u4e9b\u6570\u636e\u53ef\u4ee5\u4ece\u5916\u90e8\u8bbf\u95ee&#xff0c;\u54ea\u4e9b\u53ea\u80fd\u5728\u7c7b\u7684\u5185\u90e8\u4f7f\u7528\u3002<\/p>\n<p>Python \u867d\u7136\u4e0d\u50cf\u4e00\u4e9b\u5176\u4ed6\u9762\u5411\u5bf9\u8c61\u7684\u7f16\u7a0b\u8bed\u8a00&#xff08;\u5982 Java\u3001C&#043;&#043;&#xff09;\u90a3\u6837\u4e25\u683c\u5730\u9650\u5236\u6570\u636e\u7684\u8bbf\u95ee&#xff0c;\u4f46\u5b83\u4f9d\u7136\u652f\u6301\u901a\u8fc7\u547d\u540d\u7ea6\u5b9a\u548c\u8bbf\u95ee\u63a7\u5236\u6765\u5b9e\u73b0\u5c01\u88c5\u7684\u6982\u5ff5\u3002<\/p>\n<h4>\u5c01\u88c5\u7684\u4e3b\u8981\u601d\u60f3<\/h4>\n<p>\u5c01\u88c5\u4e3b\u8981\u6d89\u53ca\u4ee5\u4e0b\u51e0\u4e2a\u65b9\u9762&#xff1a;<\/p>\n<li>\u9690\u85cf\u5185\u90e8\u5b9e\u73b0&#xff1a;\u5bf9\u8c61\u7684\u5185\u90e8\u72b6\u6001\u5bf9\u5916\u754c\u4e0d\u53ef\u89c1&#xff0c;\u5916\u754c\u53ea\u80fd\u901a\u8fc7\u516c\u5f00\u7684\u63a5\u53e3&#xff08;\u5373\u65b9\u6cd5&#xff09;\u8bbf\u95ee\u6216\u4fee\u6539\u5bf9\u8c61\u7684\u72b6\u6001\u3002<\/li>\n<li>\u4fdd\u62a4\u5bf9\u8c61\u7684\u5b8c\u6574\u6027&#xff1a;\u901a\u8fc7\u5c01\u88c5&#xff0c;\u7c7b\u7684\u8bbe\u8ba1\u8005\u53ef\u4ee5\u63a7\u5236\u5916\u90e8\u5982\u4f55\u8bbf\u95ee\u6216\u4fee\u6539\u5185\u90e8\u6570\u636e&#xff0c;\u907f\u514d\u5916\u90e8\u5bf9\u5185\u90e8\u6570\u636e\u8fdb\u884c\u975e\u6cd5\u7684\u64cd\u4f5c&#xff0c;\u786e\u4fdd\u5bf9\u8c61\u7684\u4e00\u81f4\u6027\u548c\u5b8c\u6574\u6027\u3002<\/li>\n<li>\u63d0\u4f9b\u5b89\u5168\u7684\u8bbf\u95ee\u63a5\u53e3&#xff1a;\u901a\u8fc7\u5b9a\u4e49\u7c7b\u7684\u516c\u6709\u65b9\u6cd5&#xff08;public methods&#xff09;&#xff0c;\u5916\u90e8\u53ef\u4ee5\u5728\u4e0d\u76f4\u63a5\u64cd\u4f5c\u5185\u90e8\u6570\u636e\u7684\u60c5\u51b5\u4e0b&#xff0c;\u5b89\u5168\u5730\u5bf9\u5bf9\u8c61\u8fdb\u884c\u64cd\u4f5c\u3002<\/li>\n<h4 style=\"background-color:transparent\">Python \u4e2d\u7684\u5c01\u88c5\u673a\u5236<\/h4>\n<p>\u5728 Python \u4e2d&#xff0c;\u5c01\u88c5\u7684\u5b9e\u73b0\u4e3b\u8981\u4f9d\u8d56\u547d\u540d\u7ea6\u5b9a\u548c\u8bbf\u95ee\u63a7\u5236&#xff0c;Python \u6ca1\u6709\u50cf\u67d0\u4e9b\u7f16\u7a0b\u8bed\u8a00\u90a3\u6837\u63d0\u4f9b\u660e\u786e\u7684\u8bbf\u95ee\u6743\u9650\u63a7\u5236\u7b26&#xff08;\u5982 Java \u7684\u00a0public\u3001private\u3001protected&#xff09;&#xff0c;\u4f46\u5b83\u6709\u4e00\u4e9b\u7ea6\u5b9a\u4fd7\u6210\u7684\u89c4\u5219\u6765\u5b9e\u73b0\u5c01\u88c5\u3002<\/p>\n<h5>1. \u516c\u6709\u6210\u5458 (Public Members)<\/h5>\n<p>\u5728 Python \u4e2d&#xff0c;\u9ed8\u8ba4\u60c5\u51b5\u4e0b&#xff0c;\u7c7b\u7684\u6240\u6709\u5c5e\u6027\u548c\u65b9\u6cd5\u90fd\u662f\u516c\u6709\u7684&#xff08;public&#xff09;\u3002\u8fd9\u610f\u5473\u7740\u5916\u90e8\u53ef\u4ee5\u76f4\u63a5\u8bbf\u95ee\u6216\u4fee\u6539\u8fd9\u4e9b\u5c5e\u6027\u548c\u65b9\u6cd5\u3002\u4f8b\u5982&#xff1a;<\/p>\n<p>class MyClass:<br \/>\n    def __init__(self, name):<br \/>\n        self.name &#061; name  # \u516c\u6709\u5c5e\u6027<\/p>\n<p>    def greet(self):  # \u516c\u6709\u65b9\u6cd5<br \/>\n        return f&#034;Hello, {self.name}&#034;<\/p>\n<p># \u4f7f\u7528<br \/>\nobj &#061; MyClass(&#034;Alice&#034;)<br \/>\nprint(obj.name)      # \u76f4\u63a5\u8bbf\u95ee\u516c\u6709\u5c5e\u6027<br \/>\nprint(obj.greet())   # \u8c03\u7528\u516c\u6709\u65b9\u6cd5<\/p>\n<p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d&#xff0c;name\u00a0\u5c5e\u6027\u548c\u00a0greet()\u00a0\u65b9\u6cd5\u90fd\u662f\u516c\u6709\u7684&#xff0c;\u5916\u90e8\u53ef\u4ee5\u76f4\u63a5\u8bbf\u95ee\u5b83\u4eec\u3002<\/p>\n<h5>2. \u79c1\u6709\u6210\u5458 (Private Members)<\/h5>\n<p>\u5728 Python \u4e2d&#xff0c;\u4f7f\u7528\u53cc\u4e0b\u5212\u7ebf (__) \u5f00\u5934\u7684\u5c5e\u6027\u6216\u65b9\u6cd5\u88ab\u8ba4\u4e3a\u662f\u79c1\u6709\u7684&#xff0c;\u4e0d\u80fd\u88ab\u7c7b\u5916\u90e8\u76f4\u63a5\u8bbf\u95ee\u3002\u8fd9\u662f\u901a\u8fc7\u540d\u79f0\u91cd\u6574&#xff08;name mangling&#xff09;\u5b9e\u73b0\u7684&#xff0c;Python \u4f1a\u5728\u5c5e\u6027\u540d\u524d\u52a0\u4e0a\u7c7b\u540d\u6765\u907f\u514d\u5916\u90e8\u8bbf\u95ee\u5b83\u4eec\u3002<\/p>\n<p>class MyClass:<br \/>\n    def __init__(self, name):<br \/>\n        self.__name &#061; name  # \u79c1\u6709\u5c5e\u6027<\/p>\n<p>    def __private_method(self):  # \u79c1\u6709\u65b9\u6cd5<br \/>\n        return f&#034;Hello, {self.__name}&#034;<\/p>\n<p>    def public_method(self):<br \/>\n        return self.__private_method()  # \u516c\u6709\u65b9\u6cd5\u8c03\u7528\u79c1\u6709\u65b9\u6cd5<\/p>\n<p># \u4f7f\u7528<br \/>\nobj &#061; MyClass(&#034;Alice&#034;)<br \/>\n# print(obj.__name)           # \u4f1a\u629b\u51fa AttributeError<br \/>\n# print(obj.__private_method())  # \u4f1a\u629b\u51fa AttributeError<br \/>\nprint(obj.public_method())     # \u53ef\u4ee5\u901a\u8fc7\u516c\u6709\u65b9\u6cd5\u95f4\u63a5\u8bbf\u95ee\u79c1\u6709\u65b9\u6cd5<\/p>\n<p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d&#xff0c;__name\u00a0\u5c5e\u6027\u548c\u00a0__private_method()\u00a0\u65b9\u6cd5\u662f\u79c1\u6709\u7684&#xff0c;\u5916\u90e8\u65e0\u6cd5\u76f4\u63a5\u8bbf\u95ee\u5b83\u4eec\u3002\u5982\u679c\u5c1d\u8bd5\u8bbf\u95ee&#xff0c;\u4f1a\u62a5\u00a0AttributeError\u00a0\u9519\u8bef\u3002\u4f46\u662f&#xff0c;\u53ef\u4ee5\u901a\u8fc7\u7c7b\u5185\u90e8\u7684\u516c\u6709\u65b9\u6cd5\u6765\u8bbf\u95ee\u79c1\u6709\u6210\u5458\u3002<\/p>\n<p>\u6ce8\u610f&#xff1a;\u867d\u7136\u53cc\u4e0b\u5212\u7ebf\u7684\u5c5e\u6027\u548c\u65b9\u6cd5\u662f&#034;\u79c1\u6709&#034;\u7684&#xff0c;\u4f46\u5b9e\u9645\u4e0a Python \u53ea\u662f\u5bf9\u5b83\u4eec\u7684\u540d\u79f0\u8fdb\u884c\u4e86\u91cd\u6574\u3002\u4f60\u53ef\u4ee5\u901a\u8fc7\u00a0_ClassName__attribute\u00a0\u7684\u65b9\u5f0f\u6765\u8bbf\u95ee\u5b83\u4eec&#xff0c;Python \u5e76\u6ca1\u6709\u5b8c\u5168\u7981\u6b62\u8bbf\u95ee\u3002\u8fd9\u79cd\u8bbe\u8ba1\u66f4\u591a\u7684\u662f\u4e00\u79cd&#034;\u7ea6\u5b9a&#034;\u800c\u4e0d\u662f\u5f3a\u5236\u7684\u9690\u85cf\u3002<\/p>\n<p># \u901a\u8fc7\u540d\u79f0\u91cd\u6574\u8bbf\u95ee\u79c1\u6709\u5c5e\u6027<br \/>\nprint(obj._MyClass__name)  # \u901a\u8fc7 name mangling \u8bbf\u95ee\u79c1\u6709\u5c5e\u6027<\/p>\n<h5>3. \u53d7\u4fdd\u62a4\u6210\u5458 (Protected Members)<\/h5>\n<p>\u5728 Python \u4e2d&#xff0c;\u4f7f\u7528\u5355\u4e0b\u5212\u7ebf (_) \u5f00\u5934\u7684\u5c5e\u6027\u6216\u65b9\u6cd5\u88ab\u8ba4\u4e3a\u662f\u53d7\u4fdd\u62a4\u7684&#xff0c;\u8fd9\u662f\u4e00\u4e2a\u5f31\u5c01\u88c5\u7684\u7ea6\u5b9a\u3002\u53d7\u4fdd\u62a4\u7684\u6210\u5458\u4e0d\u5efa\u8bae\u5728\u7c7b\u5916\u90e8\u76f4\u63a5\u8bbf\u95ee&#xff0c;\u4f46\u5e76\u6ca1\u6709\u5f3a\u5236\u9650\u5236&#xff0c;\u53ef\u4ee5\u901a\u8fc7\u5b50\u7c7b\u7ee7\u627f\u548c\u6269\u5c55\u65f6\u8bbf\u95ee\u3002<\/p>\n<p>class MyClass:<br \/>\n    def __init__(self, name):<br \/>\n        self._name &#061; name  # \u53d7\u4fdd\u62a4\u5c5e\u6027<\/p>\n<p>    def _protected_method(self):  # \u53d7\u4fdd\u62a4\u65b9\u6cd5<br \/>\n        return f&#034;Hello, {self._name}&#034;<\/p>\n<p># \u4f7f\u7528<br \/>\nobj &#061; MyClass(&#034;Alice&#034;)<br \/>\nprint(obj._name)            # \u53ef\u4ee5\u8bbf\u95ee\u53d7\u4fdd\u62a4\u5c5e\u6027&#xff0c;\u4f46\u4e0d\u5efa\u8bae<br \/>\nprint(obj._protected_method())  # \u53ef\u4ee5\u8bbf\u95ee\u53d7\u4fdd\u62a4\u65b9\u6cd5&#xff0c;\u4f46\u4e0d\u5efa\u8bae<\/p>\n<p>\u53d7\u4fdd\u62a4\u7684\u6210\u5458\u53ef\u4ee5\u5728\u7c7b\u5916\u90e8\u8bbf\u95ee&#xff0c;\u4f46\u4e00\u822c\u5728\u8bbe\u8ba1\u65f6&#xff0c;\u7ea6\u5b9a\u4e0d\u5e94\u8be5\u76f4\u63a5\u8bbf\u95ee\u8fd9\u4e9b\u6210\u5458&#xff0c;\u901a\u5e38\u7528\u4e8e\u7c7b\u5185\u90e8\u6216\u5b50\u7c7b\u4e2d\u3002<\/p>\n<h5>4. \u516c\u6709\u65b9\u6cd5\u4e0e\u79c1\u6709\u5c5e\u6027\u7684\u7ed3\u5408\u4f7f\u7528<\/h5>\n<p>\u4e00\u4e2a\u5e38\u89c1\u7684\u5c01\u88c5\u6a21\u5f0f\u662f\u5c06\u7c7b\u7684\u5c5e\u6027\u8bbe\u7f6e\u4e3a\u79c1\u6709&#xff0c;\u7136\u540e\u901a\u8fc7\u516c\u6709\u7684\u65b9\u6cd5&#xff08;\u901a\u5e38\u79f0\u4e3agetter\u548csetter\u65b9\u6cd5&#xff09;\u6765\u63a7\u5236\u5916\u754c\u5982\u4f55\u8bbf\u95ee\u6216\u4fee\u6539\u8fd9\u4e9b\u5c5e\u6027\u3002\u8fd9\u79cd\u65b9\u6cd5\u5141\u8bb8\u5bf9\u5c5e\u6027\u7684\u8bbf\u95ee\u8fdb\u884c\u66f4\u7cbe\u7ec6\u7684\u63a7\u5236&#xff0c;\u907f\u514d\u4e0d\u5f53\u7684\u64cd\u4f5c\u3002<\/p>\n<p>class MyClass:<br \/>\n    def __init__(self, name):<br \/>\n        self.__name &#061; name  # \u79c1\u6709\u5c5e\u6027<\/p>\n<p>    def get_name(self):  # getter \u65b9\u6cd5<br \/>\n        return self.__name<\/p>\n<p>    def set_name(self, new_name):  # setter \u65b9\u6cd5<br \/>\n        if isinstance(new_name, str):<br \/>\n            self.__name &#061; new_name<br \/>\n        else:<br \/>\n            raise ValueError(&#034;Name must be a string&#034;)<\/p>\n<p># \u4f7f\u7528<br \/>\nobj &#061; MyClass(&#034;Alice&#034;)<br \/>\nprint(obj.get_name())   # \u901a\u8fc7 getter \u8bbf\u95ee\u79c1\u6709\u5c5e\u6027<br \/>\nobj.set_name(&#034;Bob&#034;)     # \u901a\u8fc7 setter \u4fee\u6539\u79c1\u6709\u5c5e\u6027<br \/>\nprint(obj.get_name())<\/p>\n<p>\u901a\u8fc7\u8fd9\u79cd\u8bbe\u8ba1&#xff0c;\u7a0b\u5e8f\u5458\u53ef\u4ee5\u786e\u4fdd\u53ea\u6709\u7ecf\u8fc7\u9a8c\u8bc1\u7684\u6570\u636e\u624d\u80fd\u4fee\u6539\u5c5e\u6027\u3002\u6bd4\u5982\u5728\u00a0set_name\u00a0\u65b9\u6cd5\u4e2d&#xff0c;\u6211\u4eec\u68c0\u67e5\u8f93\u5165\u662f\u5426\u4e3a\u5b57\u7b26\u4e32&#xff0c;\u5982\u679c\u4e0d\u662f&#xff0c;\u5219\u629b\u51fa\u5f02\u5e38\u3002\u8fd9\u79cd\u65b9\u5f0f\u6709\u6548\u5730\u4fdd\u62a4\u4e86\u7c7b\u7684\u5185\u90e8\u72b6\u6001\u3002<\/p>\n<h5>5. \u5c5e\u6027\u88c5\u9970\u5668 (&#064;property) \u7684\u4f7f\u7528<\/h5>\n<p>Python \u63d0\u4f9b\u4e86\u00a0&#064;property\u00a0\u88c5\u9970\u5668\u6765\u7b80\u5316 getter \u548c setter \u65b9\u6cd5\u7684\u5b9a\u4e49&#xff0c;\u5141\u8bb8\u6211\u4eec\u50cf\u8bbf\u95ee\u666e\u901a\u5c5e\u6027\u4e00\u6837\u8c03\u7528\u65b9\u6cd5\u3002\u8fd9\u662f\u4e00\u79cd\u66f4 Pythonic \u7684\u5c01\u88c5\u65b9\u5f0f\u3002<\/p>\n<p>class MyClass:<br \/>\n    def __init__(self, name):<br \/>\n        self.__name &#061; name  # \u79c1\u6709\u5c5e\u6027<\/p>\n<p>    &#064;property<br \/>\n    def name(self):  # getter \u65b9\u6cd5<br \/>\n        return self.__name<\/p>\n<p>    &#064;name.setter<br \/>\n    def name(self, new_name):  # setter \u65b9\u6cd5<br \/>\n        if isinstance(new_name, str):<br \/>\n            self.__name &#061; new_name<br \/>\n        else:<br \/>\n            raise ValueError(&#034;Name must be a string&#034;)<\/p>\n<p># \u4f7f\u7528<br \/>\nobj &#061; MyClass(&#034;Alice&#034;)<br \/>\nprint(obj.name)   # \u901a\u8fc7\u5c5e\u6027\u8bbf\u95ee<br \/>\nobj.name &#061; &#034;Bob&#034;  # \u4fee\u6539\u5c5e\u6027<br \/>\nprint(obj.name)<\/p>\n<p>&#064;property\u00a0\u5141\u8bb8\u4f60\u5c06\u65b9\u6cd5\u5305\u88c5\u6210\u5c5e\u6027\u7684\u5f62\u5f0f&#xff0c;\u4ece\u800c\u4f7f\u7c7b\u7684\u4f7f\u7528\u66f4\u52a0\u76f4\u89c2&#xff0c;\u540c\u65f6\u4fdd\u6301\u4e86\u5c01\u88c5\u6027\u3002<\/p>\n<ul>\n<li>&#064;property&#xff1a;\u5c06\u65b9\u6cd5\u8f6c\u5316\u4e3a\u5c5e\u6027&#xff0c;\u7528\u4e8e\u8bfb\u53d6\u3002<\/li>\n<li>&#064;name.setter&#xff1a;\u4e3a\u5c5e\u6027\u5b9a\u4e49\u8d4b\u503c\u903b\u8f91&#xff0c;\u7528\u4e8e\u5199\u5165\u3002<\/li>\n<\/ul>\n<p>&#x1f195;\u00a05.1\u00a0&#064;property.deleter\u00a0\u4e0e\u53ea\u8bfb\u5c5e\u6027<\/p>\n<p>class Config:<br \/>\n    def __init__(self, db_url):<br \/>\n        self._db_url &#061; db_url<\/p>\n<p>    &#064;property<br \/>\n    def db_url(self):<br \/>\n        &#034;&#034;&#034;\u53ea\u8bfb\u5c5e\u6027&#xff1a;\u5916\u90e8\u53ef\u8bfb\u53d6\u4f46\u4e0d\u80fd\u76f4\u63a5\u4fee\u6539&#034;&#034;&#034;<br \/>\n        return self._db_url<\/p>\n<p>    &#064;db_url.deleter<br \/>\n    def db_url(self):<br \/>\n        &#034;&#034;&#034;\u5220\u9664\u5c5e\u6027\u65f6\u7684\u6e05\u7406\u903b\u8f91&#034;&#034;&#034;<br \/>\n        print(&#034;Cleaning up database connection&#8230;&#034;)<br \/>\n        del self._db_url<\/p>\n<p>config &#061; Config(&#034;postgresql:\/\/localhost:5432\/mydb&#034;)<br \/>\nprint(config.db_url)       # \u2705 \u53ef\u8bfb\u53d6<br \/>\n# config.db_url &#061; &#034;xxx&#034;    # \u274c \u6ca1\u6709 setter \u2192 AttributeError<br \/>\ndel config.db_url          # \u2705 \u89e6\u53d1 deleter<\/p>\n<p>&#x1f195;\u00a05.2 \u53ea\u8bfb\u8ba1\u7b97\u5c5e\u6027\u6a21\u5f0f<\/p>\n<p>import time<\/p>\n<p>class Circle:<br \/>\n    def __init__(self, radius):<br \/>\n        self.radius &#061; radius  # \u534a\u5f84\u662f\u53ef\u53d8\u7684<\/p>\n<p>    &#064;property<br \/>\n    def area(self):<br \/>\n        &#034;&#034;&#034;\u8ba1\u7b97\u5c5e\u6027&#xff1a;\u6bcf\u6b21\u8bfb\u53d6\u65f6\u5b9e\u65f6\u8ba1\u7b97&#xff0c;\u4e0d\u53ef\u8d4b\u503c&#034;&#034;&#034;<br \/>\n        return 3.14159 * self.radius ** 2<\/p>\n<p>    &#064;property<br \/>\n    def diameter(self):<br \/>\n        return self.radius * 2<\/p>\n<p>c &#061; Circle(5)<br \/>\nprint(c.area)        # 78.53975<br \/>\nc.radius &#061; 10<br \/>\nprint(c.area)        # 314.159&#xff08;\u81ea\u52a8\u66f4\u65b0&#xff09;<br \/>\n# c.area &#061; 100       # \u274c \u53ea\u8bfb\u5c5e\u6027\u65e0\u6cd5\u8d4b\u503c<\/p>\n<h5>&#x1f195; 6. \u63cf\u8ff0\u7b26\u534f\u8bae (Descriptor Protocol) \u2014\u2014 &#064;property \u7684\u5e95\u5c42\u539f\u7406<\/h5>\n<p>&#064;property\u00a0\u672c\u8d28\u4e0a\u662f\u4e00\u4e2a\u6570\u636e\u63cf\u8ff0\u7b26 (Data Descriptor)\u3002\u7406\u89e3\u63cf\u8ff0\u7b26\u534f\u8bae\u662f\u638c\u63e1 Python \u5c01\u88c5\u9ad8\u7ea7\u7528\u6cd5\u7684\u5173\u952e\u3002<\/p>\n<p>\u63cf\u8ff0\u7b26\u534f\u8bae&#xff1a;\u4e00\u4e2a\u7c7b\u5982\u679c\u5b9e\u73b0\u4e86\u4ee5\u4e0b\u4efb\u610f\u65b9\u6cd5&#xff0c;\u5c31\u662f\u4e00\u4e2a\u63cf\u8ff0\u7b26&#xff1a;<\/p>\n<ul>\n<li>__get__(self, obj, objtype&#061;None)\u00a0\u2192 \u7528\u4e8e\u5c5e\u6027\u8bbf\u95ee<\/li>\n<li>__set__(self, obj, value)\u00a0\u2192 \u7528\u4e8e\u5c5e\u6027\u8d4b\u503c<\/li>\n<li>__delete__(self, obj)\u00a0\u2192 \u7528\u4e8e\u5c5e\u6027\u5220\u9664 (del)<\/li>\n<\/ul>\n<p># \u624b\u5199\u4e00\u4e2a\u975e\u8d1f\u6570\u6821\u9a8c\u63cf\u8ff0\u7b26&#xff08;\u7b49\u4ef7\u4e8e\u5e26\u6821\u9a8c\u7684 property&#xff09;<br \/>\nclass NonNegative:<br \/>\n    &#034;&#034;&#034;\u786e\u4fdd\u5c5e\u6027\u7684\u503c\u6c38\u8fdc &gt;&#061; 0&#034;&#034;&#034;<br \/>\n    def __init__(self, default&#061;0):<br \/>\n        self.default &#061; default<br \/>\n        self.private_name &#061; None  # \u5c06\u5728 __set_name__ \u4e2d\u8bbe\u7f6e<\/p>\n<p>    def __set_name__(self, owner, name):<br \/>\n        # Python 3.6&#043; \u81ea\u52a8\u8c03\u7528&#xff1a;\u544a\u8bc9\u6211\u4eec\u88ab\u8d4b\u7ed9\u4e86\u54ea\u4e2a\u5c5e\u6027\u540d<br \/>\n        self.private_name &#061; f&#039;_{name}&#039;<\/p>\n<p>    def __get__(self, obj, objtype&#061;None):<br \/>\n        if obj is None:<br \/>\n            return self<br \/>\n        return getattr(obj, self.private_name, self.default)<\/p>\n<p>    def __set__(self, obj, value):<br \/>\n        if value &lt; 0:<br \/>\n            raise ValueError(f&#034;{self.private_name} must be &gt;&#061; 0, got {value}&#034;)<br \/>\n        setattr(obj, self.private_name, value)<\/p>\n<p>class Product:<br \/>\n    price &#061; NonNegative(0)      # \u4f7f\u7528\u63cf\u8ff0\u7b26<br \/>\n    stock &#061; NonNegative(0)      # \u590d\u7528\u540c\u4e00\u4e2a\u63cf\u8ff0\u7b26\u7c7b<\/p>\n<p>    def __init__(self, price, stock):<br \/>\n        self.price &#061; price      # \u89e6\u53d1 NonNegative.__set__<br \/>\n        self.stock &#061; stock<\/p>\n<p>p &#061; Product(99.9, 50)<br \/>\nprint(p.price)                 # 99.9 \u2192 \u89e6\u53d1 NonNegative.__get__<br \/>\n# p.price &#061; -1                # \u274c ValueError: _price must be &gt;&#061; 0<\/p>\n<p>\u5173\u952e\u6d1e\u5bdf&#xff1a;<\/p>\n<table>\n<tr>\u7279\u6027&#064;property\u81ea\u5b9a\u4e49\u63cf\u8ff0\u7b26<\/tr>\n<tbody>\n<tr>\n<td>\u590d\u7528\u6027<\/td>\n<td>\u6bcf\u4e2a\u5c5e\u6027\u5355\u72ec\u5b9a\u4e49<\/td>\n<td>\u4e00\u4e2a\u7c7b\u53ef\u7528\u4e8e\u591a\u4e2a\u5c5e\u6027<\/td>\n<\/tr>\n<tr>\n<td>\u9002\u7528\u573a\u666f<\/td>\n<td>\u5355\u4e00\u5c5e\u6027\u7684\u7279\u6b8a\u903b\u8f91<\/td>\n<td>\u8de8\u591a\u4e2a\u5c5e\u6027\u7684\u901a\u7528\u6821\u9a8c\/\u8f6c\u6362\u903b\u8f91<\/td>\n<\/tr>\n<tr>\n<td>\u5e95\u5c42\u5173\u7cfb<\/td>\n<td>&#064;property\u00a0\u5c31\u662f\u4e00\u4e2a\u63cf\u8ff0\u7b26\u5de5\u5382<\/td>\n<td>\u63cf\u8ff0\u7b26\u662f\u66f4\u5e95\u5c42\u7684\u539f\u8bed<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>PyTorch \u7684\u00a0nn.Parameter\u3001Django ORM \u7684\u00a0Field\u00a0\u90fd\u662f\u63cf\u8ff0\u7b26\u534f\u8bae\u7684\u5b9e\u9645\u5e94\u7528\u3002<\/p>\n<h5 style=\"background-color:transparent\">&#x1f195; 7.\u00a0__slots__\u00a0\u2014\u2014 \u5185\u5b58\u4f18\u5316\u4e0e\u5c5e\u6027\u5c01\u7981<\/h5>\n<p>__slots__\u00a0\u662f Python \u63d0\u4f9b\u7684\u4e00\u4e2a\u7c7b\u7ea7\u522b\u4f18\u5316\u9009\u9879&#xff0c;\u540c\u65f6\u5177\u6709\u5c01\u88c5\u589e\u5f3a\u7684\u4f5c\u7528\u3002<\/p>\n<p>class WithoutSlots:<br \/>\n    def __init__(self, x, y):<br \/>\n        self.x &#061; x<br \/>\n        self.y &#061; y<\/p>\n<p>class WithSlots:<br \/>\n    __slots__ &#061; (&#039;x&#039;, &#039;y&#039;)   # \u58f0\u660e\u5141\u8bb8\u7684\u5c5e\u6027\u540d<\/p>\n<p>    def __init__(self, x, y):<br \/>\n        self.x &#061; x<br \/>\n        self.y &#061; y<\/p>\n<p># &#8212;- \u529f\u80fd\u5dee\u5f02 &#8212;-<br \/>\na &#061; WithoutSlots(1, 2)<br \/>\na.z &#061; 999              # \u2705 \u53ef\u4ee5\u968f\u610f\u6dfb\u52a0\u65b0\u5c5e\u6027<\/p>\n<p>b &#061; WithSlots(1, 2)<br \/>\nb.z &#061; 999              # \u274c AttributeError: &#039;WithSlots&#039; object has no attribute &#039;z&#039;<\/p>\n<p># &#8212;- \u5185\u5b58\u5dee\u5f02 &#8212;-<br \/>\nimport sys<br \/>\nprint(sys.getsizeof(WithoutSlots(1, 2)))  # \u901a\u5e38 56 bytes (&#043; __dict__)<br \/>\nprint(sys.getsizeof(WithSlots(1, 2)))     # \u901a\u5e38 32 bytes (\u65e0 __dict__)<\/p>\n<p>__slots__\u00a0\u7684\u5c01\u88c5\u610f\u4e49&#xff1a;<\/p>\n<li>\u9632\u6b62\u62fc\u5199\u9519\u8bef&#xff1a;obj.nmae &#061; 5\u00a0\u4e0d\u4f1a\u9759\u9ed8\u521b\u5efa\u9519\u8bef\u5c5e\u6027&#xff0c;\u800c\u662f\u7acb\u5373\u62a5\u9519<\/li>\n<li>\u8282\u7701\u5185\u5b58&#xff1a;\u7701\u53bb\u00a0__dict__\u00a0\u548c\u00a0__weakref__&#xff0c;\u9002\u5408\u521b\u5efa\u5927\u91cf\u5c0f\u5bf9\u8c61\u7684\u573a\u666f<\/li>\n<li>\u52a0\u901f\u5c5e\u6027\u8bbf\u95ee&#xff1a; slots \u4f7f\u7528 descriptor \u800c\u975e dict \u67e5\u627e&#xff0c;\u7565\u5feb<\/li>\n<p>\u26a0\ufe0f\u00a0\u7ee7\u627f\u6ce8\u610f\u4e8b\u9879&#xff1a;<\/p>\n<ul>\n<li>\u5b50\u7c7b\u4e0d\u4f1a\u81ea\u52a8\u7ee7\u627f\u7236\u7c7b\u7684\u00a0__slots__&#xff0c;\u9664\u975e\u5b50\u7c7b\u4e5f\u5b9a\u4e49\u4e86\u00a0__slots__<\/li>\n<li>\u5982\u679c\u7236\u7c7b\u6ca1\u6709\u5b9a\u4e49\u00a0__slots__&#xff0c;\u5b50\u7c7b\u5b9a\u4e49\u4e86\u00a0__slots__\u00a0\u5219\u65e0\u6548&#xff08;\u56e0\u4e3a\u00a0__dict__\u00a0\u5df2\u5b58\u5728&#xff09;<\/li>\n<li>\u6b63\u786e\u505a\u6cd5&#xff1a;\u7236\u7c7b\u548c\u5b50\u7c7b\u90fd\u5b9a\u4e49\u00a0__slots__<\/li>\n<\/ul>\n<p>class Base:<br \/>\n    __slots__ &#061; (&#039;x&#039;,)<\/p>\n<p>class Derived(Base):<br \/>\n    __slots__ &#061; (&#039;y&#039;,)   # \u5b50\u7c7b\u989d\u5916\u58f0\u660e\u7684 slot<\/p>\n<p>d &#061; Derived()<br \/>\nd.x &#061; 1   # \u2705 \u6765\u81ea Base<br \/>\nd.y &#061; 2   # \u2705 \u6765\u81ea Derived<br \/>\n# d.z &#061; 3 # \u274c AttributeError<\/p>\n<h5>&#x1f195; 8. \u6a21\u5757\u7ea7\u5c01\u88c5<\/h5>\n<p>Python \u7684\u5c01\u88c5\u4e0d\u6b62\u4e8e\u7c7b\u5c42\u9762&#xff0c;\u6a21\u5757&#xff08;module&#xff09;\u00a0\u4e5f\u6709\u81ea\u5df1\u7684\u5c01\u88c5\u673a\u5236&#xff1a;<\/p>\n<p># &#061;&#061;&#061;&#061;&#061; my_module.py &#061;&#061;&#061;&#061;&#061;<\/p>\n<p>__all__ &#061; [&#039;public_func&#039;, &#039;PUBLIC_CONST&#039;]  # \u663e\u5f0f\u5bfc\u51fa API<\/p>\n<p>PUBLIC_CONST &#061; 42<\/p>\n<p>def public_func():<br \/>\n    return &#034;I&#039;m part of the public API&#034;<\/p>\n<p>def _internal_helper():<br \/>\n    &#034;&#034;&#034;\u5355\u4e0b\u5212\u7ebf&#xff1a;\u7ea6\u5b9a\u4e3a\u5185\u90e8\u5b9e\u73b0&#xff0c;from X import * \u4e0d\u4f1a\u88ab\u5bfc\u5165&#034;&#034;&#034;<br \/>\n    return &#034;Implementation detail&#034;<\/p>\n<p>__secret_var &#061; &#034;double underscore: \u66f4\u5f3a\u7684\u79c1\u6709\u6697\u793a&#034;<\/p>\n<p># &#061;&#061;&#061;&#061;&#061; \u4f7f\u7528\u65b9 &#061;&#061;&#061;&#061;&#061;<br \/>\nfrom my_module import *<br \/>\nprint(public_func())     # \u2705 \u5728 __all__ \u4e2d<br \/>\n# print(_internal_helper())  # \u274c import * \u4e0d\u4f1a\u5bfc\u5165<br \/>\n# print(__secret_var)       # \u274c \u540d\u79f0\u5df2\u88ab\u91cd\u6574\u4e3a _my_module__secret_var<\/p>\n<table>\n<tr>\u673a\u5236\u4f5c\u7528\u57df\u5f3a\u5236\u7a0b\u5ea6<\/tr>\n<tbody>\n<tr>\n<td>_name\u00a0(\u5355\u4e0b\u5212\u7ebf)<\/td>\n<td>from X import *\u00a0\u4e0d\u5bfc\u5165<\/td>\n<td>\u5f31\u7ea6\u5b9a&#xff08;\u4ecd\u53ef\u76f4\u63a5 import&#xff09;<\/td>\n<\/tr>\n<tr>\n<td>__name\u00a0(\u53cc\u4e0b\u5212\u7ebf)<\/td>\n<td>\u540d\u79f0\u91cd\u6574\u4e3a\u00a0_mod__name<\/td>\n<td>\u4e2d\u7b49&#xff08;\u9700\u77e5\u9053\u91cd\u6574\u89c4\u5219\u624d\u80fd\u8bbf\u95ee&#xff09;<\/td>\n<\/tr>\n<tr>\n<td>__all__<\/td>\n<td>\u63a7\u5236\u00a0from X import *\u00a0\u7684\u884c\u4e3a<\/td>\n<td>\u5f31&#xff08;\u4e0d\u5f71\u54cd\u663e\u5f0f import&#xff09;<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h5>&#x1f195; 9. dataclass \u4e0e\u5c01\u88c5\u7684\u6700\u4f73\u5b9e\u8df5<\/h5>\n<p>Python 3.7&#043; \u7684\u00a0dataclass\u00a0\u7ed3\u5408\u00a0field()\u00a0\u53ef\u4ee5\u5199\u51fa\u7b80\u6d01\u4e14\u5c01\u88c5\u826f\u597d\u7684\u4ee3\u7801&#xff1a;<\/p>\n<p>from dataclasses import dataclass, field<\/p>\n<p>&#064;dataclass<br \/>\nclass ModelConfig:<br \/>\n    &#034;&#034;&#034;AI \u6a21\u578b\u914d\u7f6e \u2014\u2014 dataclass \u5c01\u88c5\u793a\u4f8b&#034;&#034;&#034;<br \/>\n    model_name: str<br \/>\n    learning_rate: float &#061; 1e-3<br \/>\n    batch_size: int &#061; 32<br \/>\n    _internal_cache: dict &#061; field(default_factory&#061;dict, repr&#061;False, init&#061;False)<\/p>\n<p>    def __post_init__(self):<br \/>\n        &#034;&#034;&#034;\u6784\u9020\u540e\u7684\u6821\u9a8c\u903b\u8f91&#034;&#034;&#034;<br \/>\n        if self.learning_rate &lt;&#061; 0:<br \/>\n            raise ValueError(&#034;learning_rate must be positive&#034;)<br \/>\n        if not self.model_name:<br \/>\n            raise ValueError(&#034;model_name cannot be empty&#034;)<\/p>\n<p>    &#064;property<br \/>\n    def effective_steps_per_epoch(self):<br \/>\n        &#034;&#034;&#034;\u8ba1\u7b97\u5c5e\u6027&#xff1a;\u6839\u636e batch_size \u52a8\u6001\u8ba1\u7b97&#034;&#034;&#034;<br \/>\n        return 100000 \/\/ self.batch_size  # \u5047\u8bbe\u8bad\u7ec3\u96c6 100k \u6837\u672c<\/p>\n<p>config &#061; ModelConfig(&#034;gpt-2&#034;, learning_rate&#061;5e-4)<br \/>\nprint(config)                    # ModelConfig(model_name&#061;&#039;gpt-2&#039;, learning_rate&#061;0.0005, &#8230;)<br \/>\nprint(config.effective_steps_per_epoch)  # 3125<br \/>\n# ModelConfig(&#034;&#034;, 1e-3)         # \u274c ValueError<\/p>\n<p>dataclass \u5c01\u88c5\u8981\u70b9&#xff1a;<\/p>\n<ul>\n<li>init&#061;False&#xff1a;\u4e0d\u5728\u00a0__init__\u00a0\u4e2d\u66b4\u9732&#xff08;\u7c7b\u4f3c\u79c1\u6709\u5c5e\u6027&#xff09;<\/li>\n<li>repr&#061;False&#xff1a;\u4e0d\u5728\u00a0__repr__\u00a0\u4e2d\u663e\u793a&#xff08;\u9690\u85cf\u5185\u90e8\u7ec6\u8282&#xff09;<\/li>\n<li>__post_init__&#xff1a;\u66ff\u4ee3 setter \u7684\u6821\u9a8c\u903b\u8f91<\/li>\n<li>frozen&#061;True&#xff1a;\u521b\u5efa\u4e0d\u53ef\u53d8\u5bf9\u8c61&#xff08;\u6700\u5f3a\u5c01\u88c5&#xff09;<\/li>\n<\/ul>\n<h5>&#x1f195; 10. \u53cd\u6a21\u5f0f&#xff1a;\u8fc7\u5ea6\u5c01\u88c5 (Over-Encapsulation)<\/h5>\n<p>Python \u54f2\u5b66&#xff1a;\u201cWe are all consenting adults here.\u201d \u2014\u2014 \u6211\u4eec\u90fd\u662f\u6210\u5e74\u4eba&#xff0c;\u4e0d\u9700\u8981\u4fdd\u59c6\u5f0f\u7684\u4fdd\u62a4\u3002<\/p>\n<p># \u274c \u53cd\u6a21\u5f0f&#xff1a;Java \u98ce\u683c\u7684\u8fc7\u5ea6\u5c01\u88c5<br \/>\nclass BadEncapsulation:<br \/>\n    def __init__(self, value):<br \/>\n        self.set_value(value)<\/p>\n<p>    def get_value(self):<br \/>\n        return self._value<\/p>\n<p>    def set_value(self, value):<br \/>\n        self._value &#061; value<\/p>\n<p># \u2705 Pythonic \u5199\u6cd5&#xff1a;\u7b80\u5355\u573a\u666f\u76f4\u63a5\u7528\u516c\u6709\u5c5e\u6027<br \/>\nclass GoodEncapsulation:<br \/>\n    def __init__(self, value):<br \/>\n        self.value &#061; value  # \u5148\u4fdd\u6301\u7b80\u5355<\/p>\n<p># \u53ea\u6709\u5f53\u9700\u8981\u5728\u8d4b\u503c\u65f6\u6dfb\u52a0\u6821\u9a8c\/\u526f\u4f5c\u7528\u65f6\u624d\u5347\u7ea7\u4e3a property<br \/>\nclass WhenNeeded:<br \/>\n    def __init__(self, value):<br \/>\n        self._value &#061; value<\/p>\n<p>    &#064;property<br \/>\n    def value(self):<br \/>\n        return self._value<\/p>\n<p>    &#064;value.setter<br \/>\n    def value(self, val):<br \/>\n        if val &lt; 0:<br \/>\n            raise ValueError(&#034;must be non-negative&#034;)<br \/>\n        self._value &#061; val<\/p>\n<p>\u5224\u65ad\u662f\u5426\u9700\u8981\u5c01\u88c5\u7684\u81ea\u68c0\u6e05\u5355&#xff1a;<\/p>\n<li>\u8d4b\u503c\u65f6\u9700\u8981\u6821\u9a8c\u5417&#xff1f;\u2192 \u9700\u8981 \u2192 \u7528\u00a0&#064;property<\/li>\n<li>\u53d6\u503c\u65f6\u9700\u8981\u8ba1\u7b97\/\u7f13\u5b58\u5417&#xff1f;\u2192 \u9700\u8981 \u2192 \u7528\u00a0&#064;property<\/li>\n<li>\u9700\u8981\u63a7\u5236\u5916\u90e8\u80fd\u5426\u5220\u9664\u8be5\u5c5e\u6027\u5417&#xff1f;\u2192 \u9700\u8981 \u2192 \u52a0\u00a0&#064;x.deleter<\/li>\n<li>\u4ee5\u4e0a\u90fd\u4e0d\u9700\u8981&#xff1f;\u2192 \u76f4\u63a5\u7528\u516c\u6709\u5c5e\u6027&#xff0c;\u4fdd\u6301\u7b80\u5355<\/li>\n<h5>&#x1f195; 11. AI\/ML \u6846\u67b6\u5b9e\u6218&#xff1a;PyTorch nn.Module \u7684\u5c01\u88c5\u8bbe\u8ba1<\/h5>\n<p>import torch<br \/>\nimport torch.nn as nn<\/p>\n<p>class SimpleTransformerBlock(nn.Module):<br \/>\n    &#034;&#034;&#034;\u5c55\u793a PyTorch \u4e2d\u5982\u4f55\u8fd0\u7528\u5c01\u88c5\u601d\u60f3&#034;&#034;&#034;<\/p>\n<p>    def __init__(self, d_model: int, n_heads: int, dropout: float &#061; 0.1):<br \/>\n        super().__init__()<br \/>\n        # \u79c1\u6709\u53c2\u6570&#xff1a;\u4e0d\u5e0c\u671b\u5916\u90e8\u76f4\u63a5\u4fee\u6539<br \/>\n        self._d_model &#061; d_model<br \/>\n        self._n_heads &#061; n_heads<\/p>\n<p>        # \u516c\u6709\u5b50\u6a21\u5757&#xff1a;PyTorch \u7ea6\u5b9a\u7528 .layer_name \u66b4\u9732<br \/>\n        self.attention &#061; nn.MultiheadAttention(d_model, n_heads, dropout&#061;dropout)<br \/>\n        self.norm1 &#061; nn.LayerNorm(d_model)<br \/>\n        self.ffn &#061; nn.Sequential(<br \/>\n            nn.Linear(d_model, 4 * d_model),<br \/>\n            nn.GELU(),<br \/>\n            nn.Linear(4 * d_model, d_model),<br \/>\n        )<br \/>\n        self.norm2 &#061; nn.LayerNorm(d_model)<br \/>\n        self.dropout &#061; nn.Dropout(dropout)<\/p>\n<p>    &#064;property<br \/>\n    def head_dim(self) -&gt; int:<br \/>\n        &#034;&#034;&#034;\u8ba1\u7b97\u5c5e\u6027&#xff1a;\u6bcf\u4e2a\u6ce8\u610f\u529b\u5934\u7684\u7ef4\u5ea6&#034;&#034;&#034;<br \/>\n        return self._d_model \/\/ self._n_heads<\/p>\n<p>    def forward(self, x, mask&#061;None):<br \/>\n        # \u5185\u90e8\u5b9e\u73b0\u7ec6\u8282\u88ab\u5c01\u88c5<br \/>\n        attn_out, _ &#061; self.attention(x, x, x, attn_mask&#061;mask)<br \/>\n        x &#061; self.norm1(self.dropout(attn_out) &#043; x)<br \/>\n        ffn_out &#061; self.ffn(x)<br \/>\n        x &#061; self.norm2(self.dropout(ffn_out) &#043; x)<br \/>\n        return x<\/p>\n<p>block &#061; SimpleTransformerBlock(d_model&#061;512, n_heads&#061;8)<br \/>\nprint(block.head_dim)  # 64 \u2014 \u901a\u8fc7 property \u5b89\u5168\u8bbf\u95ee\u8ba1\u7b97\u5c5e\u6027<br \/>\n# block._d_model &#061; 128  # \u26a0\ufe0f \u6280\u672f\u4e0a\u53ef\u4ee5&#xff0c;\u4f46\u8fdd\u53cd\u5c01\u88c5\u7ea6\u5b9a<\/p>\n<p>PyTorch \u5c01\u88c5\u60ef\u4f8b\u603b\u7ed3&#xff1a;<\/p>\n<ul>\n<li>\u6784\u9020\u53c2\u6570 \u2192 \u5b58\u50a8\u4e3a\u79c1\u6709 (_d_model)<\/li>\n<li>\u5b50\u6a21\u5757 \u2192 \u516c\u6709\u5c5e\u6027 (self.attention)&#xff0c;\u4fbf\u4e8e\u5916\u90e8\u8bbf\u95ee\u548c\u68c0\u67e5<\/li>\n<li>\u6d3e\u751f\u91cf \u2192\u00a0&#064;property\u00a0\u8ba1\u7b97 (head_dim)<\/li>\n<li>\u6838\u5fc3\u903b\u8f91 \u2192\u00a0forward()\u00a0\u65b9\u6cd5&#xff0c;\u5916\u90e8\u53ea\u9700\u8c03\u7528\u5373\u53ef<\/li>\n<\/ul>\n<h4>\u5c01\u88c5\u7684\u4f18\u52bf<\/h4>\n<li>\n<p>\u63d0\u9ad8\u4ee3\u7801\u7684\u5b89\u5168\u6027&#xff1a;<\/p>\n<ul>\n<li>\u5c01\u88c5\u9690\u85cf\u4e86\u7c7b\u7684\u5185\u90e8\u7ec6\u8282&#xff0c;\u9632\u6b62\u5916\u90e8\u5bf9\u5185\u90e8\u5c5e\u6027\u8fdb\u884c\u975e\u6cd5\u64cd\u4f5c&#xff0c;\u51cf\u5c11\u4e86\u6570\u636e\u4e0d\u4e00\u81f4\u6216\u65e0\u6548\u6570\u636e\u7684\u98ce\u9669\u3002<\/li>\n<\/ul>\n<\/li>\n<li>\n<p>\u63d0\u9ad8\u4ee3\u7801\u7684\u7075\u6d3b\u6027&#xff1a;<\/p>\n<ul>\n<li>\u901a\u8fc7\u5c01\u88c5&#xff0c;\u53ef\u4ee5\u7075\u6d3b\u5730\u4fee\u6539\u7c7b\u7684\u5185\u90e8\u5b9e\u73b0&#xff0c;\u800c\u65e0\u9700\u4fee\u6539\u7c7b\u7684\u5916\u90e8\u4f7f\u7528\u4ee3\u7801\u3002\u8fd9\u79cd\u8bbe\u8ba1\u5141\u8bb8\u7c7b\u7684\u5b9e\u73b0\u7ec6\u8282\u53d1\u751f\u53d8\u5316\u800c\u4e0d\u5f71\u54cd\u5176\u63a5\u53e3&#xff0c;\u5177\u6709\u8f83\u9ad8\u7684\u6269\u5c55\u6027\u3002<\/li>\n<\/ul>\n<\/li>\n<li>\n<p>\u66f4\u597d\u7684\u4ee3\u7801\u7ef4\u62a4\u6027&#xff1a;<\/p>\n<ul>\n<li>\u5c01\u88c5\u4f7f\u5f97\u4ee3\u7801\u66f4\u52a0\u6a21\u5757\u5316&#xff0c;\u6bcf\u4e2a\u7c7b\u6216\u6a21\u5757\u53ea\u66b4\u9732\u5fc5\u8981\u7684\u63a5\u53e3&#xff0c;\u51cf\u5c11\u4e86\u8026\u5408\u6027&#xff0c;\u589e\u5f3a\u4e86\u4ee3\u7801\u7684\u53ef\u7ef4\u62a4\u6027\u3002<\/li>\n<\/ul>\n<\/li>\n<li>\n<p>\u63a7\u5236\u5c5e\u6027\u8bbf\u95ee&#xff1a;<\/p>\n<ul>\n<li>\u901a\u8fc7 getter \u548c setter \u65b9\u6cd5&#xff0c;\u53ef\u4ee5\u63a7\u5236\u5bf9\u5c5e\u6027\u7684\u8bbf\u95ee\u548c\u4fee\u6539\u64cd\u4f5c&#xff0c;\u786e\u4fdd\u7c7b\u7684\u5185\u90e8\u72b6\u6001\u59cb\u7ec8\u6709\u6548\u3002<\/li>\n<\/ul>\n<\/li>\n<p>&#x1f195;\u00a05. \u5185\u5b58\u6548\u7387\u63d0\u5347&#xff1a;__slots__\u00a0\u53ef\u663e\u8457\u964d\u4f4e\u5927\u91cf\u5c0f\u5bf9\u8c61\u7684\u5185\u5b58\u5360\u7528\u3002<\/p>\n<p>&#x1f195;\u00a06. \u63a5\u53e3\u7a33\u5b9a\u6027&#xff1a;\u6a21\u5757\u7ea7\u5c01\u88c5&#xff08;__all__&#xff09;\u4fdd\u8bc1\u516c\u5171 API \u7684\u5411\u540e\u517c\u5bb9\u3002<\/p>\n<h4>\u5c01\u88c5\u4e0e\u5176\u4ed6 OOP \u6982\u5ff5\u7684\u5173\u7cfb<\/h4>\n<ul>\n<li>\u5c01\u88c5\u4e0e\u7ee7\u627f&#xff1a;\u5c01\u88c5\u53ef\u4ee5\u7ed3\u5408\u7ee7\u627f\u4e00\u8d77\u4f7f\u7528&#xff0c;\u901a\u8fc7\u5b50\u7c7b\u7ee7\u627f\u7236\u7c7b\u7684\u516c\u6709\u65b9\u6cd5\u548c\u53d7\u4fdd\u62a4\u7684\u5c5e\u6027&#xff0c;\u5c01\u88c5\u6027\u4f9d\u7136\u5f97\u4ee5\u4fdd\u6301\u3002<\/li>\n<li>\u5c01\u88c5\u4e0e\u591a\u6001&#xff1a;\u5c01\u88c5\u548c\u591a\u6001\u76f8\u8f85\u76f8\u6210&#xff0c;\u5c01\u88c5\u5141\u8bb8\u5c06\u5b9e\u73b0\u9690\u85cf&#xff0c;\u800c\u591a\u6001\u5141\u8bb8\u5bf9\u8c61\u5728\u8fd0\u884c\u65f6\u51b3\u5b9a\u5177\u4f53\u8c03\u7528\u7684\u5b9e\u73b0&#xff0c;\u4f7f\u5f97\u4ee3\u7801\u7684\u6269\u5c55\u6027\u66f4\u5f3a\u3002<\/li>\n<li>&#x1f195;\u00a0\u5c01\u88c5\u4e0e\u7ec4\u5408&#xff1a;\u826f\u597d\u7684\u5c01\u88c5\u4f7f\u5f97\u5bf9\u8c61\u53ef\u4ee5\u4f5c\u4e3a&#034;\u9ed1\u76d2\u5b50&#034;\u88ab\u7ec4\u5408\u5230\u66f4\u5927\u7684\u7cfb\u7edf\u4e2d&#xff0c;\u662f\u7ec4\u5408\u4f18\u4e8e\u7ee7\u627f\u7684\u57fa\u7840\u3002<\/li>\n<\/ul>\n<hr \/>\n<h3 id=\"18.\u4ecb\u7ecd\u4e00\u4e0bPython\u4e2d\u7684\u7ee7\u627f\uff08Inheritance\uff09\u601d\u60f3\" style=\"background-color:transparent\">\u4ecb\u7ecd\u4e00\u4e0bPython\u4e2d\u7684\u7ee7\u627f&#xff08;Inheritance&#xff09;\u601d\u60f3<\/h3>\n<p>\u7ee7\u627f\u662f\u9762\u5411\u5bf9\u8c61\u7f16\u7a0b&#xff08;OOP&#xff09;\u7684\u4e00\u4e2a\u6838\u5fc3\u6982\u5ff5&#xff0c;\u5b83\u5141\u8bb8\u4e00\u4e2a\u7c7b&#xff08;\u79f0\u4e3a\u5b50\u7c7b\u6216\u6d3e\u751f\u7c7b&#xff09;\u4ece\u53e6\u4e00\u4e2a\u7c7b&#xff08;\u79f0\u4e3a\u7236\u7c7b\u6216\u57fa\u7c7b&#xff09;\u7ee7\u627f\u5c5e\u6027\u548c\u65b9\u6cd5\u3002\u5b50\u7c7b\u53ef\u4ee5\u7ee7\u627f\u7236\u7c7b\u7684\u7279\u6027&#xff0c;\u5e76\u4e14\u53ef\u4ee5\u5728\u6b64\u57fa\u7840\u4e0a\u6dfb\u52a0\u81ea\u5df1\u7684\u65b0\u7279\u6027&#xff0c;\u4ece\u800c\u5b9e\u73b0\u4ee3\u7801\u7684\u91cd\u7528\u548c\u6269\u5c55\u3002Python \u4f5c\u4e3a\u4e00\u95e8\u652f\u6301\u9762\u5411\u5bf9\u8c61\u7f16\u7a0b\u7684\u8bed\u8a00&#xff0c;\u63d0\u4f9b\u4e86\u5f3a\u5927\u7684\u7ee7\u627f\u673a\u5236\u3002<\/p>\n<p>Python\u4e2d\u7ee7\u627f\u7684\u4f18\u52bf&#xff1a;<\/p>\n<li>\u4ee3\u7801\u91cd\u7528&#xff1a;\u5b50\u7c7b\u53ef\u4ee5\u76f4\u63a5\u4f7f\u7528\u7236\u7c7b\u5df2\u7ecf\u5b9a\u4e49\u7684\u65b9\u6cd5\u548c\u5c5e\u6027&#xff0c;\u907f\u514d\u4e86\u91cd\u590d\u7f16\u5199\u76f8\u540c\u7684\u4ee3\u7801\u7247\u6bb5\u3002<\/li>\n<li>\u53ef\u6269\u5c55\u6027&#xff1a;\u5b50\u7c7b\u53ef\u4ee5\u5728\u4e0d\u4fee\u6539\u7236\u7c7b\u7684\u60c5\u51b5\u4e0b&#xff0c;\u6dfb\u52a0\u65b0\u7684\u5c5e\u6027\u548c\u65b9\u6cd5&#xff0c;\u4ece\u800c\u4f7f\u5f97\u4ee3\u7801\u66f4\u5177\u53ef\u6269\u5c55\u6027\u3002\u8fd9\u6837\u53ef\u4ee5\u5728\u4e0d\u5f71\u54cd\u7236\u7c7b\u7684\u57fa\u7840\u4e0a&#xff0c;\u4e3a\u7a0b\u5e8f\u6dfb\u52a0\u65b0\u7684\u529f\u80fd\u3002<\/li>\n<h4>\u4e00\u3001\u7ee7\u627f\u7684\u57fa\u672c\u6982\u5ff5<\/h4>\n<h5>1. \u7236\u7c7b&#xff08;\u57fa\u7c7b&#xff09;<\/h5>\n<ul>\n<li>\u5b9a\u4e49&#xff1a;\u88ab\u7ee7\u627f\u7684\u7c7b&#xff0c;\u63d0\u4f9b\u57fa\u672c\u7684\u5c5e\u6027\u548c\u65b9\u6cd5\u3002<\/li>\n<li>\u4f5c\u7528&#xff1a;\u4f5c\u4e3a\u5b50\u7c7b\u7684\u6a21\u677f&#xff0c;\u5b50\u7c7b\u53ef\u4ee5\u7ee7\u627f\u7236\u7c7b\u7684\u5c5e\u6027\u548c\u65b9\u6cd5\u3002<\/li>\n<\/ul>\n<h5>2. \u5b50\u7c7b&#xff08;\u6d3e\u751f\u7c7b&#xff09;<\/h5>\n<ul>\n<li>\u5b9a\u4e49&#xff1a;\u4ece\u7236\u7c7b\u7ee7\u627f\u800c\u6765\u7684\u7c7b&#xff0c;\u53ef\u4ee5\u65b0\u589e\u6216\u91cd\u5199\u7236\u7c7b\u7684\u65b9\u6cd5\u548c\u5c5e\u6027\u3002<\/li>\n<li>\u4f5c\u7528&#xff1a;\u5728\u7ee7\u627f\u7236\u7c7b\u7684\u57fa\u7840\u4e0a\u8fdb\u884c\u6269\u5c55\u6216\u4fee\u6539&#xff0c;\u5b9e\u73b0\u7279\u5b9a\u7684\u529f\u80fd\u3002<\/li>\n<\/ul>\n<h5>3. \u7ee7\u627f\u7684\u76ee\u7684<\/h5>\n<ul>\n<li>\u4ee3\u7801\u91cd\u7528&#xff1a;\u907f\u514d\u91cd\u590d\u7f16\u5199\u76f8\u540c\u7684\u4ee3\u7801&#xff0c;\u63d0\u9ad8\u5f00\u53d1\u6548\u7387\u3002<\/li>\n<li>\u53ef\u6269\u5c55\u6027&#xff1a;\u901a\u8fc7\u7ee7\u627f&#xff0c;\u5b50\u7c7b\u53ef\u4ee5\u6269\u5c55\u7236\u7c7b\u7684\u529f\u80fd\u3002<\/li>\n<li>\u591a\u6001\u6027&#xff1a;\u540c\u4e00\u4e2a\u65b9\u6cd5\u5728\u4e0d\u540c\u7684\u7c7b\u4e2d\u53ef\u80fd\u6709\u4e0d\u540c\u7684\u5b9e\u73b0&#xff0c;\u589e\u5f3a\u7a0b\u5e8f\u7684\u7075\u6d3b\u6027\u3002<\/li>\n<\/ul>\n<h4>\u4e8c\u3001Python \u4e2d\u7684\u7ee7\u627f\u5b9e\u73b0<\/h4>\n<h5>1. \u57fa\u672c\u8bed\u6cd5<\/h5>\n<p>\u5728 Python \u4e2d&#xff0c;\u7ee7\u627f\u901a\u8fc7\u5728\u7c7b\u5b9a\u4e49\u65f6\u6307\u5b9a\u7236\u7c7b\u6765\u5b9e\u73b0\u3002<\/p>\n<p>class \u5b50\u7c7b\u540d(\u7236\u7c7b\u540d):<br \/>\n    # \u7c7b\u7684\u5b9a\u4e49<\/p>\n<h5>2. \u793a\u4f8b<\/h5>\n<p>\u7236\u7c7b&#xff1a;<\/p>\n<p>class Animal:<br \/>\n    def __init__(self, name):<br \/>\n        self.name &#061; name<\/p>\n<p>    def speak(self):<br \/>\n        pass<\/p>\n<p>\u5b50\u7c7b&#xff1a;<\/p>\n<p>class Dog(Animal):<br \/>\n    def speak(self):<br \/>\n        return f&#034;{self.name} says Woof!&#034;<\/p>\n<p>class Cat(Animal):<br \/>\n    def speak(self):<br \/>\n        return f&#034;{self.name} says Meow!&#034;<\/p>\n<p>\u4f7f\u7528\u5b50\u7c7b&#xff1a;<\/p>\n<p>dog &#061; Dog(&#034;Buddy&#034;)<br \/>\ncat &#061; Cat(&#034;Kitty&#034;)<\/p>\n<p>print(dog.speak())  # \u8f93\u51fa: Buddy says Woof!<br \/>\nprint(cat.speak())  # \u8f93\u51fa: Kitty says Meow!<\/p>\n<h4>\u4e09\u3001\u7ee7\u627f\u7684\u7c7b\u578b<\/h4>\n<h5>1. \u5355\u7ee7\u627f<\/h5>\n<ul>\n<li>\n<p>\u5b9a\u4e49&#xff1a;\u4e00\u4e2a\u5b50\u7c7b\u53ea\u7ee7\u627f\u4e00\u4e2a\u7236\u7c7b\u3002<\/p>\n<\/li>\n<li>\n<p>\u793a\u4f8b&#xff1a;<\/p>\n<p>class Parent:<br \/>\n    pass<\/p>\n<p>class Child(Parent):<br \/>\n    pass<\/p>\n<\/li>\n<\/ul>\n<h5>2. \u591a\u91cd\u7ee7\u627f<\/h5>\n<ul>\n<li>\n<p>\u5b9a\u4e49&#xff1a;\u4e00\u4e2a\u5b50\u7c7b\u7ee7\u627f\u591a\u4e2a\u7236\u7c7b\u3002<\/p>\n<\/li>\n<li>\n<p>\u8bed\u6cd5&#xff1a;<\/p>\n<p>class \u5b50\u7c7b\u540d(\u7236\u7c7b1, \u7236\u7c7b2, &#8230;):<br \/>\n    pass<\/p>\n<\/li>\n<li>\n<p>\u793a\u4f8b&#xff1a;<\/p>\n<p>class Flyable:<br \/>\n    def fly(self):<br \/>\n        return &#034;I can fly!&#034;<\/p>\n<p>class Swimmable:<br \/>\n    def swim(self):<br \/>\n        return &#034;I can swim!&#034;<\/p>\n<p>class Duck(Flyable, Swimmable):<br \/>\n    pass<\/p>\n<p>duck &#061; Duck()<br \/>\nprint(duck.fly())   # \u8f93\u51fa: I can fly!<br \/>\nprint(duck.swim())  # \u8f93\u51fa: I can swim!<\/p>\n<\/li>\n<\/ul>\n<h5>3. \u591a\u5c42\u7ee7\u627f<\/h5>\n<ul>\n<li>\n<p>\u5b9a\u4e49&#xff1a;\u5b50\u7c7b\u7ee7\u627f\u7236\u7c7b&#xff0c;\u7236\u7c7b\u518d\u7ee7\u627f\u5176\u7236\u7c7b&#xff0c;\u5f62\u6210\u7ee7\u627f\u94fe\u3002<\/p>\n<\/li>\n<li>\n<p>\u793a\u4f8b&#xff1a;<\/p>\n<p>class GrandParent:<br \/>\n    pass<\/p>\n<p>class Parent(GrandParent):<br \/>\n    pass<\/p>\n<p>class Child(Parent):<br \/>\n    pass<\/p>\n<\/li>\n<\/ul>\n<h4>\u56db\u3001\u65b9\u6cd5\u91cd\u5199&#xff08;Override&#xff09;<\/h4>\n<ul>\n<li>\u5b9a\u4e49&#xff1a;\u5b50\u7c7b\u91cd\u65b0\u5b9a\u4e49\u7236\u7c7b\u7684\u540c\u540d\u65b9\u6cd5&#xff0c;\u4ee5\u5b9e\u73b0\u4e0d\u540c\u7684\u529f\u80fd\u3002<\/li>\n<li>\u4f5c\u7528&#xff1a;\u8ba9\u5b50\u7c7b\u80fd\u591f\u6839\u636e\u9700\u8981\u4fee\u6539\u6216\u6269\u5c55\u7236\u7c7b\u7684\u65b9\u6cd5\u884c\u4e3a\u3002<\/li>\n<\/ul>\n<h5>\u793a\u4f8b&#xff1a;<\/h5>\n<p>class Vehicle:<br \/>\n    def move(self):<br \/>\n        print(&#034;The vehicle is moving.&#034;)<\/p>\n<p>class Car(Vehicle):<br \/>\n    def move(self):<br \/>\n        print(&#034;The car is driving on the road.&#034;)<\/p>\n<p>vehicle &#061; Vehicle()<br \/>\ncar &#061; Car()<\/p>\n<p>vehicle.move()  # \u8f93\u51fa: The vehicle is moving.<br \/>\ncar.move()      # \u8f93\u51fa: The car is driving on the road.<\/p>\n<h4>\u4e94\u3001\u8c03\u7528\u7236\u7c7b\u7684\u65b9\u6cd5<\/h4>\n<ul>\n<li>\n<p>\u4f7f\u7528\u00a0super()\u00a0\u51fd\u6570&#xff1a;\u5728\u5b50\u7c7b\u4e2d\u8c03\u7528\u7236\u7c7b\u7684\u65b9\u6cd5\u6216\u521d\u59cb\u5316\u7236\u7c7b\u3002<\/p>\n<\/li>\n<li>\n<p>\u8bed\u6cd5&#xff1a;<\/p>\n<p>class \u5b50\u7c7b\u540d(\u7236\u7c7b\u540d):<br \/>\n    def \u65b9\u6cd5\u540d(self, \u53c2\u6570):<br \/>\n        super().\u65b9\u6cd5\u540d(\u53c2\u6570)<\/p>\n<\/li>\n<\/ul>\n<h5>\u793a\u4f8b&#xff1a;<\/h5>\n<p>class Person:<br \/>\n    def __init__(self, name):<br \/>\n        self.name &#061; name<\/p>\n<p>class Employee(Person):<br \/>\n    def __init__(self, name, employee_id):<br \/>\n        super().__init__(name)  # \u8c03\u7528\u7236\u7c7b\u7684\u6784\u9020\u51fd\u6570<br \/>\n        self.employee_id &#061; employee_id<\/p>\n<p>employee &#061; Employee(&#034;Alice&#034;, &#034;E123&#034;)<br \/>\nprint(employee.name)         # \u8f93\u51fa: Alice<br \/>\nprint(employee.employee_id)  # \u8f93\u51fa: E123<\/p>\n<h4>\u516d\u3001\u7ee7\u627f\u4e2d\u7684\u7279\u6b8a\u65b9\u6cd5<\/h4>\n<h5>1.\u00a0__init__\u00a0\u6784\u9020\u51fd\u6570<\/h5>\n<ul>\n<li>\u7ee7\u627f\u7279\u6027&#xff1a;\u5b50\u7c7b\u7684\u00a0__init__\u00a0\u65b9\u6cd5\u4f1a\u8986\u76d6\u7236\u7c7b\u7684\u00a0__init__\u00a0\u65b9\u6cd5\u3002<\/li>\n<li>\u6ce8\u610f&#xff1a;\u5982\u679c\u5b50\u7c7b\u5b9a\u4e49\u4e86\u00a0__init__\u00a0\u65b9\u6cd5&#xff0c;\u9700\u8981\u663e\u5f0f\u8c03\u7528\u7236\u7c7b\u7684\u00a0__init__\u00a0\u65b9\u6cd5\u6765\u521d\u59cb\u5316\u7236\u7c7b\u7684\u5c5e\u6027\u3002<\/li>\n<\/ul>\n<p>\u793a\u4f8b&#xff1a;<\/p>\n<p>class Parent:<br \/>\n    def __init__(self):<br \/>\n        print(&#034;Parent init&#034;)<\/p>\n<p>class Child(Parent):<br \/>\n    def __init__(self):<br \/>\n        super().__init__()  # \u8c03\u7528\u7236\u7c7b\u7684\u6784\u9020\u51fd\u6570<br \/>\n        print(&#034;Child init&#034;)<\/p>\n<p>child &#061; Child()<br \/>\n# \u8f93\u51fa:<br \/>\n# Parent init<br \/>\n# Child init<\/p>\n<h5>2.\u00a0__str__\u00a0\u548c\u00a0__repr__\u00a0\u65b9\u6cd5<\/h5>\n<ul>\n<li>\u4f5c\u7528&#xff1a;\u5b9a\u4e49\u5bf9\u8c61\u7684\u5b57\u7b26\u4e32\u8868\u793a\u5f62\u5f0f\u3002<\/li>\n<li>\u7ee7\u627f\u7279\u6027&#xff1a;\u5b50\u7c7b\u53ef\u4ee5\u91cd\u5199\u8fd9\u4e9b\u65b9\u6cd5&#xff0c;\u63d0\u4f9b\u81ea\u5b9a\u4e49\u7684\u5b57\u7b26\u4e32\u8868\u793a\u3002<\/li>\n<\/ul>\n<p>\u793a\u4f8b&#xff1a;<\/p>\n<p>class Animal:<br \/>\n    def __str__(self):<br \/>\n        return &#034;This is an animal.&#034;<\/p>\n<p>class Dog(Animal):<br \/>\n    def __str__(self):<br \/>\n        return &#034;This is a dog.&#034;<\/p>\n<p>dog &#061; Dog()<br \/>\nprint(dog)  # \u8f93\u51fa: This is a dog.<\/p>\n<h4>\u4e03\u3001\u7ee7\u627f\u7684\u6ce8\u610f\u4e8b\u9879<\/h4>\n<h5>1. \u8bbf\u95ee\u6743\u9650<\/h5>\n<ul>\n<li>\n<p>Python \u4e2d\u4e0d\u5b58\u5728\u50cf Java \u6216 C&#043;&#043; \u90a3\u6837\u7684\u8bbf\u95ee\u4fee\u9970\u7b26&#xff08;public\u3001private\u3001protected&#xff09;\u3002<\/p>\n<\/li>\n<li>\n<p>\u4ee5\u53cc\u4e0b\u5212\u7ebf\u00a0__\u00a0\u5f00\u5934\u7684\u5c5e\u6027\u6216\u65b9\u6cd5\u88ab\u89c6\u4e3a\u79c1\u6709\u6210\u5458&#xff0c;\u4e0d\u80fd\u5728\u5b50\u7c7b\u4e2d\u76f4\u63a5\u8bbf\u95ee\u3002<\/p>\n<\/li>\n<li>\n<p>\u793a\u4f8b&#xff1a;<\/p>\n<p>class Parent:<br \/>\n    def __init__(self):<br \/>\n        self.__private_var &#061; 42<\/p>\n<p>class Child(Parent):<br \/>\n    def get_private_var(self):<br \/>\n        return self.__private_var  # \u8fd9\u5c06\u5f15\u53d1 AttributeError<\/p>\n<p>child &#061; Child()<br \/>\nprint(child.get_private_var())<\/p>\n<\/li>\n<\/ul>\n<h5>2. \u65b9\u6cd5\u89e3\u6790\u987a\u5e8f&#xff08;MRO&#xff09;<\/h5>\n<ul>\n<li>\u5728\u591a\u91cd\u7ee7\u627f\u4e2d&#xff0c;Python \u4f7f\u7528**\u65b9\u6cd5\u89e3\u6790\u987a\u5e8f&#xff08;Method Resolution Order, MRO&#xff09;**\u6765\u786e\u5b9a\u5c5e\u6027\u548c\u65b9\u6cd5\u7684\u67e5\u627e\u987a\u5e8f\u3002<\/li>\n<li>\u53ef\u4ee5\u4f7f\u7528\u00a0\u7c7b\u540d.mro()\u00a0\u67e5\u770b MRO \u5217\u8868\u3002<\/li>\n<\/ul>\n<h4>\u793a\u4f8b&#xff1a;<\/h4>\n<p>class A:<br \/>\n    pass<\/p>\n<p>class B(A):<br \/>\n    pass<\/p>\n<p>class C(A):<br \/>\n    pass<\/p>\n<p>class D(B, C):<br \/>\n    pass<\/p>\n<p>print(D.mro())<br \/>\n# \u8f93\u51fa: [&lt;class &#039;__main__.D&#039;&gt;, &lt;class &#039;__main__.B&#039;&gt;, &lt;class &#039;__main__.C&#039;&gt;, &lt;class &#039;__main__.A&#039;&gt;, &lt;class &#039;object&#039;&gt;]<\/p>\n<hr \/>\n<h4>&#x1f195; \u516b\u3001MRO \u4e0e C3 \u7ebf\u6027\u5316\u7b97\u6cd5 \u2014\u2014 \u6df1\u5165\u7406\u89e3<\/h4>\n<p>MRO \u4e0d\u662f&#034;\u9b54\u6cd5&#034;&#xff0c;\u800c\u662f\u7531\u00a0C3 \u7ebf\u6027\u5316\u7b97\u6cd5\u786e\u5b9a\u7684\u3002\u7406\u89e3\u8fd9\u4e2a\u7b97\u6cd5\u5bf9\u4e8e\u6392\u67e5\u591a\u91cd\u7ee7\u627f bug \u81f3\u5173\u91cd\u8981\u3002<\/p>\n<h5>C3 \u7b97\u6cd5\u6838\u5fc3\u89c4\u5219<\/h5>\n<p>\u7ed9\u5b9a\u4e00\u4e2a\u7c7b C \u7684\u7ee7\u627f\u5217\u8868\u00a0[C, (C1, C2, &#8230;, Cn)]&#xff0c;MRO \u7684\u8ba1\u7b97\u9075\u5faa&#xff1a;<\/p>\n<li>C \u7684 MRO\u00a0&#061;\u00a0[C]\u00a0&#043;\u00a0merge(C1 \u7684 MRO, C2 \u7684 MRO, \u2026, Cn \u7684 MRO,\u00a0[C1, C2, &#8230;, Cn])<\/li>\n<li>merge \u64cd\u4f5c&#xff1a;\u53d6\u6bcf\u4e2a\u5217\u8868\u7684\u5934\u90e8&#xff08;\u7b2c\u4e00\u4e2a\u5143\u7d20&#xff09;&#xff0c;\u5982\u679c\u8be5\u5143\u7d20\u4e0d\u5728\u4efb\u4f55\u5217\u8868\u7684\u5c3e\u90e8&#xff08;\u9664\u5934\u90e8\u5916\u7684\u5176\u4f59\u90e8\u5206&#xff09;&#xff0c;\u5219\u53d6\u51fa&#xff1b;\u5426\u5219\u8df3\u8fc7\u8be5\u5217\u8868&#xff0c;\u5c1d\u8bd5\u4e0b\u4e00\u4e2a<\/li>\n<h5>\u56fe\u89e3&#xff1a;\u83f1\u5f62\u7ee7\u627f\u7684 MRO \u8ba1\u7b97<\/h5>\n<p>    A<br \/>\n   \/ \\\\<br \/>\n  B   C<br \/>\n   \\\\ \/<br \/>\n    D(B, C)<\/p>\n<p>class A:<br \/>\n    def who_am_i(self):<br \/>\n        return &#034;A&#034;<\/p>\n<p>class B(A):<br \/>\n    def who_am_i(self):<br \/>\n        return &#034;B&#034;<\/p>\n<p>class C(A):<br \/>\n    def who_am_i(self):<br \/>\n        return &#034;C&#034;<\/p>\n<p>class D(B, C):<br \/>\n    pass<\/p>\n<p>\u9010\u6b65\u8ba1\u7b97 D \u7684 MRO&#xff1a;<\/p>\n<p>L[D] &#061; D &#043; merge(L[B], L[C], BC)<br \/>\n     &#061; D &#043; merge(BA, CA, BC)        \u2190 L[B]&#061;[B,A], L[C]&#061;[C,A], BC&#061;[B,C]<\/p>\n<p>Step 1: \u53d6 B&#xff08;B \u4e0d\u5728\u4efb\u4f55 tail \u4e2d&#xff09;\u2705 \u2192 [D, B]<br \/>\n        merge(A, CA, C)             \u2190 B \u5df2\u53d6\u51fa&#xff0c;\u5269\u4f59<\/p>\n<p>Step 2: \u53d6 A \u2192 A \u5728 [C, A] \u7684 tail \u4e2d \u274c \u8df3\u8fc7<br \/>\n        \u53d6 C \u2192 C \u4e0d\u5728\u4efb\u4f55 tail \u4e2d \u2705 \u2192 [D, B, C]<br \/>\n        merge(A, A)                \u2190 C \u5df2\u53d6\u51fa<\/p>\n<p>Step 3: \u53d6 A \u2705 \u2192 [D, B, C, A]<\/p>\n<p>\u6700\u7ec8 L[D] &#061; [D, B, C, A, object]<\/p>\n<p>print(D.mro())<br \/>\n# [&lt;class &#039;D&#039;&gt;, &lt;class &#039;B&#039;&gt;, &lt;class &#039;C&#039;&gt;, &lt;class &#039;A&#039;&gt;, &lt;class &#039;object&#039;&gt;]<\/p>\n<p>d &#061; D()<br \/>\nprint(d.who_am_i())  # &#034;B&#034; \u2014 B \u5728 MRO \u4e2d\u6392\u5728 C \u524d\u9762<\/p>\n<p>\u9762\u8bd5\u91cd\u70b9&#xff1a;\u4e3a\u4ec0\u4e48 D \u7684 MRO \u662f D\u2192B\u2192C\u2192A \u800c\u4e0d\u662f D\u2192C\u2192B\u2192A&#xff1f;<\/p>\n<p>\u56e0\u4e3a D \u7684\u5b9a\u4e49\u662f\u00a0class D(B, C)\u00a0\u2014\u2014\u00a0B \u5728\u524d\u4f18\u5148\u3002C3 \u4fdd\u8bc1\u4e86\u8fd9\u4e2a&#034;\u5c40\u90e8\u4f18\u5148\u5e8f&#034;\u7684\u540c\u65f6\u8fd8\u6ee1\u8db3\u5355\u8c03\u6027\u3002<\/p>\n<hr \/>\n<h4>&#x1f195; \u4e5d\u3001\u83f1\u5f62\u7ee7\u627f\u95ee\u9898 (Diamond Problem)<\/h4>\n<p>\u5f53\u591a\u91cd\u7ee7\u627f\u5f62\u6210\u83f1\u5f62\u7ed3\u6784\u65f6&#xff0c;\u5982\u4f55\u786e\u4fdd\u7956\u5148\u7c7b\u53ea\u88ab\u521d\u59cb\u5316\u4e00\u6b21&#xff1f;<\/p>\n<p>      A (\u6709 __init__)<br \/>\n     \/ \\\\<br \/>\n    B   C (\u90fd\u8c03\u7528\u4e86 super().__init__())<br \/>\n     \\\\ \/<br \/>\n      D<\/p>\n<p>\u4f20\u7edf\u8bed\u8a00\u7684\u56f0\u5883&#xff1a;C&#043;&#043; \u4e2d A \u4f1a\u88ab\u521d\u59cb\u5316\u4e24\u6b21&#xff08;B \u521d\u59cb\u5316\u4e00\u6b21&#xff0c;C \u521d\u59cb\u5316\u4e00\u6b21&#xff09;\u3002<\/p>\n<p>Python \u7684\u89e3\u51b3\u65b9\u6848&#xff1a;\u534f\u4f5c\u5f0f\u591a\u91cd\u7ee7\u627f (Cooperative MI)\u00a0&#043;\u00a0super() \u7684 MRO \u94fe\u5f0f\u8c03\u7528\u3002<\/p>\n<p>class A:<br \/>\n    def __init__(self):<br \/>\n        print(&#034;  A.__init__&#034;)<br \/>\n        self.a_val &#061; &#034;from A&#034;<\/p>\n<p>class B(A):<br \/>\n    def __init__(self):<br \/>\n        print(&#034;  B.__init__ (before super)&#034;)<br \/>\n        super().__init__()  # \u4e0d\u786c\u7f16\u7801 A&#xff0c;\u800c\u662f\u8c03\u7528 MRO \u4e2d\u7684\u4e0b\u4e00\u4e2a<br \/>\n        print(&#034;  B.__init__ (after super)&#034;)<br \/>\n        self.b_val &#061; &#034;from B&#034;<\/p>\n<p>class C(A):<br \/>\n    def __init__(self):<br \/>\n        print(&#034;  C.__init__ (before super)&#034;)<br \/>\n        super().__init__()<br \/>\n        print(&#034;  C.__init__ (after super)&#034;)<br \/>\n        self.c_val &#061; &#034;from C&#034;<\/p>\n<p>class D(B, C):<br \/>\n    def __init__(self):<br \/>\n        print(&#034;D.__init__ (before super)&#034;)<br \/>\n        super().__init__()  # \u4e00\u884c super() \u89e6\u53d1\u6574\u6761 MRO \u94fe<br \/>\n        print(&#034;D.__init__ (after super)&#034;)<\/p>\n<p>print(&#034;MRO:&#034;, [cls.__name__ for cls in D.mro()])<br \/>\nd &#061; D()<\/p>\n<p># \u8f93\u51fa&#xff1a;<br \/>\n# MRO: [&#039;D&#039;, &#039;B&#039;, &#039;C&#039;, &#039;A&#039;, &#039;object&#039;]<br \/>\n# D.__init__ (before super)<br \/>\n#   B.__init__ (before super)<br \/>\n#   C.__init__ (before super)<br \/>\n#     A.__init__        \u2190 A \u53ea\u88ab\u521d\u59cb\u5316\u4e86\u4e00\u6b21&#xff01;<br \/>\n#   C.__init__ (after super)<br \/>\n#   B.__init__ (after super)<br \/>\n# D.__init__ (after super)<\/p>\n<p>\u5173\u952e\u7ed3\u8bba&#xff1a;<\/p>\n<ul>\n<li>super()\u00a0\u4e0d\u662f&#034;\u8c03\u7528\u7236\u7c7b&#034;&#xff0c;\u800c\u662f\u8c03\u7528\u00a0MRO \u4e2d\u7684\u4e0b\u4e00\u4e2a\u7c7b<\/li>\n<li>\u53ea\u8981\u6bcf\u4e2a\u7c7b\u7684\u00a0__init__\u00a0\u90fd\u4f7f\u7528\u00a0super().__init__()&#xff0c;\u5c31\u80fd\u4fdd\u8bc1\u6bcf\u4e2a\u7c7b\u6070\u597d\u521d\u59cb\u5316\u4e00\u6b21<\/li>\n<li>\u8fd9\u5c31\u662f\u4e3a\u4ec0\u4e48 Python \u7684\u00a0super()\u00a0\u63a8\u8350\u65e0\u53c2\u8c03\u7528&#xff08;Python 3&#043;&#xff09;<\/li>\n<\/ul>\n<hr \/>\n<h4>&#x1f195; \u5341\u3001Mixin \u6a21\u5f0f \u2014\u2014 Python \u591a\u91cd\u7ee7\u627f\u7684\u6700\u4f73\u5b9e\u8df5<\/h4>\n<p>Mixin \u662f\u4e00\u79cd\u7279\u6b8a\u7684\u7c7b&#xff0c;\u5b83&#xff1a;<\/p>\n<li>\u4e0d\u5355\u72ec\u5b9e\u4f8b\u5316&#xff08;\u901a\u5e38\u4e0d\u5b9a\u4e49\u00a0__init__&#xff09;<\/li>\n<li>\u63d0\u4f9b\u5355\u4e00\u529f\u80fd&#xff08;\u5355\u4e00\u804c\u8d23&#xff09;<\/li>\n<li>\u901a\u8fc7\u7ec4\u5408\u6df7\u5165\u5230\u76ee\u6807\u7c7b&#xff08;\u547d\u540d\u4ee5\u00a0Mixin\u00a0\/\u00a0able\u00a0\u7ed3\u5c3e&#xff09;<\/li>\n<p># &#061;&#061;&#061;&#061;&#061; \u5b9a\u4e49 Mixin \u7c7b &#061;&#061;&#061;&#061;&#061;<\/p>\n<p>class JSONSerializableMixin:<br \/>\n    &#034;&#034;&#034;\u63d0\u4f9b JSON \u5e8f\u5217\u5316\u80fd\u529b&#034;&#034;&#034;<\/p>\n<p>    def to_json(self) -&gt; str:<br \/>\n        import json<br \/>\n        # \u53ea\u5e8f\u5217\u5316\u516c\u6709\u5c5e\u6027<br \/>\n        data &#061; {k: v for k, v in self.__dict__.items() if not k.startswith(&#039;_&#039;)}<br \/>\n        return json.dumps(data, default&#061;str)<\/p>\n<p>class LoggingMixin:<br \/>\n    &#034;&#034;&#034;\u63d0\u4f9b\u65b9\u6cd5\u8c03\u7528\u65e5\u5fd7\u80fd\u529b&#034;&#034;&#034;<\/p>\n<p>    def log_call(self, method_name: str, *args, **kwargs):<br \/>\n        print(f&#034;[LOG] {self.__class__.__name__}.{method_name} called &#034;<br \/>\n              f&#034;with args&#061;{args}, kwargs&#061;{kwargs}&#034;)<\/p>\n<p>class ValidationMixin:<br \/>\n    &#034;&#034;&#034;\u63d0\u4f9b\u6570\u636e\u6821\u9a8c\u80fd\u529b&#034;&#034;&#034;<\/p>\n<p>    def validate_required(self, *fields):<br \/>\n        for field in fields:<br \/>\n            if not getattr(self, field, None):<br \/>\n                raise ValueError(f&#034;{field} is required&#034;)<\/p>\n<p># &#061;&#061;&#061;&#061;&#061; \u76ee\u6807\u7c7b\u901a\u8fc7\u591a\u91cd\u7ee7\u627f\u7ec4\u5408 Mixin &#061;&#061;&#061;&#061;&#061;<\/p>\n<p>class User(JSONSerializableMixin, LoggingMixin, ValidationMixin):<br \/>\n    def __init__(self, username, email):<br \/>\n        self.username &#061; username<br \/>\n        self.email &#061; email<\/p>\n<p>    def save(self):<br \/>\n        self.log_call(&#039;save&#039;)<br \/>\n        self.validate_required(&#039;username&#039;, &#039;email&#039;)<br \/>\n        print(f&#034;Saving user: {self.to_json()}&#034;)<\/p>\n<p>u &#061; User(&#034;alice&#034;, &#034;alice&#064;example.com&#034;)<br \/>\nu.save()<br \/>\n# [LOG] User.save called with args&#061;(), kwargs&#061;{}<br \/>\n# Saving user: {&#034;username&#034;: &#034;alice&#034;, &#034;email&#034;: &#034;alice&#064;example.com&#034;}<\/p>\n<p>Mixin \u547d\u540d\u89c4\u8303&#xff1a;<\/p>\n<table>\n<tr>\u547d\u540d\u98ce\u683c\u542b\u4e49\u793a\u4f8b<\/tr>\n<tbody>\n<tr>\n<td>XxxMixin<\/td>\n<td>\u660e\u786e\u6807\u8bc6\u4e3a Mixin<\/td>\n<td>LoggingMixin<\/td>\n<\/tr>\n<tr>\n<td>Xxxable<\/td>\n<td>\u8868\u793a\u5177\u5907\u67d0\u79cd\u80fd\u529b<\/td>\n<td>Serializable<\/td>\n<\/tr>\n<tr>\n<td>\u4ee5\u00a0_\u00a0\u5f00\u5934<\/td>\n<td>\u8868\u793a\u5185\u90e8 Mixin<\/td>\n<td>_CacheMixin<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>Mixin \u5728 AI \u6846\u67b6\u4e2d\u7684\u5e94\u7528&#xff1a;<\/p>\n<p># PyTorch \u98ce\u683c\u7684 Mixin \u793a\u4f8b<br \/>\nclass GradientCheckpointingMixin:<br \/>\n    &#034;&#034;&#034;\u6df7\u5408\u68af\u5ea6 checkpoint \u4ee5\u8282\u7701\u663e\u5b58&#034;&#034;&#034;<\/p>\n<p>    def enable_gradient_checkpointing(self):<br \/>\n        if hasattr(self, &#039;forward&#039;):<br \/>\n            original_forward &#061; self.forward<\/p>\n<p>            def checkpointed_forward(*args, **kwargs):<br \/>\n                from torch.utils.checkpoint import checkpoint<br \/>\n                return checkpoint(original_forward, *args, **kwargs,<br \/>\n                                  use_reentrant&#061;False)<\/p>\n<p>            self.forward &#061; checkpointed_forward<br \/>\n            print(f&#034;[{self.__class__.__name__}] Gradient checkpointing enabled&#034;)<\/p>\n<p>class MyTransformer(GradientCheckpointingMixin, nn.Module):<br \/>\n    def __init__(self):<br \/>\n        super().__init__()<br \/>\n        self.layer &#061; nn.Linear(1024, 1024)<\/p>\n<p>    def forward(self, x):<br \/>\n        return self.layer(x)<\/p>\n<p>model &#061; MyTransformer()<br \/>\nmodel.enable_gradient_checkpointing()  # \u52a8\u6001\u6df7\u5165\u80fd\u529b<\/p>\n<hr \/>\n<h4>&#x1f195; \u5341\u4e00\u3001\u7ec4\u5408\u4f18\u4e8e\u7ee7\u627f (Composition over Inheritance)<\/h4>\n<p>\u539f\u5219&#xff1a;\u5982\u679c\u4e24\u4e2a\u7c7b\u4e4b\u95f4\u7684\u5173\u7cfb\u4e0d\u662f&#034;is-a&#034;&#xff08;\u732b\u662f\u52a8\u7269&#xff09;&#xff0c;\u800c\u662f&#034;has-a&#034;&#xff08;\u6c7d\u8f66\u6709\u5f15\u64ce&#xff09;&#xff0c;\u5e94\u8be5\u4f7f\u7528\u7ec4\u5408\u800c\u975e\u7ee7\u627f\u3002<\/p>\n<p># \u274c \u53cd\u4f8b&#xff1a;\u6ee5\u7528\u7ee7\u627f \u2014\u2014 \u5f15\u64ce\u5e76\u4e0d\u662f\u4e00\u79cd&#034;\u6c7d\u8f66&#034;<br \/>\nclass Vehicle:<br \/>\n    def move(self): pass<\/p>\n<p>class Engine(Vehicle):  # Engine is-a Vehicle? \u8bed\u4e49\u9519\u8bef&#xff01;<br \/>\n    def start(self): pass<\/p>\n<p>class Car(Engine):      # Car is-a Engine? \u66f4\u52a0\u5947\u602a<br \/>\n    pass<\/p>\n<p># \u2705 \u6b63\u89e3&#xff1a;\u4f7f\u7528\u7ec4\u5408 \u2014\u2014 \u6c7d\u8f66&#034;\u62e5\u6709&#034;\u5f15\u64ce<br \/>\nclass Engine:<br \/>\n    def start(self):<br \/>\n        print(&#034;Engine started&#034;)<\/p>\n<p>    def stop(self):<br \/>\n        print(&#034;Engine stopped&#034;)<\/p>\n<p>class Wheel:<br \/>\n    def rotate(self):<br \/>\n        print(&#034;Wheel rotating&#034;)<\/p>\n<p>class Car:<br \/>\n    &#034;&#034;&#034;Car has-an Engine and has Wheels&#034;&#034;&#034;<\/p>\n<p>    def __init__(self):<br \/>\n        self.engine &#061; Engine()   # \u7ec4\u5408<br \/>\n        self.wheels &#061; [Wheel() for _ in range(4)]<\/p>\n<p>    def start(self):<br \/>\n        self.engine.start()<\/p>\n<p>    def drive(self):<br \/>\n        for wheel in self.wheels:<br \/>\n            wheel.rotate()<\/p>\n<p># &#061;&#061;&#061;&#061;&#061; \u51b3\u7b56\u6307\u5357 &#061;&#061;&#061;&#061;&#061;<br \/>\n#<br \/>\n# \u95ee\u81ea\u5df1&#xff1a;X is a Y \u5417&#xff1f;<br \/>\n#   \u2713 \u662f \u2192 \u7528\u7ee7\u627f  (Dog is an Animal)<br \/>\n#   \u2717 \u5426 \u2192 \u7528\u7ec4\u5408  (Car has an Engine; Employee has a SalaryCalculator)<\/p>\n<p>GoF \u8bbe\u8ba1\u539f\u5219\u539f\u6587&#xff1a;<\/p>\n<p>\u201cFavor \u2018object composition\u2019 over \u2018class inheritance\u2019. [\u2026] Inheritance breaks encapsulation (subclass depends on parent\u2019s implementation details).\u201d<\/p>\n<p>\u7ee7\u627f\u7834\u574f\u5c01\u88c5\u6027\u7684\u539f\u56e0&#xff1a;\u5b50\u7c7b\u4f9d\u8d56\u4e8e\u7236\u7c7b\u7684\u5185\u90e8\u5b9e\u73b0\u7ec6\u8282&#xff0c;\u7236\u7c7b\u7684\u6539\u52a8\u53ef\u80fd\u6ce2\u53ca\u6240\u6709\u5b50\u7c7b&#xff08;\u8106\u5f31\u57fa\u7c7b\u95ee\u9898&#xff09;\u3002<\/p>\n<hr \/>\n<h4>&#x1f195; \u5341\u4e8c\u3001__init_subclass__\u00a0\u2014\u2014 \u5b50\u7c7b\u6ce8\u518c\u4e0e\u7ea6\u675f\u94a9\u5b50<\/h4>\n<p>Python 3.6&#043; \u63d0\u4f9b\u7684\u7c7b\u94a9\u5b50&#xff0c;\u5728\u5b50\u7c7b\u88ab\u5b9a\u4e49\u65f6\u81ea\u52a8\u8c03\u7528&#xff08;\u6ce8\u610f&#xff1a;\u4e0d\u662f\u5728\u5b9e\u4f8b\u5316\u65f6&#xff09;\u3002<\/p>\n<p>class PluginBase:<br \/>\n    &#034;&#034;&#034;\u63d2\u4ef6\u57fa\u7c7b&#xff1a;\u81ea\u52a8\u6ce8\u518c\u6240\u6709\u5b50\u7c7b&#034;&#034;&#034;<\/p>\n<p>    _registry &#061; {}  # plugin_name -&gt; subclass<\/p>\n<p>    def __init_subclass__(cls, plugin_name&#061;None, **kwargs):<br \/>\n        super().__init_subclass__(**kwargs)<br \/>\n        if plugin_name is None:<br \/>\n            raise TypeError(<br \/>\n                f&#034;Subclass {cls.__name__} must specify &#039;plugin_name&#039; keyword argument&#034;<br \/>\n            )<br \/>\n        cls._registry[plugin_name] &#061; cls<br \/>\n        cls.plugin_name &#061; plugin_name<br \/>\n        print(f&#034;[Auto-register] Plugin &#039;{plugin_name}&#039; \u2192 {cls.__name__}&#034;)<\/p>\n<p>    def run(self, data):<br \/>\n        raise NotImplementedError<\/p>\n<p># &#061;&#061;&#061;&#061;&#061; \u5b9a\u4e49\u63d2\u4ef6\u5b50\u7c7b &#061;&#061;&#061;&#061;&#061;<br \/>\nclass TextProcessor(PluginBase, plugin_name&#061;&#034;text&#034;):<br \/>\n    def run(self, data):<br \/>\n        return data.lower()<\/p>\n<p>class ImageProcessor(PluginBase, plugin_name&#061;&#034;image&#034;):<br \/>\n    def run(self, data):<br \/>\n        return f&#034;[IMAGE processed: {len(data)} bytes]&#034;<\/p>\n<p># &#061;&#061;&#061;&#061;&#061; \u4f7f\u7528\u6ce8\u518c\u8868 &#061;&#061;&#061;&#061;&#061;<br \/>\nprint(&#034;Available plugins:&#034;, list(PluginBase._registry.keys()))<br \/>\n# [&#039;text&#039;, &#039;image&#039;]<\/p>\n<p>processor &#061; PluginBase._registry[&#034;text&#034;]()<br \/>\nprint(processor.run(&#034;HELLO WORLD&#034;))  # &#034;hello world&#034;<\/p>\n<p>\u5b9e\u9645\u5e94\u7528\u573a\u666f&#xff1a;<\/p>\n<ul>\n<li>\u63d2\u4ef6\u7cfb\u7edf&#xff1a;\u81ea\u52a8\u53d1\u73b0\u548c\u6ce8\u518c\u5904\u7406\u5668&#xff08;\u5982 FastAPI \u7684\u8def\u7531\u6ce8\u518c&#xff09;<\/li>\n<li>\u5951\u7ea6\u5f3a\u5236&#xff1a;\u8981\u6c42\u5b50\u7c7b\u5b9e\u73b0\u7279\u5b9a\u5c5e\u6027\u6216\u65b9\u6cd5<\/li>\n<li>ORM \u6620\u5c04&#xff1a;SQLAlchemy \u7684\u00a0declarative_base\u00a0\u5c31\u662f\u57fa\u4e8e\u6b64\u673a\u5236<\/li>\n<\/ul>\n<hr \/>\n<h4>&#x1f195; \u5341\u4e09\u3001super()\u00a0\u7684\u672c\u8d28\u6df1\u5165<\/h4>\n<h5>super()\u00a0\u5230\u5e95\u8fd4\u56de\u4ec0\u4e48&#xff1f;<\/h5>\n<p>class A:<br \/>\n    def method(self):<br \/>\n        print(&#034;A.method&#034;, self)<\/p>\n<p>class B(A):<br \/>\n    def method(self):<br \/>\n        print(&#034;B.method (before super)&#034;, self)<br \/>\n        super().method()   # super() \u8fd4\u56de\u7684\u662f\u4ec0\u4e48&#xff1f;<br \/>\n        print(&#034;B.method (after super)&#034;, self)<\/p>\n<p>b &#061; B()<br \/>\nb.method()<\/p>\n<p>super()\u00a0\u8fd4\u56de\u7684\u662f\u4e00\u4e2a\u4ee3\u7406\u5bf9\u8c61 (proxy)&#xff0c;\u5b83&#xff1a;<\/p>\n<li>\u8bb0\u5f55\u4e86\u5f53\u524d\u7c7b\u7684 MRO<\/li>\n<li>\u8bb0\u5f55\u4e86\u5f53\u524d\u7c7b\u5728 MRO \u4e2d\u7684\u4f4d\u7f6e<\/li>\n<li>\u5c5e\u6027\u67e5\u627e\u4ece MRO \u7684\u4e0b\u4e00\u4e2a\u4f4d\u7f6e\u5f00\u59cb<\/li>\n<p># \u9a8c\u8bc1 super() \u7684\u884c\u4e3a<br \/>\nprint(super(B, b))  # &lt;super: &lt;class &#039;B&#039;&gt;, &lt;B object&gt;&gt;<br \/>\n# \u8fd9\u4e2a\u4ee3\u7406\u5bf9\u8c61\u5728\u67e5\u627e attribute \u65f6&#xff0c;<br \/>\n# \u4f1a\u4ece B \u7684 MRO \u4e2d B \u7684\u4e0b\u4e00\u4e2a\u7c7b\u5f00\u59cb\u627e<\/p>\n<p>\u4e3a\u4ec0\u4e48\u63a8\u8350\u65e0\u53c2\u00a0super()&#xff08;Python 3&#043;&#xff09;&#xff1f;<\/p>\n<ul>\n<li>Python 2 \u5fc5\u987b\u5199\u00a0super(ClassName, self)<\/li>\n<li>Python 3 \u7684\u00a0super()\u00a0\u5728\u7c7b\u65b9\u6cd5\u5185\u90e8\u4f1a\u81ea\u52a8\u63a8\u65ad\u5f53\u524d\u7c7b\u548c self<\/li>\n<li>\u65e0\u53c2\u5199\u6cd5\u5728\u91cd\u6784\u7c7b\u540d\u65f6\u4e0d\u4f1a\u51fa\u9519<\/li>\n<\/ul>\n<p>super()\u00a0\u7684\u5e38\u89c1\u9677\u9631&#xff1a;<\/p>\n<p># \u26a0\ufe0f \u9677\u9631&#xff1a;\u5728\u7c7b\u5916\u90e8\u4f7f\u7528 super()<br \/>\nclass Parent:<br \/>\n    def __init__(self):<br \/>\n        self.value &#061; 10<\/p>\n<p>class Child(Parent):<br \/>\n    def __init__(self):<br \/>\n        super().__init__()<\/p>\n<p># \u5728\u7c7b\u5916&#xff08;\u975e\u65b9\u6cd5\u5185&#xff09;\u4e0d\u80fd\u4f7f\u7528\u65e0\u53c2 super()<br \/>\n# super()  # RuntimeError: super(): no arguments<\/p>\n<hr \/>\n<h4>&#x1f195; \u5341\u56db\u3001AI\/ML \u6846\u67b6\u5b9e\u6218&#xff1a;HuggingFace PreTrainedModel \u7ee7\u627f\u4f53\u7cfb<\/h4>\n<p># \u7b80\u5316\u7684 HuggingFace \u6a21\u578b\u7ee7\u627f\u94fe<br \/>\nfrom abc import abstractmethod<br \/>\nfrom typing import Optional, Dict, Any<br \/>\nimport torch<br \/>\nimport torch.nn as nn<\/p>\n<p># &#061;&#061;&#061;&#061;&#061; \u7b2c\u4e00\u5c42&#xff1a;\u6a21\u578b\u57fa\u7c7b &#061;&#061;&#061;&#061;&#061;<br \/>\nclass PreTrainedModel(nn.Module):<br \/>\n    &#034;&#034;&#034;HF \u6240\u6709\u6a21\u578b\u7684\u6839\u7c7b \u2014\u2014 \u5c55\u793a\u5927\u578b\u9879\u76ee\u4e2d\u7684\u7ee7\u627f\u8bbe\u8ba1&#034;&#034;&#034;<\/p>\n<p>    config_class &#061; None  # \u7c7b\u53d8\u91cf&#xff1a;\u5b50\u7c7b\u5fc5\u987b\u8986\u5199<br \/>\n    base_model_prefix &#061; &#034;model&#034;<\/p>\n<p>    def __init__(self, config):<br \/>\n        super().__init__()<br \/>\n        self.config &#061; config<\/p>\n<p>    &#064;abstractmethod<br \/>\n    def forward(<br \/>\n        self,<br \/>\n        input_ids: Optional[torch.Tensor] &#061; None,<br \/>\n        attention_mask: Optional[torch.Tensor] &#061; None,<br \/>\n        **kwargs<br \/>\n    ) -&gt; Dict[str, torch.Tensor]:<br \/>\n        &#034;&#034;&#034;\u6240\u6709\u5b50\u7c7b\u5fc5\u987b\u5b9e\u73b0 forward&#034;&#034;&#034;<br \/>\n        &#8230;<\/p>\n<p>    def save_pretrained(self, output_dir):<br \/>\n        &#034;&#034;&#034;\u901a\u7528\u4fdd\u5b58\u903b\u8f91 \u2014\u2014 \u6240\u6709\u5b50\u7c7b\u5171\u4eab&#xff0c;\u65e0\u9700\u5404\u81ea\u5b9e\u73b0&#034;&#034;&#034;<br \/>\n        print(f&#034;[Save] Saving {self.__class__.__name__} to {output_dir}&#034;)<br \/>\n        # \u4fdd\u5b58 config.json &#043; model.safetensors<br \/>\n        self.config.save_pretrained(output_dir)<br \/>\n        torch.save(self.state_dict(), f&#034;{output_dir}\/model.pt&#034;)<\/p>\n<p>    &#064;classmethod<br \/>\n    def from_pretrained(cls, pretrained_path):<br \/>\n        &#034;&#034;&#034;\u901a\u7528\u52a0\u8f7d\u903b\u8f91 \u2014\u2014 \u5de5\u5382\u65b9\u6cd5\u6a21\u5f0f&#034;&#034;&#034;<br \/>\n        print(f&#034;[Load] Loading {cls.__name__} from {pretrained_path}&#034;)<br \/>\n        config &#061; cls.config_class.from_pretrained(pretrained_path)<br \/>\n        model &#061; cls(config)<br \/>\n        state_dict &#061; torch.load(f&#034;{pretrained_path}\/model.pt&#034;)<br \/>\n        model.load_state_dict(state_dict)<br \/>\n        return model<\/p>\n<p>    def gradient_checkpointing_enable(self):<br \/>\n        &#034;&#034;&#034;\u53ef\u9009\u80fd\u529b&#xff1a;\u901a\u8fc7 mixin \u6216\u76f4\u63a5\u5b9a\u4e49&#034;&#034;&#034;<br \/>\n        self.gradient_checkpointing &#061; True<\/p>\n<p># &#061;&#061;&#061;&#061;&#061; \u7b2c\u4e8c\u5c42&#xff1a;\u5177\u4f53\u6a21\u578b &#061;&#061;&#061;&#061;&#061;<br \/>\nclass GPT2Model(PreTrainedModel):<br \/>\n    &#034;&#034;&#034;GPT-2 \u6a21\u578b \u2014\u2014 \u7ee7\u627f\u901a\u7528\u80fd\u529b&#xff0c;\u4e13\u6ce8\u81ea\u8eab\u903b\u8f91&#034;&#034;&#034;<\/p>\n<p>    config_class &#061; GPT2Config  # \u7ed1\u5b9a\u914d\u7f6e\u7c7b<\/p>\n<p>    def __init__(self, config: GPT2Config):<br \/>\n        super().__init__(config)<br \/>\n        # GPT-2 \u7279\u6709\u7684\u5c42\u7ed3\u6784<br \/>\n        self.wte &#061; nn.Embedding(config.vocab_size, config.hidden_size)<br \/>\n        self.wpe &#061; nn.Embedding(config.max_position_embeddings, config.hidden_size)<br \/>\n        self.h &#061; nn.ModuleList([<br \/>\n            GPT2Block(config) for _ in config.num_hidden_layers<br \/>\n        ])<br \/>\n        self.ln_f &#061; nn.LayerNorm(config.hidden_size)<\/p>\n<p>    def forward(self, input_ids, attention_mask&#061;None, **kwargs):<br \/>\n        # GPT-2 \u7279\u6709\u7684\u524d\u5411\u4f20\u64ad\u903b\u8f91<br \/>\n        positions &#061; torch.arange(input_ids.shape[-1])<br \/>\n        hidden_states &#061; self.wte(input_ids) &#043; self.wpe(positions)<\/p>\n<p>        for block in self.h:<br \/>\n            hidden_states &#061; block(hidden_states, attention_mask)<\/p>\n<p>        hidden_states &#061; self.ln_f(hidden_states)<br \/>\n        return {&#034;last_hidden_state&#034;: hidden_states}<\/p>\n<p># &#061;&#061;&#061;&#061;&#061; \u7b2c\u4e09\u5c42&#xff1a;\u5e26\u4efb\u52a1\u5934\u7684\u6d3e\u751f\u7c7b &#061;&#061;&#061;&#061;&#061;<br \/>\nclass GPT2ForSequenceClassification(GPT2Model):<br \/>\n    &#034;&#034;&#034;GPT-2 \u6587\u672c\u5206\u7c7b \u2014\u2014 \u7ee7\u627f GPT2Model&#xff0c;\u6dfb\u52a0\u5206\u7c7b\u5934&#034;&#034;&#034;<\/p>\n<p>    def __init__(self, config):<br \/>\n        super().__init__(config)<br \/>\n        # \u65b0\u589e&#xff1a;\u5206\u7c7b\u5934&#xff08;\u7ec4\u5408&#xff09;<br \/>\n        self.classifier &#061; nn.Linear(config.hidden_size, config.num_labels)<br \/>\n        self.num_labels &#061; config.num_labels<\/p>\n<p>    def forward(self, input_ids, attention_mask&#061;None, labels&#061;None, **kwargs):<br \/>\n        # \u590d\u7528\u7236\u7c7b\u7684 transformer \u7f16\u7801<br \/>\n        outputs &#061; super().forward(input_ids, attention_mask, **kwargs)<br \/>\n        logits &#061; self.classifier(outputs[&#034;last_hidden_state&#034;][:, -1, :])<\/p>\n<p>        loss &#061; None<br \/>\n        if labels is not None:<br \/>\n            loss_fn &#061; nn.CrossEntropyLoss()<br \/>\n            loss &#061; loss_fn(logits, labels)<\/p>\n<p>        return {&#034;loss&#034;: loss, &#034;logits&#034;: logits}<\/p>\n<p># &#061;&#061;&#061;&#061;&#061; \u4f7f\u7528\u793a\u4f8b &#061;&#061;&#061;&#061;&#061;<br \/>\nmodel &#061; GPT2ForSequenceClassification.from_pretrained(&#034;.\/my-gpt2-classifier&#034;)<br \/>\nresult &#061; model(input_ids&#061;torch.tensor([[101, 202, 303]]))<br \/>\nmodel.save_pretrained(&#034;.\/updated-model&#034;)  # \u7ee7\u627f\u81ea PreTrainedModel \u7684\u901a\u7528\u65b9\u6cd5<\/p>\n<p>\u8fd9\u4e2a\u7ee7\u627f\u4f53\u7cfb\u7684\u8bbe\u8ba1\u4eae\u70b9&#xff1a;<\/p>\n<table>\n<tr>\u5c42\u6b21\u7c7b\u804c\u8d23\u7ee7\u627f\u7684\u4ef7\u503c<\/tr>\n<tbody>\n<tr>\n<td>L1<\/td>\n<td>PreTrainedModel<\/td>\n<td>\u901a\u7528\u80fd\u529b&#xff08;save\/load\/checkpoint&#xff09;<\/td>\n<td>\u5b50\u7c7b\u514d\u8d39\u83b7\u5f97<\/td>\n<\/tr>\n<tr>\n<td>L2<\/td>\n<td>GPT2Model<\/td>\n<td>\u6a21\u578b\u7279\u6709\u7ed3\u6784<\/td>\n<td>\u590d\u7528 L1&#xff0c;\u4e13\u6ce8\u81ea\u8eab<\/td>\n<\/tr>\n<tr>\n<td>L3<\/td>\n<td>GPT2ForSeqCls<\/td>\n<td>\u4efb\u52a1\u5934<\/td>\n<td>\u590d\u7528 L1&#043;L2&#xff0c;\u53ea\u52a0\u5206\u7c7b\u5934<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<hr \/>\n<h3 id=\"19.\u4ecb\u7ecd\u4e00\u4e0bPython\u4e2d\u7684\u591a\u6001\uff08Polymorphism\uff09\u601d\u60f3\" style=\"background-color:transparent\">\u4ecb\u7ecd\u4e00\u4e0bPython\u4e2d\u7684\u591a\u6001&#xff08;Polymorphism&#xff09;\u601d\u60f3<\/h3>\n<p>\u591a\u6001&#xff08;Polymorphism&#xff09;\u00a0\u662f\u9762\u5411\u5bf9\u8c61\u7f16\u7a0b&#xff08;OOP&#xff09;\u7684\u6838\u5fc3\u6982\u5ff5\u4e4b\u4e00&#xff0c;\u6307\u7684\u662f\u540c\u4e00\u64cd\u4f5c\u4f5c\u7528\u4e8e\u4e0d\u540c\u5bf9\u8c61\u65f6&#xff0c;\u80fd\u591f\u4ea7\u751f\u4e0d\u540c\u7684\u89e3\u91ca\u548c\u884c\u4e3a\u3002\u7b80\u5355\u6765\u8bf4&#xff0c;\u591a\u6001\u5141\u8bb8\u6211\u4eec\u5728\u4e0d\u8003\u8651\u5bf9\u8c61\u5177\u4f53\u7c7b\u578b\u7684\u60c5\u51b5\u4e0b&#xff0c;\u5bf9\u4e0d\u540c\u7c7b\u578b\u7684\u5bf9\u8c61\u6267\u884c\u76f8\u540c\u7684\u64cd\u4f5c\u3002\u5728 Python \u4e2d&#xff0c;\u591a\u6001\u6027\u901a\u8fc7\u52a8\u6001\u7c7b\u578b\u548c\u7075\u6d3b\u7684\u5bf9\u8c61\u6a21\u578b\u5f97\u4ee5\u5b9e\u73b0\u3002<\/p>\n<h4>\u4e00\u3001\u4ec0\u4e48\u662f\u591a\u6001&#xff1f;<\/h4>\n<h5>1. \u5b9a\u4e49<\/h5>\n<ul>\n<li>\u591a\u6001\u6027&#xff08;Polymorphism&#xff09;&#xff1a;\u6e90\u81ea\u5e0c\u814a\u8bed&#xff0c;\u610f\u4e3a&#034;\u591a\u79cd\u5f62\u5f0f&#034;\u3002\u5728\u7f16\u7a0b\u4e2d&#xff0c;\u5b83\u6307\u7684\u662f\u540c\u4e00\u64cd\u4f5c\u5728\u4e0d\u540c\u5bf9\u8c61\u4e0a\u5177\u6709\u4e0d\u540c\u7684\u884c\u4e3a\u3002<\/li>\n<\/ul>\n<h5>2. \u591a\u6001\u7684\u7c7b\u578b&#xff08;&#x1f195; \u8865\u5145\u5b8c\u6574\u5206\u7c7b&#xff09;<\/h5>\n<p>\u591a\u6001\u5728\u7f16\u7a0b\u8bed\u8a00\u7406\u8bba\u4e2d\u6709\u4e09\u79cd\u57fa\u672c\u5f62\u5f0f&#xff1a;<\/p>\n<table>\n<tr>\u591a\u6001\u7c7b\u578b\u5b9e\u73b0\u65b9\u5f0fPython \u652f\u6301\u60c5\u51b5<\/tr>\n<tbody>\n<tr>\n<td>Ad-hoc \u591a\u6001&#xff08;\u7279\u6b8a\u591a\u6001&#xff09;<\/td>\n<td>\u65b9\u6cd5\u91cd\u8f7d\u3001\u8fd0\u7b97\u7b26\u91cd\u8f7d<\/td>\n<td>\u8fd0\u7b97\u7b26\u91cd\u8f7d \u2705&#xff1b;\u65b9\u6cd5\u91cd\u8f7d\u7528\u9ed8\u8ba4\u53c2\u6570\u6a21\u62df \u2705<\/td>\n<\/tr>\n<tr>\n<td>\u5b50\u7c7b\u578b\u591a\u6001&#xff08;Subtype&#xff09;<\/td>\n<td>\u7ee7\u627f &#043; \u65b9\u6cd5\u91cd\u5199<\/td>\n<td>\u6838\u5fc3 \u2705<\/td>\n<\/tr>\n<tr>\n<td>\u53c2\u6570\u591a\u6001&#xff08;Parametric&#xff09;<\/td>\n<td>\u6cdb\u578b \/ TypeVar<\/td>\n<td>typing.Generic \u2705<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>\u6b64\u5916\u8fd8\u6709\u4e24\u79cd\u91cd\u8981\u7684\u53d8\u4f53&#xff1a;<\/p>\n<table>\n<tr>\u591a\u6001\u7c7b\u578b\u5b9e\u73b0\u65b9\u5f0fPython \u652f\u6301\u60c5\u51b5<\/tr>\n<tbody>\n<tr>\n<td>\u6cdb\u578b\u51fd\u6570\u591a\u6001<\/td>\n<td>singledispatch&#xff08;\u6309\u7c7b\u578b\u5206\u6d3e\u51fd\u6570&#xff09;<\/td>\n<td>functools.singledispatch \u2705<\/td>\n<\/tr>\n<tr>\n<td>\u7ed3\u6784\u5316\u5b50\u7c7b\u578b<\/td>\n<td>Protocol&#xff08;\u7ed3\u6784\u5339\u914d\u800c\u975e\u7ee7\u627f&#xff09;<\/td>\n<td>typing.Protocol \u2705 (3.8&#043;)<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h4>\u4e8c\u3001Python \u4e2d\u7684\u591a\u6001\u5b9e\u73b0<\/h4>\n<h5>1. \u52a8\u6001\u7c7b\u578b\u548c\u9e2d\u5b50\u7c7b\u578b<\/h5>\n<ul>\n<li>\u52a8\u6001\u7c7b\u578b&#xff1a;Python \u662f\u52a8\u6001\u7c7b\u578b\u8bed\u8a00&#xff0c;\u53d8\u91cf\u7684\u7c7b\u578b\u5728\u8fd0\u884c\u65f6\u786e\u5b9a\u3002\u8fd9\u4f7f\u5f97\u591a\u6001\u6027\u66f4\u81ea\u7136\u3002<\/li>\n<li>\u9e2d\u5b50\u7c7b\u578b&#xff08;Duck Typing&#xff09;&#xff1a;\u53ea\u8981\u5bf9\u8c61\u5177\u6709\u6240\u9700\u7684\u65b9\u6cd5\u6216\u5c5e\u6027&#xff0c;\u5c31\u53ef\u4ee5\u4f7f\u7528&#xff0c;\u65e0\u9700\u5173\u5fc3\u5bf9\u8c61\u7684\u5177\u4f53\u7c7b\u578b\u3002<\/li>\n<\/ul>\n<p>\u793a\u4f8b&#xff1a;<\/p>\n<p>class Dog:<br \/>\n    def speak(self):<br \/>\n        return &#034;Woof!&#034;<\/p>\n<p>class Cat:<br \/>\n    def speak(self):<br \/>\n        return &#034;Meow!&#034;<\/p>\n<p>class Duck:<br \/>\n    def speak(self):<br \/>\n        return &#034;Quack!&#034;<\/p>\n<p>def animal_speak(animal):<br \/>\n    return animal.speak()<\/p>\n<p>animals &#061; [Dog(), Cat(), Duck()]<br \/>\nfor animal in animals:<br \/>\n    print(animal_speak(animal))<\/p>\n<p>\u8f93\u51fa&#xff1a;<\/p>\n<p>Woof!<br \/>\nMeow!<br \/>\nQuack!<\/p>\n<ul>\n<li>\u89e3\u91ca&#xff1a;animal_speak\u00a0\u51fd\u6570\u53ef\u4ee5\u63a5\u53d7\u4efb\u4f55\u5177\u6709\u00a0speak\u00a0\u65b9\u6cd5\u7684\u5bf9\u8c61&#xff0c;\u800c\u4e0d\u5173\u5fc3\u5176\u5177\u4f53\u7c7b\u578b\u3002\u8fd9\u5c31\u662f\u9e2d\u5b50\u7c7b\u578b\u7684\u4f53\u73b0\u3002<\/li>\n<\/ul>\n<h5>2. \u7ee7\u627f\u548c\u65b9\u6cd5\u91cd\u5199<\/h5>\n<ul>\n<li>\u7ee7\u627f&#xff1a;\u5b50\u7c7b\u7ee7\u627f\u7236\u7c7b\u7684\u65b9\u6cd5\u548c\u5c5e\u6027\u3002<\/li>\n<li>\u65b9\u6cd5\u91cd\u5199&#xff08;Override&#xff09;&#xff1a;\u5b50\u7c7b\u53ef\u4ee5\u91cd\u5199\u7236\u7c7b\u7684\u65b9\u6cd5&#xff0c;\u5b9e\u73b0\u4e0d\u540c\u7684\u884c\u4e3a\u3002<\/li>\n<\/ul>\n<p>\u793a\u4f8b&#xff1a;<\/p>\n<p>class Animal:<br \/>\n    def speak(self):<br \/>\n        raise NotImplementedError(&#034;Subclasses must implement this method.&#034;)<\/p>\n<p>class Dog(Animal):<br \/>\n    def speak(self):<br \/>\n        return &#034;Woof!&#034;<\/p>\n<p>class Cat(Animal):<br \/>\n    def speak(self):<br \/>\n        return &#034;Meow!&#034;<\/p>\n<p>def animal_speak(animal):<br \/>\n    return animal.speak()<\/p>\n<p>animals &#061; [Dog(), Cat()]<br \/>\nfor animal in animals:<br \/>\n    print(animal_speak(animal))<\/p>\n<p>\u8f93\u51fa&#xff1a;<\/p>\n<p>Woof!<br \/>\nMeow!<\/p>\n<ul>\n<li>\u89e3\u91ca&#xff1a;Animal\u00a0\u7c7b\u5b9a\u4e49\u4e86\u4e00\u4e2a\u62bd\u8c61\u65b9\u6cd5\u00a0speak&#xff0c;\u5b50\u7c7b\u00a0Dog\u00a0\u548c\u00a0Cat\u00a0\u5206\u522b\u5b9e\u73b0\u4e86\u81ea\u5df1\u7684\u7248\u672c\u3002animal_speak\u00a0\u51fd\u6570\u8c03\u7528\u65f6&#xff0c;\u6839\u636e\u4f20\u5165\u5bf9\u8c61\u7684\u7c7b\u578b\u6267\u884c\u5bf9\u5e94\u7684\u65b9\u6cd5\u3002<\/li>\n<\/ul>\n<h5>3. \u8fd0\u7b97\u7b26\u91cd\u8f7d<\/h5>\n<ul>\n<li>\u8fd0\u7b97\u7b26\u91cd\u8f7d&#xff1a;\u5728\u7c7b\u4e2d\u5b9a\u4e49\u7279\u6b8a\u65b9\u6cd5&#xff0c;\u5b9e\u73b0\u5bf9\u5185\u7f6e\u8fd0\u7b97\u7b26\u7684\u91cd\u8f7d\u3002<\/li>\n<\/ul>\n<p>\u793a\u4f8b&#xff1a;<\/p>\n<p>class Vector:<br \/>\n    def __init__(self, x, y):<br \/>\n        self.x &#061; x<br \/>\n        self.y &#061; y<\/p>\n<p>    # \u91cd\u8f7d\u52a0\u6cd5\u8fd0\u7b97\u7b26<br \/>\n    def __add__(self, other):<br \/>\n        return Vector(self.x &#043; other.x, self.y &#043; other.y)<\/p>\n<p>    # \u91cd\u8f7d\u5b57\u7b26\u4e32\u8868\u793a<br \/>\n    def __str__(self):<br \/>\n        return f&#034;Vector({self.x}, {self.y})&#034;<\/p>\n<p>v1 &#061; Vector(2, 3)<br \/>\nv2 &#061; Vector(5, 7)<br \/>\nv3 &#061; v1 &#043; v2<br \/>\nprint(v3)<\/p>\n<p>\u8f93\u51fa&#xff1a;<\/p>\n<p>Vector(7, 10)<\/p>\n<ul>\n<li>\u89e3\u91ca&#xff1a;\u901a\u8fc7\u5b9a\u4e49\u00a0__add__\u00a0\u65b9\u6cd5&#xff0c;\u5b9e\u73b0\u4e86\u00a0Vector\u00a0\u5bf9\u8c61\u7684\u52a0\u6cd5\u8fd0\u7b97\u3002\u8fd9\u662f Python \u7684\u53e6\u4e00\u79cd\u591a\u6001\u5f62\u5f0f\u3002<\/li>\n<\/ul>\n<h4>\u4e09\u3001\u9e2d\u5b50\u7c7b\u578b\u8be6\u89e3<\/h4>\n<h5>1. \u6982\u5ff5<\/h5>\n<ul>\n<li>\u9e2d\u5b50\u7c7b\u578b&#xff1a;\u5982\u679c\u4e00\u53ea\u9e1f\u8d70\u8d77\u6765\u50cf\u9e2d\u5b50\u3001\u6e38\u6cf3\u50cf\u9e2d\u5b50\u3001\u53eb\u58f0\u50cf\u9e2d\u5b50&#xff0c;\u90a3\u4e48\u8fd9\u53ea\u9e1f\u53ef\u4ee5\u88ab\u79f0\u4e3a\u9e2d\u5b50\u3002<\/li>\n<li>\u5728 Python \u4e2d&#xff1a;\u53ea\u8981\u5bf9\u8c61\u5177\u6709\u6240\u9700\u7684\u65b9\u6cd5\u6216\u5c5e\u6027&#xff0c;\u5c31\u53ef\u4ee5\u5c06\u5176\u89c6\u4e3a\u67d0\u79cd\u7c7b\u578b\u3002<\/li>\n<\/ul>\n<p>\u793a\u4f8b&#xff1a;<\/p>\n<p>class Bird:<br \/>\n    def fly(self):<br \/>\n        print(&#034;Bird is flying.&#034;)<\/p>\n<p>class Airplane:<br \/>\n    def fly(self):<br \/>\n        print(&#034;Airplane is flying.&#034;)<\/p>\n<p>class Fish:<br \/>\n    def swim(self):<br \/>\n        print(&#034;Fish is swimming.&#034;)<\/p>\n<p>def lift_off(entity):<br \/>\n    entity.fly()<\/p>\n<p>bird &#061; Bird()<br \/>\nplane &#061; Airplane()<br \/>\nfish &#061; Fish()<\/p>\n<p>lift_off(bird)   # \u8f93\u51fa: Bird is flying.<br \/>\nlift_off(plane)  # \u8f93\u51fa: Airplane is flying.<br \/>\n# lift_off(fish)  # AttributeError: &#039;Fish&#039; object has no attribute &#039;fly&#039;<\/p>\n<ul>\n<li>\u89e3\u91ca&#xff1a;lift_off\u00a0\u51fd\u6570\u53ef\u4ee5\u63a5\u53d7\u4efb\u4f55\u5177\u6709\u00a0fly\u00a0\u65b9\u6cd5\u7684\u5bf9\u8c61\u3002Fish\u00a0\u5bf9\u8c61\u7531\u4e8e\u6ca1\u6709\u00a0fly\u00a0\u65b9\u6cd5&#xff0c;\u8c03\u7528\u65f6\u4f1a\u629b\u51fa\u00a0AttributeError\u3002<\/li>\n<\/ul>\n<p>\u00a02. \u5b89\u5168\u7684\u9e2d\u5b50\u7c7b\u578b&#xff1a;\u52a8\u6001\u68c0\u67e5\u4e0e\u5bb9\u9519<\/p>\n<p>\u5de5\u7a0b\u5b9e\u8df5\u4e2d&#xff0c;\u9e2d\u5b50\u7c7b\u578b\u5e38\u914d\u5408\u52a8\u6001\u68c0\u67e5\u4f7f\u7528&#xff1a;<\/p>\n<p>def safe_fly(entity):<br \/>\n    &#034;&#034;&#034;\u5b89\u5168\u7684\u9e2d\u5b50\u7c7b\u578b\u8c03\u7528&#xff1a;\u5148\u68c0\u67e5\u518d\u8c03\u7528&#034;&#034;&#034;<br \/>\n    if hasattr(entity, &#039;fly&#039;):<br \/>\n        entity.fly()<br \/>\n    elif hasattr(entity, &#039;swim&#039;):<br \/>\n        print(f&#034;{entity.__class__.__name__} can&#039;t fly, but can swim.&#034;)<br \/>\n    else:<br \/>\n        raise TypeError(f&#034;{entity.__class__.__name__} is neither flyable nor swimmable&#034;)<\/p>\n<p># \u6216\u8005\u7528 getattr \u63d0\u4f9b\u9ed8\u8ba4\u503c<br \/>\ndef get_or_default(obj, attr, default&#061;None):<br \/>\n    &#034;&#034;&#034;\u83b7\u53d6\u5c5e\u6027&#xff0c;\u4e0d\u5b58\u5728\u65f6\u8fd4\u56de\u9ed8\u8ba4\u503c\u800c\u4e0d\u662f\u629b\u5f02\u5e38&#034;&#034;&#034;<br \/>\n    return getattr(obj, attr, default) or default<\/p>\n<p># EAFP \u98ce\u683c&#xff08;Easier to Ask Forgiveness than Permission&#xff09;<br \/>\ndef eager_fly(entity):<br \/>\n    try:<br \/>\n        entity.fly()<br \/>\n    except AttributeError:<br \/>\n        print(f&#034;{entity.__class__.__name__}: cannot fly&#034;)<\/p>\n<p>\u4e09\u79cd\u9e2d\u5b50\u7c7b\u578b\u5b89\u5168\u7b56\u7565\u5bf9\u6bd4&#xff1a;<\/p>\n<table>\n<tr>\u7b56\u7565\u98ce\u683c\u9002\u7528\u573a\u666f<\/tr>\n<tbody>\n<tr>\n<td>hasattr()\u00a0&#043;\u00a0if<\/td>\n<td>LBYL (Look Before You Leap)<\/td>\n<td>\u6709\u660e\u786e\u7684 fallback \u5206\u652f<\/td>\n<\/tr>\n<tr>\n<td>getattr(obj, attr, default)<\/td>\n<td>LBYL<\/td>\n<td>\u9700\u8981\u63d0\u4f9b\u9ed8\u8ba4\u503c\u7684\u573a\u666f<\/td>\n<\/tr>\n<tr>\n<td>try\/except AttributeError<\/td>\n<td>EAFP<\/td>\n<td>\u5f02\u5e38\u8def\u5f84\u6781\u5c11\u7684\u60c5\u51b5&#xff08;\u66f4\u5feb&#xff09;<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h4>\u56db\u3001\u591a\u6001\u6027\u7684\u4f18\u70b9<\/h4>\n<h5>1. \u63d0\u9ad8\u4ee3\u7801\u7684\u7075\u6d3b\u6027<\/h5>\n<ul>\n<li>\u53ef\u4ee5\u7f16\u5199\u4e0e\u7279\u5b9a\u7c7b\u578b\u65e0\u5173\u7684\u4ee3\u7801&#xff0c;\u5904\u7406\u4e0d\u540c\u7c7b\u578b\u7684\u5bf9\u8c61\u3002<\/li>\n<\/ul>\n<h5>2. \u589e\u5f3a\u4ee3\u7801\u7684\u53ef\u6269\u5c55\u6027<\/h5>\n<ul>\n<li>\u6dfb\u52a0\u65b0\u7c7b\u578b\u7684\u5bf9\u8c61\u65f6&#xff0c;\u65e0\u9700\u4fee\u6539\u73b0\u6709\u4ee3\u7801&#xff0c;\u53ea\u9700\u786e\u4fdd\u65b0\u5bf9\u8c61\u5b9e\u73b0\u4e86\u6240\u9700\u7684\u65b9\u6cd5\u3002<\/li>\n<\/ul>\n<h5>3. \u4ee3\u7801\u91cd\u7528<\/h5>\n<ul>\n<li>\u901a\u8fc7\u591a\u6001&#xff0c;\u53ef\u4ee5\u7f16\u5199\u901a\u7528\u7684\u51fd\u6570\u6216\u65b9\u6cd5&#xff0c;\u907f\u514d\u91cd\u590d\u4ee3\u7801\u3002<\/li>\n<\/ul>\n<h4>\u4e94\u3001\u62bd\u8c61\u57fa\u7c7b&#xff08;Abstract Base Class&#xff09;<\/h4>\n<ul>\n<li>\u6982\u5ff5&#xff1a;\u62bd\u8c61\u57fa\u7c7b\u5b9a\u4e49\u4e86\u63a5\u53e3\u89c4\u8303&#xff0c;\u5b50\u7c7b\u5fc5\u987b\u5b9e\u73b0\u7279\u5b9a\u7684\u65b9\u6cd5\u3002<\/li>\n<li>\u4f5c\u7528&#xff1a;\u786e\u4fdd\u5b50\u7c7b\u5b9e\u73b0\u5fc5\u8981\u7684\u65b9\u6cd5&#xff0c;\u63d0\u4f9b\u4e00\u81f4\u7684\u63a5\u53e3\u3002<\/li>\n<\/ul>\n<h4>\u793a\u4f8b&#xff1a;<\/h4>\n<p>from abc import ABC, abstractmethod<\/p>\n<p>class Shape(ABC):<br \/>\n    &#064;abstractmethod<br \/>\n    def area(self):<br \/>\n        pass<\/p>\n<p>class Rectangle(Shape):<br \/>\n    def __init__(self, width, height):<br \/>\n        self.width &#061; width<br \/>\n        self.height &#061; height<\/p>\n<p>    def area(self):<br \/>\n        return self.width * self.height<\/p>\n<p>class Circle(Shape):<br \/>\n    def __init__(self, radius):<br \/>\n        self.radius &#061; radius<\/p>\n<p>    def area(self):<br \/>\n        return 3.1416 * self.radius ** 2<\/p>\n<p>shapes &#061; [Rectangle(3, 4), Circle(5)]<br \/>\nfor shape in shapes:<br \/>\n    print(f&#034;Area: {shape.area()}&#034;)<\/p>\n<p>\u8f93\u51fa&#xff1a;<\/p>\n<p>Area: 12<br \/>\nArea: 78.53999999999999<\/p>\n<ul>\n<li>\u89e3\u91ca&#xff1a;Shape\u00a0\u662f\u4e00\u4e2a\u62bd\u8c61\u57fa\u7c7b&#xff0c;\u5b9a\u4e49\u4e86\u00a0area\u00a0\u65b9\u6cd5\u3002Rectangle\u00a0\u548c\u00a0Circle\u00a0\u5b9e\u73b0\u4e86\u8be5\u65b9\u6cd5\u3002\u901a\u8fc7\u591a\u6001&#xff0c;\u53ef\u4ee5\u7edf\u4e00\u5904\u7406\u4e0d\u540c\u5f62\u72b6\u7684\u9762\u79ef\u8ba1\u7b97\u3002<\/li>\n<\/ul>\n<h4>\u516d\u3001Python \u4e2d\u4e0d\u652f\u6301\u65b9\u6cd5\u91cd\u8f7d<\/h4>\n<ul>\n<li>\u8bf4\u660e&#xff1a;\u5728 Python \u4e2d&#xff0c;\u65b9\u6cd5\u91cd\u8f7d&#xff08;\u76f8\u540c\u65b9\u6cd5\u540d&#xff0c;\u4e0d\u540c\u53c2\u6570&#xff09;\u5e76\u4e0d\u88ab\u652f\u6301\u3002\u540e\u5b9a\u4e49\u7684\u65b9\u6cd5\u4f1a\u8986\u76d6\u5148\u524d\u7684\u65b9\u6cd5\u3002<\/li>\n<li>\u66ff\u4ee3\u65b9\u6848&#xff1a;\u4f7f\u7528\u9ed8\u8ba4\u53c2\u6570\u6216\u53ef\u53d8\u53c2\u6570\u3002<\/li>\n<\/ul>\n<h4>\u793a\u4f8b&#xff1a;<\/h4>\n<p>class MathOperations:<br \/>\n    def multiply(self, x, y, z&#061;None):<br \/>\n        if z is not None:<br \/>\n            return x * y * z<br \/>\n        else:<br \/>\n            return x * y<\/p>\n<p>math_ops &#061; MathOperations()<br \/>\nprint(math_ops.multiply(2, 3))       # \u8f93\u51fa: 6<br \/>\nprint(math_ops.multiply(2, 3, 4))    # \u8f93\u51fa: 24<\/p>\n<ul>\n<li>\u89e3\u91ca&#xff1a;\u901a\u8fc7\u4f7f\u7528\u9ed8\u8ba4\u53c2\u6570&#xff0c;\u5b9e\u73b0\u7c7b\u4f3c\u65b9\u6cd5\u91cd\u8f7d\u7684\u6548\u679c\u3002<\/li>\n<\/ul>\n<p>&#x1f195;\u00a0\u8865\u5145&#xff1a;typing.overload\u00a0\u2014\u2014 \u9759\u6001\u7c7b\u578b\u5c42\u9762\u7684\u65b9\u6cd5\u91cd\u8f7d<\/p>\n<p>\u867d\u7136\u8fd0\u884c\u65f6\u4e0d\u652f\u6301\u91cd\u8f7d&#xff0c;\u4f46 Python 3.5&#043; \u7684\u00a0typing.overload\u00a0\u5141\u8bb8\u5728\u9759\u6001\u7c7b\u578b\u68c0\u67e5\u65f6\u58f0\u660e\u591a\u4e2a\u7b7e\u540d&#xff1a;<\/p>\n<p>from typing import overload, Union<\/p>\n<p>class Processor:<br \/>\n    &#064;overload<br \/>\n    def process(self, data: str) -&gt; str: &#8230;<br \/>\n    &#064;overload<br \/>\n    def process(self, data: list[str]) -&gt; list[str]: &#8230;<br \/>\n    &#064;overload<br \/>\n    def process(self, data: None) -&gt; None: &#8230;<\/p>\n<p>    def process(self, data):<br \/>\n        if isinstance(data, str):<br \/>\n            return data.upper()<br \/>\n        elif isinstance(data, list):<br \/>\n            return [item.upper() for item in data]<br \/>\n        elif data is None:<br \/>\n            return None<br \/>\n        raise TypeError(&#034;Unsupported type&#034;)<\/p>\n<p># mypy \/ pyright \u80fd\u6b63\u786e\u63a8\u65ad\u6bcf\u79cd\u8c03\u7528\u7684\u8fd4\u56de\u7c7b\u578b<br \/>\np &#061; Processor()<br \/>\nresult1: str &#061; p.process(&#034;hello&#034;)        # \u7c7b\u578b\u68c0\u67e5\u901a\u8fc7<br \/>\nresult2: list[str] &#061; p.process([&#034;a&#034;])    # \u7c7b\u578b\u68c0\u67e5\u901a\u8fc7<br \/>\nresult3: None &#061; p.process(None)          # \u7c7b\u578b\u68c0\u67e5\u901a\u8fc7<\/p>\n<p>\u6ce8\u610f&#xff1a;overload\u00a0\u53ea\u5f71\u54cd\u7c7b\u578b\u68c0\u67e5\u5668\u7684\u63a8\u65ad&#xff0c;\u8fd0\u884c\u65f6\u4ecd\u7136\u53ea\u6709\u4e00\u4e2a\u00a0process\u00a0\u65b9\u6cd5\u3002\u5b83\u662f\u7ed9 IDE \u548c mypy \u7528\u7684&#034;\u7c7b\u578b\u6ce8\u89e3\u91cd\u8f7d&#034;\u3002<\/p>\n<h4>\u4e03\u3001\u65b9\u6cd5\u89e3\u6790\u987a\u5e8f&#xff08;MRO&#xff09;\u5728\u591a\u6001\u4e2d\u7684\u4f5c\u7528<\/h4>\n<ul>\n<li>MRO&#xff08;Method Resolution Order&#xff09;&#xff1a;\u5728\u591a\u91cd\u7ee7\u627f\u4e2d&#xff0c;Python \u6309\u7167 MRO \u51b3\u5b9a\u8c03\u7528\u54ea\u4e2a\u7c7b\u7684\u65b9\u6cd5\u3002<\/li>\n<li>\u591a\u6001\u4e0e MRO&#xff1a;\u5f53\u5b50\u7c7b\u7ee7\u627f\u591a\u4e2a\u7236\u7c7b&#xff0c;\u4e14\u7236\u7c7b\u4e2d\u6709\u540c\u540d\u65b9\u6cd5\u65f6&#xff0c;MRO \u51b3\u5b9a\u4e86\u65b9\u6cd5\u7684\u8c03\u7528\u987a\u5e8f\u3002<\/li>\n<\/ul>\n<h4>\u793a\u4f8b&#xff1a;<\/h4>\n<p>class A:<br \/>\n    def do_something(self):<br \/>\n        print(&#034;Method from A&#034;)<\/p>\n<p>class B:<br \/>\n    def do_something(self):<br \/>\n        print(&#034;Method from B&#034;)<\/p>\n<p>class C(B, A):<br \/>\n    pass<\/p>\n<p>c &#061; C()<br \/>\nc.do_something()<br \/>\nprint(C.mro())<\/p>\n<p>\u8f93\u51fa&#xff1a;<\/p>\n<p>Method from B<br \/>\n[&lt;class &#039;__main__.C&#039;&gt;, &lt;class &#039;__main__.B&#039;&gt;, &lt;class &#039;__main__.A&#039;&gt;, &lt;class &#039;object&#039;&gt;]<\/p>\n<ul>\n<li>\u89e3\u91ca&#xff1a;C\u00a0\u7ee7\u627f\u4e86\u00a0B\u00a0\u548c\u00a0A&#xff0c;\u7531\u4e8e\u00a0B\u00a0\u5728\u524d&#xff0c;\u8c03\u7528\u540c\u540d\u65b9\u6cd5\u65f6&#xff0c;B\u00a0\u7684\u65b9\u6cd5\u4f18\u5148\u3002<\/li>\n<\/ul>\n<hr \/>\n<h4>&#x1f195; \u516b\u3001typing.Protocol\u00a0\u2014\u2014 \u7ed3\u6784\u5316\u5b50\u7c7b\u578b\u591a\u6001<\/h4>\n<p>Python 3.8&#043; \u5f15\u5165\u7684\u00a0typing.Protocol\u00a0\u662f\u5bf9\u4f20\u7edf ABC \u591a\u6001\u7684\u91cd\u8981\u8865\u5145\u3002\u5b83\u5b9e\u73b0\u4e86\u7ed3\u6784\u5316\u5b50\u7c7b\u578b (Structural Subtyping)&#xff1a;\u4e0d\u9700\u8981\u663e\u5f0f\u7ee7\u627f&#xff0c;\u53ea\u8981\u7ed3\u6784&#xff08;\u65b9\u6cd5\/\u5c5e\u6027&#xff09;\u5339\u914d\u5373\u53ef\u3002<\/p>\n<h5>ABC vs Protocol \u6838\u5fc3\u5bf9\u6bd4<\/h5>\n<p># &#061;&#061;&#061;&#061;&#061; \u540d\u4e49\u5b50\u7c7b\u578b (Nominal) \u2014\u2014 \u4f20\u7edf ABC &#061;&#061;&#061;&#061;&#061;<br \/>\nfrom abc import ABC, abstractmethod<\/p>\n<p>class Flyer(ABC):<br \/>\n    &#064;abstractmethod<br \/>\n    def fly(self) -&gt; str: &#8230;<\/p>\n<p>class Bird(Flyer):       # \u5fc5\u987b\u663e\u5f0f\u7ee7\u627f Flyer<br \/>\n    def fly(self) -&gt; str:<br \/>\n        return &#034;Bird flies&#034;<\/p>\n<p>def let_it_fly(flyer: Flyer):  # \u53ea\u63a5\u53d7 Flyer \u53ca\u5176\u5b50\u7c7b<br \/>\n    return flyer.fly()<\/p>\n<p>let_it_fly(Bird())       # \u2705<br \/>\nlet_it_fly(Airplane())   # \u274c Airplane \u6ca1\u6709\u7ee7\u627f Flyer<\/p>\n<p># &#061;&#061;&#061;&#061;&#061; \u7ed3\u6784\u5316\u5b50\u7c7b\u578b (Structural) \u2014\u2014 Protocol &#061;&#061;&#061;&#061;&#061;<br \/>\nfrom typing import Protocol, runtime_checkable<\/p>\n<p>&#064;runtime_checkable<br \/>\nclass Flyable(Protocol):  # Protocol \u662f\u63a5\u53e3\u7ea6\u5b9a&#xff0c;\u4e0d\u662f\u57fa\u7c7b<br \/>\n    def fly(self) -&gt; str: &#8230;<\/p>\n<p>class Bird:<br \/>\n    def fly(self) -&gt; str:<br \/>\n        return &#034;Bird flies&#034;<\/p>\n<p>class Airplane:          # \u65e0\u9700\u7ee7\u627f\u4efb\u4f55\u4e1c\u897f&#xff01;<br \/>\n    def fly(self) -&gt; str:<br \/>\n        return &#034;Airplane flies&#034;<\/p>\n<p>def let_it_fly_cool(entity: Flyable):  # \u63a5\u53d7\u4efb\u4f55\u6709 fly() \u65b9\u6cd5\u7684\u5bf9\u8c61<br \/>\n    return entity.fly()<\/p>\n<p>let_it_fly_cool(Bird())      # \u2705 \u7ed3\u6784\u5339\u914d<br \/>\nlet_it_fly_cool(Airplane())  # \u2705 \u7ed3\u6784\u5339\u914d<\/p>\n<h5>\u5b8c\u6574\u5bf9\u6bd4\u8868<\/h5>\n<table>\n<tr>\u7ef4\u5ea6ABC (\u540d\u4e49\u5b50\u7c7b\u578b)Protocol (\u7ed3\u6784\u5316\u5b50\u7c7b\u578b)<\/tr>\n<tbody>\n<tr>\n<td>\u5339\u914d\u4f9d\u636e<\/td>\n<td>\u7ee7\u627f\u5173\u7cfb&#xff08;is-a&#xff09;<\/td>\n<td>\u7ed3\u6784\u517c\u5bb9&#xff08;has-a-method&#xff09;<\/td>\n<\/tr>\n<tr>\n<td>\u662f\u5426\u9700\u8981\u7ee7\u627f<\/td>\n<td>\u2705 \u5fc5\u987b<\/td>\n<td>\u274c \u4e0d\u9700\u8981<\/td>\n<\/tr>\n<tr>\n<td>\u8fd0\u884c\u65f6\u68c0\u67e5<\/td>\n<td>isinstance(x, AbcClass)<\/td>\n<td>\u9700\u8981\u00a0&#064;runtime_checkable\u00a0&#043;\u00a0isinstance<\/td>\n<\/tr>\n<tr>\n<td>\u7c7b\u578b\u68c0\u67e5\u5668<\/td>\n<td>\u539f\u751f\u652f\u6301<\/td>\n<td>mypy\/pyright \u652f\u6301<\/td>\n<\/tr>\n<tr>\n<td>\u9002\u7528\u573a\u666f<\/td>\n<td>\u5f3a\u5951\u7ea6\u3001\u6846\u67b6\u57fa\u7c7b<\/td>\n<td>\u89e3\u8026\u3001\u8de8\u5e93\u63a5\u53e3\u3001\u9e2d\u5b50\u7c7b\u578b\u5b89\u5168\u5316<\/td>\n<\/tr>\n<tr>\n<td>\u6027\u80fd\u5f00\u9500<\/td>\n<td>\u4f4e<\/td>\n<td>&#064;runtime_checkable\u00a0\u6709\u5c11\u91cf\u5f00\u9500<\/td>\n<\/tr>\n<tr>\n<td>Python \u7248\u672c<\/td>\n<td>\u5168\u90e8<\/td>\n<td>3.8&#043;<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h5>Protocol \u9ad8\u7ea7\u7528\u6cd5&#xff1a;\u590d\u5408\u534f\u8bae<\/h5>\n<p>from typing import Protocol, TypeVar<\/p>\n<p>T &#061; TypeVar(&#039;T&#039;)<\/p>\n<p>class Serializable(Protocol):<br \/>\n    def to_bytes(self) -&gt; bytes: &#8230;<\/p>\n<p>class Hashable(Protocol):<br \/>\n    def hash(self) -&gt; int: &#8230;<\/p>\n<p># \u590d\u5408\u534f\u8bae&#xff1a;\u540c\u65f6\u6ee1\u8db3\u4e24\u4e2a\u534f\u8bae<br \/>\nclass SerializableAndHashable(Serializable, Hashable, Protocol):<br \/>\n    pass<\/p>\n<p>def cache_and_serialize(obj: SerializableAndHashable) -&gt; bytes:<br \/>\n    &#034;&#034;&#034;\u53ea\u63a5\u53d7\u540c\u65f6\u53ef\u5e8f\u5217\u5316\u548c\u53ef\u54c8\u5e0c\u7684\u5bf9\u8c61&#034;&#034;&#034;<br \/>\n    key &#061; obj.hash()<br \/>\n    print(f&#034;Caching with key&#061;{key}&#034;)<br \/>\n    return obj.to_bytes()<\/p>\n<p>AI \u6846\u67b6\u4e2d\u7684 Protocol \u5e94\u7528&#xff1a;<\/p>\n<p># \u5b9a\u4e49\u6a21\u578b\u63a5\u53e3\u534f\u8bae \u2014\u2014 \u4e0d\u7ed1\u5b9a\u4efb\u4f55\u57fa\u7c7b<br \/>\nclass HasForward(Protocol):<br \/>\n    config: object  # \u9700\u8981\u6709 config \u5c5e\u6027<\/p>\n<p>    def forward(self, x: torch.Tensor) -&gt; torch.Tensor: &#8230;<\/p>\n<p>class HasLoss(Protocol):<br \/>\n    def compute_loss(self, pred: torch.Tensor, target: torch.Tensor) -&gt; torch.Tensor: &#8230;<\/p>\n<p># \u4efb\u4f55\u7b26\u5408\u7ed3\u6784\u7684\u6a21\u578b\u90fd\u53ef\u4ee5\u4f20\u5165&#xff0c;\u65e0\u8bba\u6765\u81ea\u54ea\u4e2a\u5e93<br \/>\ndef train_step(model: HasForward &amp; HasLoss, batch):<br \/>\n    pred &#061; model.forward(batch[&#039;input&#039;])<br \/>\n    loss &#061; model.compute_loss(pred, batch[&#039;target&#039;])<br \/>\n    loss.backward()<br \/>\n    return loss<\/p>\n<hr \/>\n<h4>&#x1f195; \u4e5d\u3001functools.singledispatch\u00a0\u2014\u2014 \u6cdb\u578b\u51fd\u6570\u591a\u6001<\/h4>\n<p>&#064;singledispatch\u00a0\u5b9e\u73b0\u4e86\u4e00\u79cd\u4e0d\u540c\u4e8e\u7ee7\u627f\u7684\u591a\u6001\u5f62\u5f0f&#xff1a;\u6839\u636e\u7b2c\u4e00\u4e2a\u53c2\u6570\u7684\u8fd0\u884c\u65f6\u7c7b\u578b&#xff0c;\u5c06\u51fd\u6570\u8c03\u7528\u5206\u6d3e\u5230\u4e0d\u540c\u7684\u5b9e\u73b0\u3002<\/p>\n<p>from functools import singledispatch<\/p>\n<p>&#064;singledispatch<br \/>\ndef serialize(data):<br \/>\n    &#034;&#034;&#034;\u9ed8\u8ba4\u5b9e\u73b0&#xff1a;\u672a\u77e5\u7c7b\u578b\u8f6c\u5b57\u7b26\u4e32&#034;&#034;&#034;<br \/>\n    return str(data)<\/p>\n<p>&#064;serialize.register(int)<br \/>\ndef _(data: int):<br \/>\n    return f&#034;INT:{data}&#034;<\/p>\n<p>&#064;serialize.register(float)<br \/>\ndef _(data: float):<br \/>\n    return f&#034;FLOAT:{data:.2f}&#034;<\/p>\n<p>&#064;serialize.register(list)<br \/>\ndef _(data: list):<br \/>\n    return f&#034;LIST:[{&#039;,&#039;.join(serialize(x) for x in data)}]&#034;<\/p>\n<p>&#064;serialize.register(dict)<br \/>\ndef _(data: dict):<br \/>\n    pairs &#061; [f&#034;{k}&#061;{serialize(v)}&#034; for k, v in data.items()]<br \/>\n    return f&#034;DICT:{{{&#039;,&#039;.join(pairs)}}}&#034;<\/p>\n<p># &#061;&#061;&#061;&#061;&#061; \u4f7f\u7528 &#061;&#061;&#061;&#061;&#061;<br \/>\nprint(serialize(42))                  # INT:42<br \/>\nprint(serialize(3.14))                # FLOAT:3.14<br \/>\nprint(serialize([1, 2.5, &#034;hello&#034;]))   # LIST:[INT:1,FLOAT:2.5,hello]<br \/>\nprint(serialize({&#034;a&#034;: 1, &#034;b&#034;: [2]}))  # DICT:{a&#061;INT:1,b&#061;LIST:[INT:2]}<\/p>\n<p>\u4e0e\u7ee7\u627f\u591a\u6001\u7684\u5173\u952e\u533a\u522b&#xff1a;<\/p>\n<table>\n<tr>\u7ef4\u5ea6\u7ee7\u627f\u591a\u6001singledispatch \u591a\u6001<\/tr>\n<tbody>\n<tr>\n<td>\u5206\u53d1\u4f9d\u636e<\/td>\n<td>\u5bf9\u8c61\u81ea\u8eab\u7684\u7c7b\u578b&#xff08;self&#xff09;<\/td>\n<td>\u51fd\u6570\u7b2c\u4e00\u4e2a\u53c2\u6570\u7684\u7c7b\u578b<\/td>\n<\/tr>\n<tr>\n<td>\u6269\u5c55\u65b9\u5f0f<\/td>\n<td>\u521b\u5efa\u65b0\u5b50\u7c7b<\/td>\n<td>&#064;func.register(NewType)<\/td>\n<\/tr>\n<tr>\n<td>\u8026\u5408\u5ea6<\/td>\n<td>\u5bf9\u8c61\u9700\u8981\u77e5\u9053\u81ea\u5df1\u7684\u7c7b<\/td>\n<td>\u5bf9\u8c61\u5b8c\u5168\u4e0d\u77e5\u9053\u88ab\u5206\u53d1<\/td>\n<\/tr>\n<tr>\n<td>\u9002\u7528\u573a\u666f<\/td>\n<td>\u884c\u4e3a\u4e3b\u8981\u7531\u5bf9\u8c61\u81ea\u8eab\u51b3\u5b9a<\/td>\n<td>\u540c\u4e00\u6570\u636e\u6709\u591a\u79cd\u8868\u793a\u683c\u5f0f<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>AI\/ML \u4e2d\u7684\u5178\u578b\u5e94\u7528&#xff1a;<\/p>\n<p>&#064;singledispatch<br \/>\ndef to_tensor(data):<br \/>\n    raise TypeError(f&#034;Cannot convert {type(data).__name__} to tensor&#034;)<\/p>\n<p>&#064;to_tensor.register(torch.Tensor)<br \/>\ndef _(data):<br \/>\n    return data  # \u5df2\u7ecf\u662f tensor<\/p>\n<p>&#064;to_tensor.register(np.ndarray)<br \/>\ndef _(data):<br \/>\n    return torch.from_numpy(data)<\/p>\n<p>&#064;to_tensor.register(list)<br \/>\ndef _(data):<br \/>\n    return torch.tensor(data)<\/p>\n<p>&#064;to_tensor.register(pd.DataFrame)<br \/>\ndef _(data):<br \/>\n    return torch.tensor(data.values)<\/p>\n<hr \/>\n<h4>&#x1f195; \u5341\u3001\u53c2\u6570\u591a\u6001 (Parametric Polymorphism) \u2014\u2014 TypeVar \u4e0e Generic<\/h4>\n<p>\u53c2\u6570\u591a\u6001\u662f\u6307\u51fd\u6570\u6216\u7c7b\u5728\u4e0d\u6307\u5b9a\u5177\u4f53\u7c7b\u578b\u7684\u60c5\u51b5\u4e0b\u5de5\u4f5c&#xff0c;\u7531\u8c03\u7528\u8005\u51b3\u5b9a\u7c7b\u578b\u3002<\/p>\n<p>from typing import TypeVar, Generic, List<\/p>\n<p>T &#061; TypeVar(&#039;T&#039;)  # \u7c7b\u578b\u53d8\u91cf<\/p>\n<p># &#061;&#061;&#061;&#061;&#061; \u6cdb\u578b\u51fd\u6570 &#061;&#061;&#061;&#061;&#061;<br \/>\ndef first_item(items: List[T]) -&gt; T:<br \/>\n    &#034;&#034;&#034;\u9002\u7528\u4e8e\u4efb\u4f55\u5143\u7d20\u7c7b\u578b\u7684\u5217\u8868&#034;&#034;&#034;<br \/>\n    return items[0]<\/p>\n<p>int_list &#061; [1, 2, 3]<br \/>\nstr_list &#061; [&#034;a&#034;, &#034;b&#034;, &#034;c&#034;]<\/p>\n<p>a: int &#061; first_item(int_list)    # \u7c7b\u578b\u63a8\u65ad T &#061; int<br \/>\nb: str &#061; first_item(str_list)    # \u7c7b\u578b\u63a8\u65ad T &#061; str<\/p>\n<p># &#061;&#061;&#061;&#061;&#061; \u6cdb\u578b\u7c7b &#061;&#061;&#061;&#061;&#061;<br \/>\nclass Stack(Generic[T]):<br \/>\n    &#034;&#034;&#034;\u7c7b\u578b\u5b89\u5168\u7684\u6808 \u2014\u2014 \u540c\u4e00\u4efd\u4ee3\u7801\u9002\u7528\u4e8e\u4efb\u4f55 T&#034;&#034;&#034;<\/p>\n<p>    def __init__(self):<br \/>\n        self._items: List[T] &#061; []<\/p>\n<p>    def push(self, item: T) -&gt; None:<br \/>\n        self._items.append(item)<\/p>\n<p>    def pop(self) -&gt; T:<br \/>\n        return self._items.pop()<\/p>\n<p>    def peek(self) -&gt; T:<br \/>\n        return self._items[-1]<\/p>\n<p>int_stack &#061; Stack[int]()<br \/>\nint_stack.push(1)<br \/>\nint_stack.push(2)<br \/>\nx: int &#061; int_stack.pop()  # \u7c7b\u578b\u68c0\u67e5\u5668\u77e5\u9053 pop() \u8fd4\u56de int<\/p>\n<p>str_stack &#061; Stack[str]()<br \/>\nstr_stack.push(&#034;hello&#034;)<br \/>\ny: str &#061; str_stack.pop()  # \u7c7b\u578b\u68c0\u67e5\u5668\u77e5\u9053 pop() \u8fd4\u56de str<\/p>\n<p>\u6709\u754c\u7c7b\u578b\u53c2\u6570 (Bounded TypeVar)&#xff1a;<\/p>\n<p>from typing import TypeVar<\/p>\n<p>class Shape:<br \/>\n    def area(self) -&gt; float: &#8230;<\/p>\n<p>class Rectangle(Shape):<br \/>\n    def __init__(self, w, h): self.w, self.h &#061; w, h<br \/>\n    def area(self) -&gt; float: return self.w * self.h<\/p>\n<p>class Circle(Shape):<br \/>\n    def __init__(self, r): self.r &#061; r<br \/>\n    def area(self) -&gt; float: return 3.14 * self.r ** 2<\/p>\n<p>S &#061; TypeVar(&#039;S&#039;, bound&#061;Shape)  # S \u5fc5\u987b\u662f Shape \u6216\u5176\u5b50\u7c7b<\/p>\n<p>def total_area(shapes: list[S]) -&gt; float:<br \/>\n    &#034;&#034;&#034;\u6cdb\u578b\u51fd\u6570 &#043; \u6709\u754c\u7c7b\u578b\u53c2\u6570 &#061; \u7c7b\u578b\u5b89\u5168\u7684\u591a\u6001&#034;&#034;&#034;<br \/>\n    return sum(s.area() for s in shapes)<\/p>\n<p>total_area([Rectangle(3, 4), Circle(5)])  # \u2705 \u8fd4\u56de float<br \/>\n# total_area([1, 2, 3])                   # \u274c \u7c7b\u578b\u9519\u8bef&#xff1a;int \u4e0d\u6ee1\u8db3 bound&#061;Shape<\/p>\n<hr \/>\n<h4 style=\"background-color:transparent\">&#x1f195; \u5341\u4e00\u3001__call__\u00a0\u2014\u2014 \u53ef\u8c03\u7528\u5bf9\u8c61\u591a\u6001<\/h4>\n<p>Python \u4e2d\u4e00\u5207\u7686\u5bf9\u8c61&#xff0c;\u5305\u62ec\u51fd\u6570\u672c\u8eab\u3002__call__\u00a0\u65b9\u6cd5\u8ba9\u4efb\u4f55\u5bf9\u8c61\u90fd\u53ef\u4ee5\u50cf\u51fd\u6570\u4e00\u6837\u88ab\u8c03\u7528&#xff0c;\u8fd9\u662f\u6781\u5176\u5f3a\u5927\u7684\u591a\u6001\u5f62\u5f0f\u3002<\/p>\n<p># &#061;&#061;&#061;&#061;&#061; \u4e0d\u540c\u5f62\u5f0f\u7684&#034;\u53ef\u8c03\u7528\u7269&#034; &#061;&#061;&#061;&#061;&#061;<\/p>\n<p>def plain_function(x):<br \/>\n    return x * 2<\/p>\n<p>class Multiplier:<br \/>\n    &#034;&#034;&#034;\u53ef\u8c03\u7528\u5bf9\u8c61 \u2014\u2014 \u884c\u4e3a\u4e0e\u51fd\u6570\u4e00\u81f4&#034;&#034;&#034;<\/p>\n<p>    def __init__(self, factor):<br \/>\n        self.factor &#061; factor<\/p>\n<p>    def __call__(self, x):<br \/>\n        return x * self.factor<\/p>\n<p>class Exponential:<br \/>\n    &#034;&#034;&#034;\u53e6\u4e00\u4e2a\u53ef\u8c03\u7528\u5bf9\u8c61 \u2014\u2014 \u76f8\u540c\u63a5\u53e3&#xff0c;\u4e0d\u540c\u884c\u4e3a&#034;&#034;&#034;<\/p>\n<p>    def __call__(self, x):<br \/>\n        return x ** 2<\/p>\n<p># &#061;&#061;&#061;&#061;&#061; \u7edf\u4e00\u8c03\u7528 &#061;&#061;&#061;&#061;&#061;<br \/>\ndef apply_operation(value, operation):<br \/>\n    &#034;&#034;&#034;operation \u53ef\u4ee5\u662f\u51fd\u6570\u3001lambda\u3001\u53ef\u8c03\u7528\u5bf9\u8c61 \u2014\u2014 \u591a\u6001&#034;&#034;&#034;<br \/>\n    return operation(value)<\/p>\n<p>print(apply_operation(5, plain_function))   # 10  (\u51fd\u6570)<br \/>\nprint(apply_operation(5, Multiplier(3)))    # 15  (\u53ef\u8c03\u7528\u5bf9\u8c61)<br \/>\nprint(apply_operation(5, Exponential()))    # 25  (\u53ef\u8c03\u7528\u5bf9\u8c61)<br \/>\nprint(apply_operation(5, lambda x: x &#043; 1))  # 6   (lambda)<\/p>\n<p>AI\/ML \u6846\u67b6\u4e2d\u7684\u00a0__call__\u00a0\u591a\u6001&#xff1a;<\/p>\n<p>import torch<br \/>\nimport torch.nn as nn<\/p>\n<p># PyTorch \u4e2d&#xff0c;\u6a21\u578b\u3001\u5c42\u3001\u635f\u5931\u51fd\u6570\u3001\u6fc0\u6d3b\u51fd\u6570\u5168\u90e8\u662f callable<br \/>\nmodel &#061; nn.Linear(10, 5)       # \u5c42<br \/>\nactivation &#061; nn.ReLU()         # \u6fc0\u6d3b\u51fd\u6570<br \/>\nloss_fn &#061; nn.MSELoss()         # \u635f\u5931\u51fd\u6570<\/p>\n<p>x &#061; torch.randn(4, 10)<\/p>\n<p># \u7edf\u4e00\u7684\u8c03\u7528\u63a5\u53e3 \u2014\u2014 \u5168\u90e8\u4f7f\u7528 __call__<br \/>\nx &#061; model(x)          # \u7b49\u4ef7\u4e8e model.__call__(x)<br \/>\nx &#061; activation(x)     # \u7b49\u4ef7\u4e8e activation.__call__(x)<br \/>\n# loss &#061; loss_fn(pred, target)  # \u7b49\u4ef7\u4e8e loss_fn.__call__(pred, target)<\/p>\n<p># &#061;&#061;&#061;&#061;&#061; \u81ea\u5b9a\u4e49 callable \u6a21\u5757 &#061;&#061;&#061;&#061;&#061;<br \/>\nclass Preprocessor(nn.Module):<br \/>\n    &#034;&#034;&#034;\u9884\u5904\u7406 &#043; \u6a21\u578b\u7684\u7ec4\u5408 pipeline&#034;&#034;&#034;<\/p>\n<p>    def __init__(self, vocab_size, embed_dim):<br \/>\n        super().__init__()<br \/>\n        self.embedding &#061; nn.Embedding(vocab_size, embed_dim)<br \/>\n        self.dropout &#061; nn.Dropout(0.1)<\/p>\n<p>    def forward(self, input_ids):<br \/>\n        return self.dropout(self.embedding(input_ids))<\/p>\n<p>    # \u8ba9\u5b83\u65e2\u53ef\u4ee5\u7528 forward() \u4e5f\u53ef\u4ee5\u76f4\u63a5\u8c03\u7528<br \/>\n    __call__ &#061; forward<\/p>\n<p>preprocessor &#061; Preprocessor(vocab_size&#061;30000, embed_dim&#061;768)<br \/>\ntokens &#061; torch.tensor([[1, 2, 3, 4]])<br \/>\nvectors &#061; preprocessor(tokens)  # \u901a\u8fc7 __call__ \u8c03\u7528<\/p>\n<p>__call__\u00a0\u591a\u6001\u7684\u672c\u8d28&#xff1a;<\/p>\n<ul>\n<li>\u51fd\u6570\u3001bound method\u3001lambda\u3001functools.partial\u3001__call__\u00a0\u5bf9\u8c61\u5b9e\u73b0\u4e86\u7edf\u4e00\u7684\u8c03\u7528\u534f\u8bae<\/li>\n<li>callable(obj)\u00a0\u68c0\u67e5\u5bf9\u8c61\u662f\u5426\u53ef\u8c03\u7528<\/li>\n<li>\u8fd9\u662f Python \u4e2d\u6700\u9ad8\u5c42\u6b21\u7684\u591a\u6001\u4e4b\u4e00<\/li>\n<\/ul>\n<hr \/>\n<h4>&#x1f195; \u5341\u4e8c\u3001\u534f\u8bae\u591a\u6001 (Protocol-based Polymorphism) \u6982\u89c8<\/h4>\n<p>Python \u4e2d\u9664\u4e86\u65b9\u6cd5\u8c03\u7528\u591a\u6001&#xff0c;\u8fd8\u6709\u591a\u79cd\u9690\u5f0f\u534f\u8bae\u6784\u6210\u7684\u591a\u6001&#xff1a;<\/p>\n<p># &#061;&#061;&#061;&#061;&#061; 1. \u8fed\u4ee3\u5668\u534f\u8bae &#061;&#061;&#061;&#061;&#061;<br \/>\nclass Counter:<br \/>\n    def __init__(self, limit):<br \/>\n        self.limit &#061; limit<br \/>\n        self.count &#061; 0<\/p>\n<p>    def __iter__(self):<br \/>\n        return self<\/p>\n<p>    def __next__(self):<br \/>\n        if self.count &gt;&#061; self.limit:<br \/>\n            raise StopIteration<br \/>\n        self.count &#043;&#061; 1<br \/>\n        return self.count<\/p>\n<p># for \u5faa\u73af\u4e0d\u5173\u5fc3\u5bf9\u8c61\u7c7b\u578b&#xff0c;\u53ea\u8981\u5b9e\u73b0\u4e86 __iter__\/__next__<br \/>\nfor i in Counter(5):<br \/>\n    print(i, end&#061;&#039; &#039;)  # 1 2 3 4 5<\/p>\n<p># &#061;&#061;&#061;&#061;&#061; 2. \u4e0a\u4e0b\u6587\u7ba1\u7406\u5668\u534f\u8bae &#061;&#061;&#061;&#061;&#061;<br \/>\nclass Timer:<br \/>\n    def __enter__(self):<br \/>\n        import time<br \/>\n        self.start &#061; time.time()<br \/>\n        return self<\/p>\n<p>    def __exit__(self, exc_type, exc_val, exc_tb):<br \/>\n        import time<br \/>\n        elapsed &#061; time.time() &#8211; self.start<br \/>\n        print(f&#034;\\\\nElapsed: {elapsed:.3f}s&#034;)<br \/>\n        return False<\/p>\n<p># with \u8bed\u53e5\u4e0d\u5173\u5fc3\u5bf9\u8c61\u7c7b\u578b&#xff0c;\u53ea\u8981\u5b9e\u73b0\u4e86 __enter__\/__exit__<br \/>\nwith Timer():<br \/>\n    sum(range(10_000_000))<\/p>\n<p># &#061;&#061;&#061;&#061;&#061; 3. \u5bb9\u5668\u534f\u8bae &#061;&#061;&#061;&#061;&#061;<br \/>\nclass SparseVector:<br \/>\n    def __init__(self, data):<br \/>\n        self._data &#061; {i: v for i, v in enumerate(data) if v !&#061; 0}<\/p>\n<p>    def __len__(self):<br \/>\n        return max(self._data) &#043; 1 if self._data else 0<\/p>\n<p>    def __getitem__(self, index):<br \/>\n        return self._data.get(index, 0)<\/p>\n<p>sv &#061; SparseVector([0, 0, 5, 0, 8])<br \/>\nprint(len(sv))      # 5  (__len__)<br \/>\nprint(sv[2])        # 5   (__getitem__)<br \/>\nprint(sv[4])        # 8<br \/>\nprint(sv[0])        # 0   (\u9ed8\u8ba4\u503c)<\/p>\n<p>\u534f\u8bae\u591a\u6001\u7684\u610f\u4e49&#xff1a;Python \u7684\u00a0for\u00a0\/\u00a0with\u00a0\/\u00a0[]\u00a0\/\u00a0len()\u00a0\u7b49\u8bed\u6cd5\u5bf9\u6240\u6709\u5b9e\u73b0\u4e86\u76f8\u5e94\u534f\u8bae\u7684\u5bf9\u8c61\u90fd\u6709\u6548\u2014\u2014\u8fd9\u5c31\u662f\u5185\u7f6e\u7684\u591a\u6001\u57fa\u7840\u8bbe\u65bd\u3002<\/p>\n<hr \/>\n<h4 style=\"background-color:transparent\">&#x1f195; \u5341\u4e09\u3001AI\/ML \u6846\u67b6\u5b9e\u6218&#xff1a;\u591a\u6001\u5728\u63a8\u7406\u5f15\u64ce\u4e2d\u7684\u5e94\u7528<\/h4>\n<p>&#034;&#034;&#034;\u6a21\u62df\u4e00\u4e2a\u591a\u540e\u7aef\u63a8\u7406\u5f15\u64ce \u2014\u2014 \u5c55\u793a\u591a\u6001\u5728\u5b9e\u9645 AI \u5de5\u7a0b\u4e2d\u7684\u7efc\u5408\u8fd0\u7528&#034;&#034;&#034;<br \/>\nfrom abc import ABC, abstractmethod<br \/>\nfrom typing import Protocol, Union<br \/>\nfrom functools import singledispatch<br \/>\nimport numpy as np<\/p>\n<p># &#061;&#061;&#061;&#061;&#061; 1. \u62bd\u8c61\u57fa\u7c7b\u5b9a\u4e49\u63a5\u53e3 &#061;&#061;&#061;&#061;&#061;<br \/>\nclass InferenceBackend(ABC):<br \/>\n    &#034;&#034;&#034;\u63a8\u7406\u540e\u7aef\u7684\u62bd\u8c61\u57fa\u7c7b \u2014\u2014 \u540d\u4e49\u5b50\u7c7b\u578b\u591a\u6001&#034;&#034;&#034;<\/p>\n<p>    &#064;abstractmethod<br \/>\n    def load_model(self, model_path: str) -&gt; None:<br \/>\n        &#8230;<\/p>\n<p>    &#064;abstractmethod<br \/>\n    def infer(self, input_data: np.ndarray) -&gt; np.ndarray:<br \/>\n        &#8230;<\/p>\n<p>    &#064;abstractmethod<br \/>\n    def benchmark(self, iterations: int &#061; 100) -&gt; dict:<br \/>\n        &#8230;<\/p>\n<p># &#061;&#061;&#061;&#061;&#061; 2. \u5177\u4f53\u540e\u7aef\u5b9e\u73b0 &#061;&#061;&#061;&#061;&#061;<br \/>\nclass ONNXBackend(InferenceBackend):<br \/>\n    &#034;&#034;&#034;ONNX Runtime \u540e\u7aef&#034;&#034;&#034;<\/p>\n<p>    def load_model(self, model_path: str):<br \/>\n        print(f&#034;[ONNX] Loading model from {model_path}&#034;)<br \/>\n        self.model &#061; f&#034;onnx_model:{model_path}&#034;<\/p>\n<p>    def infer(self, input_data: np.ndarray):<br \/>\n        print(f&#034;[ONNX] Running inference on shape {input_data.shape}&#034;)<br \/>\n        return input_data * 0.5 &#043; 0.1  # \u6a21\u62df\u63a8\u7406<\/p>\n<p>    def benchmark(self, iterations: int &#061; 100) -&gt; dict:<br \/>\n        return {&#034;backend&#034;: &#034;onnx&#034;, &#034;avg_ms&#034;: 2.3, &#034;iterations&#034;: iterations}<\/p>\n<p>class TensorRTBackend(InferenceBackend):<br \/>\n    &#034;&#034;&#034;TensorRT \u540e\u7aef&#034;&#034;&#034;<\/p>\n<p>    def load_model(self, model_path: str):<br \/>\n        print(f&#034;[TRT] Loading engine from {model_path}&#034;)<br \/>\n        self.engine &#061; f&#034;trt_engine:{model_path}&#034;<\/p>\n<p>    def infer(self, input_data: np.ndarray):<br \/>\n        print(f&#034;[TRT] Running inference on shape {input_data.shape}&#034;)<br \/>\n        return input_data * 0.5 &#043; 0.1<\/p>\n<p>    def benchmark(self, iterations: int &#061; 100) -&gt; dict:<br \/>\n        return {&#034;backend&#034;: &#034;tensorrt&#034;, &#034;avg_ms&#034;: 0.8, &#034;iterations&#034;: iterations}<\/p>\n<p>class VLLMBackend(InferenceBackend):<br \/>\n    &#034;&#034;&#034;vLLM \u540e\u7aef (LLM \u63a8\u7406)&#034;&#034;&#034;<\/p>\n<p>    def load_model(self, model_path: str):<br \/>\n        print(f&#034;[vLLM] Loading LLM from {model_path}&#034;)<br \/>\n        self.llm &#061; f&#034;vllm:{model_path}&#034;<\/p>\n<p>    def infer(self, input_data: np.ndarray):<br \/>\n        print(f&#034;[vLLM] Generating tokens for batch {input_data.shape}&#034;)<br \/>\n        # \u8fd4\u56de token ids<br \/>\n        return np.random.randint(0, 32000, size&#061;(input_data.shape[0], 20))<\/p>\n<p>    def benchmark(self, iterations: int &#061; 100) -&gt; dict:<br \/>\n        return {&#034;backend&#034;: &#034;vllm&#034;, &#034;avg_ms_p3&#034;: 45.2, &#034;iterations&#034;: iterations}<\/p>\n<p># &#061;&#061;&#061;&#061;&#061; 3. \u63a8\u7406\u5f15\u64ce \u2014\u2014 \u4f7f\u7528\u591a\u6001 &#061;&#061;&#061;&#061;&#061;<br \/>\nclass InferenceEngine:<br \/>\n    &#034;&#034;&#034;<br \/>\n    \u591a\u6001\u5e94\u7528\u7684\u7efc\u5408\u5c55\u793a&#xff1a;<br \/>\n    &#8211; \u5b50\u7c7b\u578b\u591a\u6001&#xff1a;\u4e0d\u540c\u540e\u7aef\u7edf\u4e00\u63a5\u53e3<br \/>\n    &#8211; Protocol \u591a\u6001&#xff1a;\u8f93\u5165\u6570\u636e\u53ef\u4ee5\u662f\u5404\u79cd\u683c\u5f0f<br \/>\n    &#8211; singledispatch&#xff1a;\u4e0d\u540c\u8f93\u51fa\u683c\u5f0f\u7684\u5e8f\u5217\u5316<br \/>\n    &#034;&#034;&#034;<\/p>\n<p>    def __init__(self, backend: InferenceBackend):<br \/>\n        self.backend &#061; backend<\/p>\n<p>    def run(self, input_data: np.ndarray) -&gt; np.ndarray:<br \/>\n        &#034;&#034;&#034;\u7edf\u4e00\u5165\u53e3 \u2014\u2014 \u4e0d\u5173\u5fc3\u5177\u4f53\u662f\u54ea\u79cd\u540e\u7aef&#034;&#034;&#034;<br \/>\n        return self.backend.infer(input_data)<\/p>\n<p>    def run_benchmark(self) -&gt; dict:<br \/>\n        return self.backend.benchmark()<\/p>\n<p>    def switch_backend(self, new_backend: InferenceBackend):<br \/>\n        &#034;&#034;&#034;\u5207\u6362\u540e\u7aef \u2014\u2014 \u591a\u6001\u8ba9\u5207\u6362\u96f6\u6210\u672c&#034;&#034;&#034;<br \/>\n        self.backend &#061; new_backend<\/p>\n<p># &#061;&#061;&#061;&#061;&#061; 4. singledispatch&#xff1a;\u8f93\u51fa\u683c\u5f0f\u591a\u6001 &#061;&#061;&#061;&#061;&#061;<br \/>\n&#064;singledispatch<br \/>\ndef format_result(result):<br \/>\n    return str(result)<\/p>\n<p>&#064;format_result.register(np.ndarray)<br \/>\ndef _(result: np.ndarray):<br \/>\n    return f&#034;ndarray(shape&#061;{result.shape}, dtype&#061;{result.dtype})&#034;<\/p>\n<p>&#064;format_result.register(dict)<br \/>\ndef _(result: dict):<br \/>\n    return &#034; | &#034;.join(f&#034;{k}&#061;{v}&#034; for k, v in result.items())<\/p>\n<p># &#061;&#061;&#061;&#061;&#061; 5. \u4f7f\u7528\u793a\u4f8b &#061;&#061;&#061;&#061;&#061;<br \/>\nengine &#061; InferenceEngine(ONNXBackend())<br \/>\nengine.backend.load_model(&#034;model.onnx&#034;)<\/p>\n<p>data &#061; np.random.randn(1, 3, 224, 224)<br \/>\nresult &#061; engine.run(data)<br \/>\nprint(format_result(result))<\/p>\n<p>print(format_result(engine.run_benchmark()))<\/p>\n<p># \u5207\u6362\u5230 TensorRT \u2014\u2014 \u63a5\u53e3\u4e0d\u53d8&#xff0c;\u884c\u4e3a\u6539\u53d8<br \/>\nengine.switch_backend(TensorRTBackend())<br \/>\nengine.backend.load_model(&#034;model.engine&#034;)<br \/>\nresult &#061; engine.run(data)<br \/>\nprint(format_result(result))<br \/>\nprint(format_result(engine.run_benchmark()))<\/p>\n<p>\u8fd9\u4e2a\u6848\u4f8b\u7efc\u5408\u8fd0\u7528\u4e86\u56db\u79cd\u591a\u6001\u5f62\u5f0f&#xff1a;<\/p>\n<table>\n<tr>\u591a\u6001\u5f62\u5f0f\u5728\u6848\u4f8b\u4e2d\u7684\u4f53\u73b0<\/tr>\n<tbody>\n<tr>\n<td>\u5b50\u7c7b\u578b\u591a\u6001 (ABC)<\/td>\n<td>InferenceBackend\u00a0&#043; \u4e09\u4e2a\u5177\u4f53\u540e\u7aef<\/td>\n<\/tr>\n<tr>\n<td>\u7ed3\u6784\u5316\u5b50\u7c7b\u578b (Protocol)<\/td>\n<td>\u8f93\u5165\u6570\u636e\u53ef\u6269\u5c55\u4e3a\u4efb\u4f55\u00a0HasArray\u00a0\u534f\u8bae<\/td>\n<\/tr>\n<tr>\n<td>\u6cdb\u578b\u51fd\u6570\u591a\u6001 (singledispatch)<\/td>\n<td>format_result\u00a0\u6309\u7c7b\u578b\u9009\u62e9\u5e8f\u5217\u5316\u65b9\u5f0f<\/td>\n<\/tr>\n<tr>\n<td>\u534f\u8bae\u591a\u6001<\/td>\n<td>InferenceEngine\u00a0\u5bf9\u540e\u7aef\u7684\u65e0\u611f\u77e5\u5207\u6362<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<hr \/>\n<h2>\u7b2c\u4e09\u90e8\u5206&#xff1a;\u603b\u7ed3 \u2014\u2014 \u4e09\u8282\u5185\u5bb9\u7684\u5173\u8054\u4e0e\u8fdb\u9636\u8def\u7ebf<\/h2>\n<h3>\u4e09\u8005\u7684\u5173\u7cfb\u56fe\u8c31<\/h3>\n<p>\u250c\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2510<br \/>\n\u2502                  \u9762\u5411\u5bf9\u8c61\u7f16\u7a0b (OOP)                    \u2502<br \/>\n\u2502                                                       \u2502<br \/>\n\u2502  \u250c\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2510   \u250c\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2510   \u250c\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2510         \u2502<br \/>\n\u2502  \u2502  \u5c01\u88c5     \u2502   \u2502  \u7ee7\u627f     \u2502   \u2502  \u591a\u6001     \u2502         \u2502<br \/>\n\u2502  \u2502 Encaps.  \u2502   \u2502 Inherit. \u2502   \u2502 Poly.    \u2502         \u2502<br \/>\n\u2502  \u251c\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2524   \u251c\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2524   \u251c\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2524         \u2502<br \/>\n\u2502  \u2502 public   \u2502   \u2502 \u5355\u7ee7\u627f    \u2502   \u2502 \u5b50\u7c7b\u578b    \u2502         \u2502<br \/>\n\u2502  \u2502 private  \u2502   \u2502 \u591a\u91cd\u7ee7\u627f  \u2502   \u2502 \u9e2d\u5b50\u7c7b\u578b  \u2502         \u2502<br \/>\n\u2502  \u2502 protected\u2502   \u2502 \u591a\u5c42\u7ee7\u627f  \u2502   \u2502 \u8fd0\u7b97\u7b26\u91cd\u8f7d\u2502         \u2502<br \/>\n\u2502  \u2502 property \u2502   \u2502 Mixin    \u2502   \u2502 Protocol \u2502         \u2502<br \/>\n\u2502  \u2502 \u63cf\u8ff0\u7b26   \u2502   \u2502 super()  \u2502   \u2502 singledisp\u2502         \u2502<br \/>\n\u2502  \u2502 __slots_\u2502   \u2502 __init_  \u2502   \u2502 \u53c2\u6570\u591a\u6001  \u2502         \u2502<br \/>\n\u2502  \u2502 \u6a21\u5757\u5c01\u88c5  \u2502   \u2502 subclass \u2502   \u2502 __call__ \u2502         \u2502<br \/>\n\u2502  \u2502 dataclass\u2502   \u2502 \u7ec4\u5408\u4f18\u4e8e  \u2502   \u2502 \u534f\u8bae\u591a\u6001  \u2502         \u2502<br \/>\n\u2502  \u2514\u2500\u2500\u2500\u2500\u252c\u2500\u2500\u2500\u2500\u2500\u2518   \u2514\u2500\u2500\u2500\u2500\u252c\u2500\u2500\u2500\u2500\u2500\u2518   \u2514\u2500\u2500\u2500\u2500\u252c\u2500\u2500\u2500\u2500\u2500\u2518         \u2502<br \/>\n\u2502       \u2502              \u2502              \u2502                \u2502<br \/>\n\u2502       \u2514\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u253c\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2518                \u2502<br \/>\n\u2502                      \u25bc                               \u2502<br \/>\n\u2502              \u250c\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2510                        \u2502<br \/>\n\u2502              \u2502  \u8bbe\u8ba1\u6a21\u5f0f     \u2502                        \u2502<br \/>\n\u2502              \u2502 \u5de5\u5382\/\u7b56\u7565\/\u6a21\u677f \u2502                        \u2502<br \/>\n\u2502              \u2502 \u89c2\u5bdf\u8005\/\u547d\u4ee4   \u2502                        \u2502<br \/>\n\u2502              \u2514\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2518                        \u2502<br \/>\n\u2514\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2518<\/p>\n<h3>\u9762\u8bd5\u56de\u7b54\u7b56\u7565\u5efa\u8bae<\/h3>\n<table>\n<tr>\u95ee\u9898\u56de\u7b54\u8981\u70b9\u5bf9\u5e94\u672c\u8282\u5185\u5bb9<\/tr>\n<tbody>\n<tr>\n<td>\u201c\u4ec0\u4e48\u662f\u5c01\u88c5&#xff1f;\u201d<\/td>\n<td>\u547d\u540d\u7ea6\u5b9a &#043; property &#043; \u63cf\u8ff0\u7b26 &#043;\u00a0__slots__\u00a0&#043; \u6a21\u5757\u7ea7\u5c01\u88c5<\/td>\n<td><\/td>\n<\/tr>\n<tr>\n<td>\u201cPython \u7684\u79c1\u6709\u662f\u771f\u6b63\u7684\u79c1\u6709\u5417&#xff1f;\u201d<\/td>\n<td>name mangling &#043; \u201cconsenting adults\u201d \u54f2\u5b66<\/td>\n<td><\/td>\n<\/tr>\n<tr>\n<td>\u201cMRO \u662f\u4ec0\u4e48&#xff1f;\u201d<\/td>\n<td>C3 \u7ebf\u6027\u5316 &#043; \u83f1\u5f62\u7ee7\u627f &#043; super() \u672c\u8d28<\/td>\n<td><\/td>\n<\/tr>\n<tr>\n<td>\u201c\u4ec0\u4e48\u65f6\u5019\u7528 Mixin&#xff1f;\u201d<\/td>\n<td>\u5355\u4e00\u529f\u80fd &#043; \u4e0d\u5355\u72ec\u5b9e\u4f8b\u5316 &#043; \u547d\u540d\u89c4\u8303<\/td>\n<td><\/td>\n<\/tr>\n<tr>\n<td>\u201c\u7ec4\u5408 vs \u7ee7\u627f&#xff1f;\u201d<\/td>\n<td>is-a \u7528\u7ee7\u627f&#xff0c;has-a \u7528\u7ec4\u5408 &#043; \u8106\u5f31\u57fa\u7c7b\u95ee\u9898<\/td>\n<td><\/td>\n<\/tr>\n<tr>\n<td>\u201cPython \u6709\u51e0\u79cd\u591a\u6001&#xff1f;\u201d<\/td>\n<td>\u56db\u79cd&#xff1a;\u5b50\u7c7b\u578b\/\u9e2d\u5b50\/\u53c2\u6570\/\u6cdb\u578b\u51fd\u6570<\/td>\n<td><\/td>\n<\/tr>\n<tr>\n<td>\u201cABC vs Protocol&#xff1f;\u201d<\/td>\n<td>\u540d\u4e49 vs \u7ed3\u6784 &#043; \u8868\u683c\u5bf9\u6bd4<\/td>\n<td><\/td>\n<\/tr>\n<tr>\n<td>\u201c\u5982\u4f55\u5728 AI \u9879\u76ee\u4e2d\u8fd0\u7528 OOP&#xff1f;\u201d<\/td>\n<td>PyTorch\/HF \u7ee7\u627f\u4f53\u7cfb &#043; \u63a8\u7406\u5f15\u64ce\u591a\u6001<\/td>\n<td><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<hr \/>\n<p>\u6587\u6863\u7ed3\u675f\u3002\u672c\u6587\u6863\u5171\u5305\u542b&#xff1a;<\/p>\n<ul>\n<li>\u7b2c17\u8282&#xff1a;11 \u4e2a\u5b50\u8282&#xff08;\u539f\u6587 5 \u4e2a &#043; &#x1f195; 6 \u4e2a\u65b0\u589e&#xff09;<\/li>\n<li>\u7b2c18\u8282&#xff1a;14 \u4e2a\u5b50\u8282&#xff08;\u539f\u6587 7 \u4e2a &#043; &#x1f195; 7 \u4e2a\u65b0\u589e&#xff09;<\/li>\n<li>\u7b2c19\u8282&#xff1a;13 \u4e2a\u5b50\u8282&#xff08;\u539f\u6587 7 \u4e2a &#043; &#x1f195; 6 \u4e2a\u65b0\u589e&#xff09;<\/li>\n<li>\u65b0\u589e AI\/ML \u5b9e\u6218\u6848\u4f8b 4 \u4e2a&#xff08;PyTorch \u5c01\u88c5\u3001HF \u7ee7\u627f\u4f53\u7cfb\u3001\u63a8\u7406\u5f15\u64ce\u591a\u6001\u3001Mixin \u68af\u5ea6 checkpoint&#xff09;<\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>\u4e09\u3001 \u51fd\u6570\u3001\u7c7b\u4e0e\u9762\u5411\u5bf9\u8c61\u7f16\u7a0b (OOP)\u6807\u9898&#xff1a;\u51fd\u6570\u53c2\u6570\u4e0e\u9ad8\u9636\u51fd\u6570\u88c5\u9970\u5668\u4e0e\u95ed\u5305\u9762\u5411\u5bf9\u8c61\u4e09\u5927\u7279\u5f81\u7c7b\u3001\u5bf9\u8c61\u4e0e\u9b54\u672f\u65b9\u6cd5\u5f02\u5e38\u5904\u7406\u4ecb\u7ecd\u4e00\u4e0bPython\u4e2d\u7684\u5c01\u88c5(Encapsulation)\u601d\u60f3\u5c01\u88c5 (Encapsulation) \u5728 Python \u4e2d\u7684\u6982\u5ff5\u5c01\u88c5\u662f\u9762\u5411\u5bf9\u8c61\u7f16\u7a0b&#xff08;OOP&#xff09;\u7684\u56db\u5927\u57fa\u672c\u539f\u5219\u4e4b\u4e00&#xff0c;\u5176\u4ed6\u4e09\u4e2a\u662f\u7ee7\u627f&#xff08;Inheritance&#xff09;\u3001\u591a\u6001&#xff08;Polymorphism&#xff09;\u548c<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[81,190],"topic":[],"class_list":["post-101500","post","type-post","status-publish","format-standard","hentry","category-server","tag-python","tag-190"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v20.3 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Python.\u4e09.\uff08\u4e09\uff09-3. \u9762\u5411\u5bf9\u8c61\u4e09\u5927\u7279\u5f81 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.wsisp.com\/helps\/101500.html\" \/>\n<meta property=\"og:locale\" content=\"zh_CN\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Python.\u4e09.\uff08\u4e09\uff09-3. \u9762\u5411\u5bf9\u8c61\u4e09\u5927\u7279\u5f81 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"og:description\" content=\"\u4e09\u3001 \u51fd\u6570\u3001\u7c7b\u4e0e\u9762\u5411\u5bf9\u8c61\u7f16\u7a0b (OOP)\u6807\u9898&#xff1a;\u51fd\u6570\u53c2\u6570\u4e0e\u9ad8\u9636\u51fd\u6570\u88c5\u9970\u5668\u4e0e\u95ed\u5305\u9762\u5411\u5bf9\u8c61\u4e09\u5927\u7279\u5f81\u7c7b\u3001\u5bf9\u8c61\u4e0e\u9b54\u672f\u65b9\u6cd5\u5f02\u5e38\u5904\u7406\u4ecb\u7ecd\u4e00\u4e0bPython\u4e2d\u7684\u5c01\u88c5(Encapsulation)\u601d\u60f3\u5c01\u88c5 (Encapsulation) \u5728 Python \u4e2d\u7684\u6982\u5ff5\u5c01\u88c5\u662f\u9762\u5411\u5bf9\u8c61\u7f16\u7a0b&#xff08;OOP&#xff09;\u7684\u56db\u5927\u57fa\u672c\u539f\u5219\u4e4b\u4e00&#xff0c;\u5176\u4ed6\u4e09\u4e2a\u662f\u7ee7\u627f&#xff08;Inheritance&#xff09;\u3001\u591a\u6001&#xff08;Polymorphism&#xff09;\u548c\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.wsisp.com\/helps\/101500.html\" \/>\n<meta property=\"og:site_name\" content=\"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"article:published_time\" content=\"2026-09-06T15:25:18+00:00\" \/>\n<meta name=\"author\" content=\"admin\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"\u4f5c\u8005\" \/>\n\t<meta name=\"twitter:data1\" content=\"admin\" \/>\n\t<meta name=\"twitter:label2\" content=\"\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4\" \/>\n\t<meta name=\"twitter:data2\" content=\"28 \u5206\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/101500.html\",\"url\":\"https:\/\/www.wsisp.com\/helps\/101500.html\",\"name\":\"Python.\u4e09.\uff08\u4e09\uff09-3. \u9762\u5411\u5bf9\u8c61\u4e09\u5927\u7279\u5f81 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\",\"isPartOf\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#website\"},\"datePublished\":\"2026-09-06T15:25:18+00:00\",\"dateModified\":\"2026-09-06T15:25:18+00:00\",\"author\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41\"},\"breadcrumb\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/101500.html#breadcrumb\"},\"inLanguage\":\"zh-Hans\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.wsisp.com\/helps\/101500.html\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/101500.html#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"\u9996\u9875\",\"item\":\"https:\/\/www.wsisp.com\/helps\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Python.\u4e09.\uff08\u4e09\uff09--3. \u9762\u5411\u5bf9\u8c61\u4e09\u5927\u7279\u5f81\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#website\",\"url\":\"https:\/\/www.wsisp.com\/helps\/\",\"name\":\"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\",\"description\":\"\u9999\u6e2f\u670d\u52a1\u5668_\u9999\u6e2f\u4e91\u670d\u52a1\u5668\u8d44\u8baf_\u670d\u52a1\u5668\u5e2e\u52a9\u6587\u6863_\u670d\u52a1\u5668\u6559\u7a0b\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.wsisp.com\/helps\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"zh-Hans\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41\",\"name\":\"admin\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"zh-Hans\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery\",\"contentUrl\":\"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery\",\"caption\":\"admin\"},\"sameAs\":[\"http:\/\/wp.wsisp.com\"],\"url\":\"https:\/\/www.wsisp.com\/helps\/author\/admin\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Python.\u4e09.\uff08\u4e09\uff09-3. \u9762\u5411\u5bf9\u8c61\u4e09\u5927\u7279\u5f81 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.wsisp.com\/helps\/101500.html","og_locale":"zh_CN","og_type":"article","og_title":"Python.\u4e09.\uff08\u4e09\uff09-3. \u9762\u5411\u5bf9\u8c61\u4e09\u5927\u7279\u5f81 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","og_description":"\u4e09\u3001 \u51fd\u6570\u3001\u7c7b\u4e0e\u9762\u5411\u5bf9\u8c61\u7f16\u7a0b (OOP)\u6807\u9898&#xff1a;\u51fd\u6570\u53c2\u6570\u4e0e\u9ad8\u9636\u51fd\u6570\u88c5\u9970\u5668\u4e0e\u95ed\u5305\u9762\u5411\u5bf9\u8c61\u4e09\u5927\u7279\u5f81\u7c7b\u3001\u5bf9\u8c61\u4e0e\u9b54\u672f\u65b9\u6cd5\u5f02\u5e38\u5904\u7406\u4ecb\u7ecd\u4e00\u4e0bPython\u4e2d\u7684\u5c01\u88c5(Encapsulation)\u601d\u60f3\u5c01\u88c5 (Encapsulation) \u5728 Python \u4e2d\u7684\u6982\u5ff5\u5c01\u88c5\u662f\u9762\u5411\u5bf9\u8c61\u7f16\u7a0b&#xff08;OOP&#xff09;\u7684\u56db\u5927\u57fa\u672c\u539f\u5219\u4e4b\u4e00&#xff0c;\u5176\u4ed6\u4e09\u4e2a\u662f\u7ee7\u627f&#xff08;Inheritance&#xff09;\u3001\u591a\u6001&#xff08;Polymorphism&#xff09;\u548c","og_url":"https:\/\/www.wsisp.com\/helps\/101500.html","og_site_name":"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","article_published_time":"2026-09-06T15:25:18+00:00","author":"admin","twitter_card":"summary_large_image","twitter_misc":{"\u4f5c\u8005":"admin","\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4":"28 \u5206"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.wsisp.com\/helps\/101500.html","url":"https:\/\/www.wsisp.com\/helps\/101500.html","name":"Python.\u4e09.\uff08\u4e09\uff09-3. \u9762\u5411\u5bf9\u8c61\u4e09\u5927\u7279\u5f81 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","isPartOf":{"@id":"https:\/\/www.wsisp.com\/helps\/#website"},"datePublished":"2026-09-06T15:25:18+00:00","dateModified":"2026-09-06T15:25:18+00:00","author":{"@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41"},"breadcrumb":{"@id":"https:\/\/www.wsisp.com\/helps\/101500.html#breadcrumb"},"inLanguage":"zh-Hans","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.wsisp.com\/helps\/101500.html"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.wsisp.com\/helps\/101500.html#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"\u9996\u9875","item":"https:\/\/www.wsisp.com\/helps"},{"@type":"ListItem","position":2,"name":"Python.\u4e09.\uff08\u4e09\uff09--3. \u9762\u5411\u5bf9\u8c61\u4e09\u5927\u7279\u5f81"}]},{"@type":"WebSite","@id":"https:\/\/www.wsisp.com\/helps\/#website","url":"https:\/\/www.wsisp.com\/helps\/","name":"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","description":"\u9999\u6e2f\u670d\u52a1\u5668_\u9999\u6e2f\u4e91\u670d\u52a1\u5668\u8d44\u8baf_\u670d\u52a1\u5668\u5e2e\u52a9\u6587\u6863_\u670d\u52a1\u5668\u6559\u7a0b","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.wsisp.com\/helps\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"zh-Hans"},{"@type":"Person","@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41","name":"admin","image":{"@type":"ImageObject","inLanguage":"zh-Hans","@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/image\/","url":"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery","contentUrl":"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery","caption":"admin"},"sameAs":["http:\/\/wp.wsisp.com"],"url":"https:\/\/www.wsisp.com\/helps\/author\/admin"}]}},"_links":{"self":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts\/101500","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/comments?post=101500"}],"version-history":[{"count":0,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts\/101500\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/media?parent=101500"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/categories?post=101500"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/tags?post=101500"},{"taxonomy":"topic","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/topic?post=101500"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}