{"id":657,"date":"2026-03-10T14:00:00","date_gmt":"2026-03-10T06:00:00","guid":{"rendered":"https:\/\/mitongxue.cn\/?p=657"},"modified":"2026-07-14T14:24:07","modified_gmt":"2026-07-14T06:24:07","slug":"python%e4%b8%ad%e7%b1%bb%e5%92%8c%e5%af%b9%e8%b1%a1","status":"publish","type":"post","link":"https:\/\/mitongxue.cn\/index.php\/2026\/03\/10\/python%e4%b8%ad%e7%b1%bb%e5%92%8c%e5%af%b9%e8%b1%a1\/","title":{"rendered":"python\u4e2d\u7c7b\u548c\u5bf9\u8c61"},"content":{"rendered":"\n<h2 class=\"wp-block-heading\">\u5b9a\u4e49\u7c7b<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">\u4f7f\u7528 <code>class<\/code> \u5173\u952e\u5b57\u5b9a\u4e49\u7c7b\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>class Dog:<br> &nbsp; &nbsp;pass<br>\u200b<br># \u521b\u5efa\u5b9e\u4f8b<br>my_dog = Dog()<br>print(type(my_dog)) &nbsp;# &lt;class '__main__.Dog'&gt;<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">\u4f7f\u7528 <code>type()<\/code> \u52a8\u6001\u5b9a\u4e49\u7c7b<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\u7c7b\u672c\u8eab\u4e5f\u662f\u5bf9\u8c61\uff0c<code>type()<\/code> \u662f\u521b\u5efa\u7c7b\u7684\u5185\u7f6e\u51fd\u6570\u3002\u53ef\u4ee5\u52a8\u6001\u5730\u521b\u5efa\u7c7b\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># type(\u7c7b\u540d, (\u7236\u7c7b\u5143\u7ec4,), {\u5c5e\u6027\u5b57\u5178})<br>\u200b<br># \u5b9a\u4e49\u65b9\u6cd5<br>def bark(self):<br> &nbsp; &nbsp;print(f\"{self.name} says: Woof!\")<br>\u200b<br># \u52a8\u6001\u521b\u5efa Dog \u7c7b<br>Dog = type('Dog', (), {'name': 'Buddy', 'bark': bark})<br>\u200b<br># \u521b\u5efa\u5b9e\u4f8b<br>my_dog = Dog()<br>print(my_dog.name) &nbsp;# Buddy<br>my_dog.bark() &nbsp; &nbsp; &nbsp; # Buddy says: Woof!<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u53c2\u6570\u8bf4\u660e\uff1a<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u7b2c\u4e00\u4e2a\u53c2\u6570\uff1a\u7c7b\u540d\uff08\u5b57\u7b26\u4e32\uff09<\/li>\n\n\n\n<li>\u7b2c\u4e8c\u4e2a\u53c2\u6570\uff1a\u7ee7\u627f\u7684\u7236\u7c7b\u5143\u7ec4<\/li>\n\n\n\n<li>\u7b2c\u4e09\u4e2a\u53c2\u6570\uff1a\u7c7b\u5c5e\u6027\u548c\u65b9\u6cd5\u7684\u5b57\u5178<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u5b9e\u9645\u5e94\u7528\u573a\u666f\uff1a<\/strong> \u5728 ORM \u6846\u67b6\u6216\u9700\u8981\u6839\u636e\u914d\u7f6e\u52a8\u6001\u751f\u6210\u7c7b\u7684\u573a\u666f\u4e2d\u7ecf\u5e38\u4f7f\u7528\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">\u6784\u9020\u65b9\u6cd5 <code>__init__<\/code><\/h2>\n\n\n\n<p class=\"wp-block-paragraph\"><code>__init__<\/code> \u662f\u7c7b\u7684\u6784\u9020\u65b9\u6cd5\uff0c\u5728\u521b\u5efa\u5bf9\u8c61\u65f6\u81ea\u52a8\u8c03\u7528\uff0c\u7528\u4e8e\u521d\u59cb\u5316\u5bf9\u8c61\u7684\u5c5e\u6027\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>class Dog:<br> &nbsp; &nbsp;def __init__(this, name, age):<br> &nbsp; &nbsp; &nbsp; &nbsp;this.name = name<br> &nbsp; &nbsp; &nbsp; &nbsp;this.age = age<br>\u200b<br># \u521b\u5efa\u5bf9\u8c61\u65f6\u4f20\u5165\u53c2\u6570<br>my_dog = Dog(\"Buddy\", 3)<br>print(my_dog.name) &nbsp;# Buddy<br>print(my_dog.age) &nbsp; # 3<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u6ce8\u610f\uff1a<\/strong> <code>self<\/code> \u4ee3\u8868\u5bf9\u8c61\u672c\u8eab\uff0c\u5fc5\u987b\u662f\u7b2c\u4e00\u4e2a\u53c2\u6570\uff0c\u4f46\u8c03\u7528\u65f6\u4e0d\u9700\u8981\u4f20\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">\u5b9e\u4f8b\u5c5e\u6027\u4e0e\u7c7b\u5c5e\u6027<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">\u5b9e\u4f8b\u5c5e\u6027<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\u6bcf\u4e2a\u5bf9\u8c61\u72ec\u7acb\u7684\u5c5e\u6027\uff0c\u901a\u8fc7 <code>self<\/code> \u5b9a\u4e49\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>class Dog:<br> &nbsp; &nbsp;def __init__(self, name):<br> &nbsp; &nbsp; &nbsp; &nbsp;self.name = name &nbsp;# \u5b9e\u4f8b\u5c5e\u6027<br>\u200b<br>dog1 = Dog(\"Buddy\")<br>dog2 = Dog(\"Max\")<br>\u200b<br>print(dog1.name) &nbsp;# Buddy<br>print(dog2.name) &nbsp;# Max<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">\u7c7b\u5c5e\u6027<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\u6240\u6709\u5bf9\u8c61\u5171\u4eab\u7684\u5c5e\u6027\uff0c\u5728\u7c7b\u5185\u90e8\u76f4\u63a5\u5b9a\u4e49\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>class Dog:<br> &nbsp; &nbsp;species = \"Canis familiaris\" &nbsp;# \u7c7b\u5c5e\u6027<br>\u200b<br> &nbsp; &nbsp;def __init__(self, name):<br> &nbsp; &nbsp; &nbsp; &nbsp;self.name = name<br>\u200b<br>dog1 = Dog(\"Buddy\")<br>dog2 = Dog(\"Max\")<br>\u200b<br>print(dog1.species) &nbsp;# Canis familiaris<br>print(dog2.species) &nbsp;# Canis familiaris<br>\u200b<br># \u4fee\u6539\u7c7b\u5c5e\u6027\uff08\u901a\u8fc7\u7c7b\u540d\uff09<br>Dog.species = \"Canis lupus\"<br>print(dog1.species) &nbsp;# Canis lupus<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u8bbf\u95ee\u89c4\u5219\uff1a<\/strong> \u5b9e\u4f8b\u5c5e\u6027\u901a\u8fc7 <code>self<\/code> \u8bbf\u95ee\uff0c\u7c7b\u5c5e\u6027\u901a\u8fc7 <code>\u7c7b\u540d<\/code> \u6216 <code>self<\/code> \u8bbf\u95ee\u3002<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\u5b9e\u4f8b\u65b9\u6cd5<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">\u5b9a\u4e49\u5728\u7c7b\u4e2d\u7684\u51fd\u6570\uff0c\u7b2c\u4e00\u4e2a\u53c2\u6570\u5fc5\u987b\u662f <code>self<\/code>\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>class Dog:<br> &nbsp; &nbsp;def __init__(self, name):<br> &nbsp; &nbsp; &nbsp; &nbsp;self.name = name<br>\u200b<br> &nbsp; &nbsp;def bark(self):<br> &nbsp; &nbsp; &nbsp; &nbsp;print(f\"{self.name} says: Woof!\")<br>\u200b<br> &nbsp; &nbsp;def introduce(self):<br> &nbsp; &nbsp; &nbsp; &nbsp;self.bark() &nbsp;# \u65b9\u6cd5\u5185\u8c03\u7528\u5176\u4ed6\u65b9\u6cd5<br> &nbsp; &nbsp; &nbsp; &nbsp;print(f\"My name is {self.name}\")<br>\u200b<br>my_dog = Dog(\"Buddy\")<br>my_dog.bark() &nbsp; &nbsp; &nbsp; # Buddy says: Woof!<br>my_dog.introduce() &nbsp;# Buddy says: Woof! My name is Buddy<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\u7c7b\u65b9\u6cd5\u4e0e\u9759\u6001\u65b9\u6cd5<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">\u7c7b\u65b9\u6cd5 <code>@classmethod<\/code><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\u7b2c\u4e00\u4e2a\u53c2\u6570\u662f <code>cls<\/code>\uff0c\u4ee3\u8868\u7c7b\u672c\u8eab\uff0c\u53ef\u4ee5\u8bbf\u95ee\u6216\u4fee\u6539\u7c7b\u5c5e\u6027\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>class Dog:<br> &nbsp; &nbsp;count = 0 &nbsp;# \u7c7b\u5c5e\u6027\uff1a\u8bb0\u5f55\u521b\u5efa\u4e86\u591a\u5c11\u53ea\u72d7<br>\u200b<br> &nbsp; &nbsp;def __init__(self, name):<br> &nbsp; &nbsp; &nbsp; &nbsp;self.name = name<br> &nbsp; &nbsp; &nbsp; &nbsp;Dog.count += 1<br>\u200b<br> &nbsp; &nbsp;@classmethod<br> &nbsp; &nbsp;def get_count(cls):<br> &nbsp; &nbsp; &nbsp; &nbsp;return cls.count<br>\u200b<br>dog1 = Dog(\"Buddy\")<br>dog2 = Dog(\"Max\")<br>\u200b<br>print(Dog.get_count()) &nbsp;# 2<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">\u9759\u6001\u65b9\u6cd5 <code>@staticmethod<\/code><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\u4e0d\u63a5\u6536 <code>self<\/code> \u6216 <code>cls<\/code>\uff0c\u4e0e\u666e\u901a\u51fd\u6570\u7c7b\u4f3c\uff0c\u53ea\u662f\u7ec4\u7ec7\u5728\u7c7b\u4e2d\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>class MathUtils:<br> &nbsp; &nbsp;@staticmethod<br> &nbsp; &nbsp;def add(a, b):<br> &nbsp; &nbsp; &nbsp; &nbsp;return a + b<br>\u200b<br> &nbsp; &nbsp;@staticmethod<br> &nbsp; &nbsp;def is_even(n):<br> &nbsp; &nbsp; &nbsp; &nbsp;return n % 2 == 0<br>\u200b<br>print(MathUtils.add(3, 5)) &nbsp; &nbsp; &nbsp;# 8<br>print(MathUtils.is_even(4)) &nbsp; &nbsp; # True<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u5bf9\u6bd4\uff1a<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th class=\"has-text-align-left\" data-align=\"left\">\u65b9\u6cd5\u7c7b\u578b<\/th><th class=\"has-text-align-left\" data-align=\"left\">\u88c5\u9970\u5668<\/th><th class=\"has-text-align-left\" data-align=\"left\">\u7b2c\u4e00\u4e2a\u53c2\u6570<\/th><th class=\"has-text-align-left\" data-align=\"left\">\u8bbf\u95ee\u5b9e\u4f8b\u5c5e\u6027<\/th><th class=\"has-text-align-left\" data-align=\"left\">\u8bbf\u95ee\u7c7b\u5c5e\u6027<\/th><\/tr><\/thead><tbody><tr><td class=\"has-text-align-left\" data-align=\"left\">\u5b9e\u4f8b\u65b9\u6cd5<\/td><td class=\"has-text-align-left\" data-align=\"left\">\u65e0<\/td><td class=\"has-text-align-left\" data-align=\"left\"><code>self<\/code><\/td><td class=\"has-text-align-left\" data-align=\"left\">\u2713<\/td><td class=\"has-text-align-left\" data-align=\"left\">\u2713<\/td><\/tr><tr><td class=\"has-text-align-left\" data-align=\"left\">\u7c7b\u65b9\u6cd5<\/td><td class=\"has-text-align-left\" data-align=\"left\"><code>@classmethod<\/code><\/td><td class=\"has-text-align-left\" data-align=\"left\"><code>cls<\/code><\/td><td class=\"has-text-align-left\" data-align=\"left\">\u2717<\/td><td class=\"has-text-align-left\" data-align=\"left\">\u2713<\/td><\/tr><tr><td class=\"has-text-align-left\" data-align=\"left\">\u9759\u6001\u65b9\u6cd5<\/td><td class=\"has-text-align-left\" data-align=\"left\"><code>@staticmethod<\/code><\/td><td class=\"has-text-align-left\" data-align=\"left\">\u65e0<\/td><td class=\"has-text-align-left\" data-align=\"left\">\u2717<\/td><td class=\"has-text-align-left\" data-align=\"left\">\u2717<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\u7ee7\u627f<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">\u5b50\u7c7b\u7ee7\u627f\u7236\u7c7b\u7684\u5c5e\u6027\u548c\u65b9\u6cd5\uff0c\u5e76\u53ef\u4ee5\u6269\u5c55\u6216\u91cd\u5199\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>class Animal:<br> &nbsp; &nbsp;def __init__(self, name):<br> &nbsp; &nbsp; &nbsp; &nbsp;self.name = name<br>\u200b<br> &nbsp; &nbsp;def speak(self):<br> &nbsp; &nbsp; &nbsp; &nbsp;print(\"Some sound\")<br>\u200b<br>class Dog(Animal): &nbsp;# Dog \u7ee7\u627f Animal<br> &nbsp; &nbsp;def speak(self): &nbsp;# \u91cd\u5199\u7236\u7c7b\u65b9\u6cd5<br> &nbsp; &nbsp; &nbsp; &nbsp;print(f\"{self.name} says: Woof!\")<br>\u200b<br>class Cat(Animal):<br> &nbsp; &nbsp;def speak(self):<br> &nbsp; &nbsp; &nbsp; &nbsp;print(f\"{self.name} says: Meow!\")<br>\u200b<br>dog = Dog(\"Buddy\")<br>cat = Cat(\"Kitty\")<br>\u200b<br>dog.speak() &nbsp;# Buddy says: Woof!<br>cat.speak() &nbsp;# Kitty says: Meow!<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">\u65b9\u6cd5\u89e3\u6790\u987a\u5e8f\uff08MRO\uff09<\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">\u5f53\u591a\u4e2a\u7236\u7c7b\u6709\u540c\u540d\u65b9\u6cd5\u65f6\uff0cPython \u6309\u7167 <strong>MRO<\/strong>\uff08Method Resolution Order\uff09\u987a\u5e8f\u67e5\u627e\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>class A:<br> &nbsp; &nbsp;def hello(self):<br> &nbsp; &nbsp; &nbsp; &nbsp;print(\"Hello from A\")<br>\u200b<br>class B(A):<br> &nbsp; &nbsp;def hello(self):<br> &nbsp; &nbsp; &nbsp; &nbsp;print(\"Hello from B\")<br>\u200b<br>class C(A):<br> &nbsp; &nbsp;def hello(self):<br> &nbsp; &nbsp; &nbsp; &nbsp;print(\"Hello from C\")<br>\u200b<br>class D(B, C): &nbsp;# MRO: D -&gt; B -&gt; C -&gt; A<br> &nbsp; &nbsp;pass<br>\u200b<br>d = D()<br>d.hello() &nbsp;# Hello from B\uff08\u5148\u627e\u5230 B \u7684\u65b9\u6cd5\uff09<br>\u200b<br># \u67e5\u770b MRO \u987a\u5e8f<br>print(D.__mro__)<br># (&lt;class 'D'&gt;, &lt;class 'B'&gt;, &lt;class 'C'&gt;, &lt;class 'A'&gt;, &lt;class 'object'&gt;)<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5b9a\u4e49\u7c7b \u4f7f\u7528 class \u5173\u952e\u5b57\u5b9a\u4e49\u7c7b\uff1a \u4f7f\u7528 t..<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[52,14],"tags":[],"class_list":["post-657","post","type-post","status-publish","format-standard","hentry","category-python","category-back"],"featured_image_urls":{"full":"","thumbnail":"","medium":"","medium_large":"","large":"","1536x1536":"","2048x2048":""},"author_info":{"info":["\u9648 \u67d0\u4eba"]},"category_info":"<a href=\"https:\/\/mitongxue.cn\/index.php\/category\/back\/python\/\" rel=\"category tag\">Python<\/a> <a href=\"https:\/\/mitongxue.cn\/index.php\/category\/back\/\" rel=\"category tag\">\u540e\u7aef<\/a>","tag_info":"\u540e\u7aef","comment_count":"0","_links":{"self":[{"href":"https:\/\/mitongxue.cn\/index.php\/wp-json\/wp\/v2\/posts\/657","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/mitongxue.cn\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/mitongxue.cn\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/mitongxue.cn\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/mitongxue.cn\/index.php\/wp-json\/wp\/v2\/comments?post=657"}],"version-history":[{"count":2,"href":"https:\/\/mitongxue.cn\/index.php\/wp-json\/wp\/v2\/posts\/657\/revisions"}],"predecessor-version":[{"id":659,"href":"https:\/\/mitongxue.cn\/index.php\/wp-json\/wp\/v2\/posts\/657\/revisions\/659"}],"wp:attachment":[{"href":"https:\/\/mitongxue.cn\/index.php\/wp-json\/wp\/v2\/media?parent=657"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mitongxue.cn\/index.php\/wp-json\/wp\/v2\/categories?post=657"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mitongxue.cn\/index.php\/wp-json\/wp\/v2\/tags?post=657"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}