{"id":680,"date":"2026-03-15T20:57:00","date_gmt":"2026-03-15T12:57:00","guid":{"rendered":"https:\/\/mitongxue.cn\/?p=680"},"modified":"2026-07-14T21:04:51","modified_gmt":"2026-07-14T13:04:51","slug":"python-%e9%ad%94%e6%9c%af%e6%96%b9%e6%b3%95%e8%af%a6%e7%bb%86","status":"publish","type":"post","link":"https:\/\/mitongxue.cn\/index.php\/2026\/03\/15\/python-%e9%ad%94%e6%9c%af%e6%96%b9%e6%b3%95%e8%af%a6%e7%bb%86\/","title":{"rendered":"Python \u9b54\u672f\u65b9\u6cd5\u8be6\u7ec6"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">\u9b54\u672f\u65b9\u6cd5\uff08Magic Methods\uff09\u662f Python \u4e2d<strong>\u4ee5\u53cc\u4e0b\u5212\u7ebf\u5f00\u5934\u548c\u7ed3\u5c3e<\/strong>\u7684\u7279\u6b8a\u65b9\u6cd5\uff0c\u5982 <code>__init__<\/code>\u3001<code>__str__<\/code>\u3002\u5b83\u4eec\u4e0d\u9700\u8981\u663e\u5f0f\u8c03\u7528\uff0c\u800c\u662f\u7531 Python \u5728\u7279\u5b9a\u573a\u666f\u4e0b<strong>\u81ea\u52a8\u89e6\u53d1<\/strong>\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">\u5b57\u7b26\u4e32\u8868\u793a<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">\u5f53\u4f7f\u7528 <code>print()<\/code>\u3001<code>str()<\/code> \u6216 <code>repr()<\/code> \u65f6\uff0cPython \u4f1a\u81ea\u52a8\u8c03\u7528\u5bf9\u5e94\u7684\u9b54\u672f\u65b9\u6cd5\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>class Point:<br> &nbsp; &nbsp;def __init__(self, x, y):<br> &nbsp; &nbsp; &nbsp; &nbsp;self.x = x<br> &nbsp; &nbsp; &nbsp; &nbsp;self.y = y<br>\u200b<br> &nbsp; &nbsp;def __str__(self):<br> &nbsp; &nbsp; &nbsp; &nbsp;\"\"\"\u9762\u5411\u7528\u6237\uff0c\u53cb\u597d\u7684\u53ef\u8bfb\u683c\u5f0f\"\"\"<br> &nbsp; &nbsp; &nbsp; &nbsp;return f\"Point({self.x}, {self.y})\"<br>\u200b<br> &nbsp; &nbsp;def __repr__(self):<br> &nbsp; &nbsp; &nbsp; &nbsp;\"\"\"\u9762\u5411\u5f00\u53d1\u8005\uff0c\u7cbe\u786e\u7684\u91cd\u5efa\u683c\u5f0f\"\"\"<br> &nbsp; &nbsp; &nbsp; &nbsp;return f\"Point({self.x!r}, {self.y!r})\"<br>\u200b<br>\u200b<br>p = Point(3, 4)<br>print(p) &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; # Point(3, 4) \u2014\u2014 \u8c03\u7528 __str__<br>print(str(p)) &nbsp; &nbsp; &nbsp;# Point(3, 4) \u2014\u2014 \u8c03\u7528 __str__<br>print(repr(p)) &nbsp; &nbsp; # Point(3, 4) \u2014\u2014 \u8c03\u7528 __repr__<br>\u200b<br># \u4ea4\u4e92\u5f0f\u73af\u5883\u4e2d\u76f4\u63a5\u663e\u793a\u5bf9\u8c61\uff0c\u8c03\u7528 __repr__<br># p  # Point(3, 4)<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u5efa\u8bae\uff1a<\/strong> \u4e24\u4e2a\u65b9\u6cd5\u90fd\u5b9e\u73b0\u3002\u5982\u679c\u53ea\u5b9e\u73b0 <code>__repr__<\/code>\uff0c<code>__str__<\/code> \u4f1a\u56de\u9000\u5230\u4f7f\u7528\u5b83\u3002<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\u6bd4\u8f83\u64cd\u4f5c<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">\u901a\u8fc7\u5b9e\u73b0\u6bd4\u8f83\u9b54\u672f\u65b9\u6cd5\uff0c\u53ef\u4ee5\u8ba9\u81ea\u5b9a\u4e49\u5bf9\u8c61\u652f\u6301 <code>==<\/code>\u3001<code>&lt;<\/code>\u3001<code>&gt;<\/code> \u7b49\u64cd\u4f5c\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>class Person:<br> &nbsp; &nbsp;def __init__(self, name, age):<br> &nbsp; &nbsp; &nbsp; &nbsp;self.name = name<br> &nbsp; &nbsp; &nbsp; &nbsp;self.age = age<br>\u200b<br> &nbsp; &nbsp;def __eq__(self, other):<br> &nbsp; &nbsp; &nbsp; &nbsp;\"\"\"==\"\"\"<br> &nbsp; &nbsp; &nbsp; &nbsp;if not isinstance(other, Person):<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;return NotImplemented<br> &nbsp; &nbsp; &nbsp; &nbsp;return self.age == other.age<br>\u200b<br> &nbsp; &nbsp;def __lt__(self, other):<br> &nbsp; &nbsp; &nbsp; &nbsp;\"\"\"&lt;\"\"\"<br> &nbsp; &nbsp; &nbsp; &nbsp;if not isinstance(other, Person):<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;return NotImplemented<br> &nbsp; &nbsp; &nbsp; &nbsp;return self.age &lt; other.age<br>\u200b<br> &nbsp; &nbsp;def __le__(self, other):<br> &nbsp; &nbsp; &nbsp; &nbsp;\"\"\"&lt;=\"\"\"<br> &nbsp; &nbsp; &nbsp; &nbsp;return self &lt; other or self == other<br>\u200b<br> &nbsp; &nbsp;def __gt__(self, other):<br> &nbsp; &nbsp; &nbsp; &nbsp;\"\"\"&gt;\"\"\"<br> &nbsp; &nbsp; &nbsp; &nbsp;return not self &lt;= other<br>\u200b<br> &nbsp; &nbsp;def __ge__(self, other):<br> &nbsp; &nbsp; &nbsp; &nbsp;\"\"\"&gt;=\"\"\"<br> &nbsp; &nbsp; &nbsp; &nbsp;return not self &lt; other<br>\u200b<br> &nbsp; &nbsp;def __ne__(self, other):<br> &nbsp; &nbsp; &nbsp; &nbsp;\"\"\"!=\"\"\"<br> &nbsp; &nbsp; &nbsp; &nbsp;return not self == other<br>\u200b<br> &nbsp; &nbsp;def __repr__(self):<br> &nbsp; &nbsp; &nbsp; &nbsp;return f\"Person({self.name!r}, {self.age})\"<br>\u200b<br>\u200b<br>alice = Person(\"Alice\", 30)<br>bob = Person(\"Bob\", 25)<br>\u200b<br>print(alice == bob) &nbsp;# False &nbsp; <br>print(alice &gt; bob) &nbsp; # True<br>print(alice &lt;= bob) &nbsp;# False<br>\u200b<br># \u5b9e\u73b0\u4e86\u6bd4\u8f83\u65b9\u6cd5\u540e\uff0c\u53ef\u4ee5\u4f7f\u7528 sorted<br>people = &#91;bob, alice]<br>print(sorted(people)) &nbsp;# &#91;Person('Bob', 25), Person('Alice', 30)]<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u7b80\u5316\u65b9\u6848\uff1a<\/strong> \u4f7f\u7528 <code>@functools.total_ordering<\/code> \u88c5\u9970\u5668\uff0c\u53ea\u9700\u5b9e\u73b0 <code>__eq__<\/code> \u548c\u5176\u4e2d\u4e00\u4e2a\uff08\u5982 <code>__lt__<\/code>\uff09\uff0c\u5176\u4f59\u4f1a\u81ea\u52a8\u63a8\u5bfc\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>from functools import total_ordering<br>\u200b<br>@total_ordering<br>class Person:<br> &nbsp; &nbsp;def __init__(self, name, age):<br> &nbsp; &nbsp; &nbsp; &nbsp;self.name = name<br> &nbsp; &nbsp; &nbsp; &nbsp;self.age = age<br>\u200b<br> &nbsp; &nbsp;def __eq__(self, other):<br> &nbsp; &nbsp; &nbsp; &nbsp;if not isinstance(other, Person):<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;return NotImplemented<br> &nbsp; &nbsp; &nbsp; &nbsp;return self.age == other.age<br>\u200b<br> &nbsp; &nbsp;def __lt__(self, other):<br> &nbsp; &nbsp; &nbsp; &nbsp;if not isinstance(other, Person):<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;return NotImplemented<br> &nbsp; &nbsp; &nbsp; &nbsp;return self.age &lt; other.age<br>\u200b<br> &nbsp; &nbsp;def __repr__(self):<br> &nbsp; &nbsp; &nbsp; &nbsp;return f\"Person({self.name!r}, {self.age})\"<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\u7b97\u672f\u8fd0\u7b97<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">\u8ba9\u5bf9\u8c61\u652f\u6301 <code>+<\/code>\u3001<code>-<\/code>\u3001<code>*<\/code>\u3001<code>\/<\/code> \u7b49\u8fd0\u7b97\u7b26\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>class Vector:<br> &nbsp; &nbsp;def __init__(self, x, y):<br> &nbsp; &nbsp; &nbsp; &nbsp;self.x = x<br> &nbsp; &nbsp; &nbsp; &nbsp;self.y = y<br>\u200b<br> &nbsp; &nbsp;def __add__(self, other):<br> &nbsp; &nbsp; &nbsp; &nbsp;\"\"\"+\"\"\"<br> &nbsp; &nbsp; &nbsp; &nbsp;if isinstance(other, Vector):<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;return Vector(self.x + other.x, self.y + other.y)<br> &nbsp; &nbsp; &nbsp; &nbsp;return NotImplemented &nbsp;# \u8fd4\u56de NotImplemented\uff0c\u8ba9 Python \u5c1d\u8bd5 other \u7684 __radd__<br>\u200b<br> &nbsp; &nbsp;def __sub__(self, other):<br> &nbsp; &nbsp; &nbsp; &nbsp;\"\"\"-\"\"\"<br> &nbsp; &nbsp; &nbsp; &nbsp;if isinstance(other, Vector):<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;return Vector(self.x - other.x, self.y - other.y)<br> &nbsp; &nbsp; &nbsp; &nbsp;return NotImplemented<br>\u200b<br> &nbsp; &nbsp;def __mul__(self, scalar):<br> &nbsp; &nbsp; &nbsp; &nbsp;\"\"\"*\uff0c\u5411\u91cf\u4e0e\u6807\u91cf\u76f8\u4e58\"\"\"<br> &nbsp; &nbsp; &nbsp; &nbsp;if isinstance(scalar, (int, float)):<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;return Vector(self.x * scalar, self.y * scalar)<br> &nbsp; &nbsp; &nbsp; &nbsp;return NotImplemented<br>\u200b<br> &nbsp; &nbsp;def __rmul__(self, scalar):<br> &nbsp; &nbsp; &nbsp; &nbsp;\"\"\"\u53f3\u4e58\uff1ascalar * vector\"\"\"<br> &nbsp; &nbsp; &nbsp; &nbsp;return self * scalar &nbsp;# \u590d\u7528 __mul__<br>\u200b<br> &nbsp; &nbsp;def __truediv__(self, scalar):<br> &nbsp; &nbsp; &nbsp; &nbsp;\"\"\"\/\"\"\"<br> &nbsp; &nbsp; &nbsp; &nbsp;if isinstance(scalar, (int, float)):<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;return Vector(self.x \/ scalar, self.y \/ scalar)<br> &nbsp; &nbsp; &nbsp; &nbsp;return NotImplemented<br>\u200b<br> &nbsp; &nbsp;def __neg__(self):<br> &nbsp; &nbsp; &nbsp; &nbsp;\"\"\"\u8d1f\u53f7\uff1a-vector\"\"\"<br> &nbsp; &nbsp; &nbsp; &nbsp;return Vector(-self.x, -self.y)<br>\u200b<br> &nbsp; &nbsp;def __abs__(self):<br> &nbsp; &nbsp; &nbsp; &nbsp;\"\"\"abs()\"\"\"<br> &nbsp; &nbsp; &nbsp; &nbsp;return (self.x ** 2 + self.y ** 2) ** 0.5<br>\u200b<br> &nbsp; &nbsp;def __repr__(self):<br> &nbsp; &nbsp; &nbsp; &nbsp;return f\"Vector({self.x}, {self.y})\"<br>\u200b<br>\u200b<br>v1 = Vector(1, 2)<br>v2 = Vector(3, 4)<br>\u200b<br>print(v1 + v2) &nbsp; &nbsp; &nbsp; # Vector(4, 6)<br>print(v2 - v1) &nbsp; &nbsp; &nbsp; # Vector(2, 2)<br>print(v1 * 3) &nbsp; &nbsp; &nbsp; &nbsp;# Vector(3, 6)<br>print(2 * v1) &nbsp; &nbsp; &nbsp; &nbsp;# Vector(2, 4) \u2014\u2014 \u8c03\u7528 __rmul__<br>print(-v1) &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; # Vector(-1, -2)<br>print(abs(v1)) &nbsp; &nbsp; &nbsp; # 2.236...<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u5e38\u7528\u7b97\u672f\u9b54\u672f\u65b9\u6cd5\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\">\u8fd0\u7b97\u7b26<\/th><th class=\"has-text-align-left\" data-align=\"left\">\u9b54\u672f\u65b9\u6cd5<\/th><th class=\"has-text-align-left\" data-align=\"left\">\u8bf4\u660e<\/th><\/tr><\/thead><tbody><tr><td class=\"has-text-align-left\" data-align=\"left\"><code>+<\/code><\/td><td class=\"has-text-align-left\" data-align=\"left\"><code>__add__<\/code><\/td><td class=\"has-text-align-left\" data-align=\"left\">\u52a0\u6cd5<\/td><\/tr><tr><td class=\"has-text-align-left\" data-align=\"left\"><code>-<\/code><\/td><td class=\"has-text-align-left\" data-align=\"left\"><code>__sub__<\/code><\/td><td class=\"has-text-align-left\" data-align=\"left\">\u51cf\u6cd5<\/td><\/tr><tr><td class=\"has-text-align-left\" data-align=\"left\"><code>*<\/code><\/td><td class=\"has-text-align-left\" data-align=\"left\"><code>__mul__<\/code><\/td><td class=\"has-text-align-left\" data-align=\"left\">\u4e58\u6cd5<\/td><\/tr><tr><td class=\"has-text-align-left\" data-align=\"left\"><code>\/<\/code><\/td><td class=\"has-text-align-left\" data-align=\"left\"><code>__truediv__<\/code><\/td><td class=\"has-text-align-left\" data-align=\"left\">\u771f\u9664\u6cd5<\/td><\/tr><tr><td class=\"has-text-align-left\" data-align=\"left\"><code>\/\/<\/code><\/td><td class=\"has-text-align-left\" data-align=\"left\"><code>__floordiv__<\/code><\/td><td class=\"has-text-align-left\" data-align=\"left\">\u6574\u9664<\/td><\/tr><tr><td class=\"has-text-align-left\" data-align=\"left\"><code>%<\/code><\/td><td class=\"has-text-align-left\" data-align=\"left\"><code>__mod__<\/code><\/td><td class=\"has-text-align-left\" data-align=\"left\">\u53d6\u6a21<\/td><\/tr><tr><td class=\"has-text-align-left\" data-align=\"left\"><code>**<\/code><\/td><td class=\"has-text-align-left\" data-align=\"left\"><code>__pow__<\/code><\/td><td class=\"has-text-align-left\" data-align=\"left\">\u5e42\u8fd0\u7b97<\/td><\/tr><tr><td class=\"has-text-align-left\" data-align=\"left\"><code>+a<\/code><\/td><td class=\"has-text-align-left\" data-align=\"left\"><code>__pos__<\/code><\/td><td class=\"has-text-align-left\" data-align=\"left\">\u6b63\u53f7<\/td><\/tr><tr><td class=\"has-text-align-left\" data-align=\"left\"><code>-a<\/code><\/td><td class=\"has-text-align-left\" data-align=\"left\"><code>__neg__<\/code><\/td><td class=\"has-text-align-left\" data-align=\"left\">\u8d1f\u53f7<\/td><\/tr><tr><td class=\"has-text-align-left\" data-align=\"left\"><code>abs()<\/code><\/td><td class=\"has-text-align-left\" data-align=\"left\"><code>__abs__<\/code><\/td><td class=\"has-text-align-left\" data-align=\"left\">\u7edd\u5bf9\u503c<\/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\">\u5bb9\u5668\u534f\u8bae<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">\u5b9e\u73b0\u5bb9\u5668\u534f\u8bae\uff0c\u8ba9\u81ea\u5b9a\u4e49\u5bf9\u8c61\u53ef\u4ee5\u50cf <code>list<\/code>\u3001<code>dict<\/code> \u4e00\u6837\u4f7f\u7528 <code>[]<\/code>\u3001 <code>len()<\/code>\u3001<code>in<\/code> \u7b49\u64cd\u4f5c\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>class ShoppingCart:<br> &nbsp; &nbsp;def __init__(self):<br> &nbsp; &nbsp; &nbsp; &nbsp;self._items = &#91;]<br>\u200b<br> &nbsp; &nbsp;def __len__(self):<br> &nbsp; &nbsp; &nbsp; &nbsp;\"\"\"len(cart)\"\"\"<br> &nbsp; &nbsp; &nbsp; &nbsp;return len(self._items)<br>\u200b<br> &nbsp; &nbsp;def __getitem__(self, index):<br> &nbsp; &nbsp; &nbsp; &nbsp;\"\"\"cart&#91;index]\"\"\"<br> &nbsp; &nbsp; &nbsp; &nbsp;return self._items&#91;index]<br>\u200b<br> &nbsp; &nbsp;def __setitem__(self, index, value):<br> &nbsp; &nbsp; &nbsp; &nbsp;\"\"\"cart&#91;index] = value\"\"\"<br> &nbsp; &nbsp; &nbsp; &nbsp;self._items&#91;index] = value<br>\u200b<br> &nbsp; &nbsp;def __delitem__(self, index):<br> &nbsp; &nbsp; &nbsp; &nbsp;\"\"\"del cart&#91;index]\"\"\"<br> &nbsp; &nbsp; &nbsp; &nbsp;del self._items&#91;index]<br>\u200b<br> &nbsp; &nbsp;def __contains__(self, item):<br> &nbsp; &nbsp; &nbsp; &nbsp;\"\"\"item in cart\"\"\"<br> &nbsp; &nbsp; &nbsp; &nbsp;return item in self._items<br>\u200b<br> &nbsp; &nbsp;def __iter__(self):<br> &nbsp; &nbsp; &nbsp; &nbsp;\"\"\"for item in cart\"\"\"<br> &nbsp; &nbsp; &nbsp; &nbsp;return iter(self._items)<br>\u200b<br> &nbsp; &nbsp;def append(self, item):<br> &nbsp; &nbsp; &nbsp; &nbsp;self._items.append(item)<br>\u200b<br> &nbsp; &nbsp;def __repr__(self):<br> &nbsp; &nbsp; &nbsp; &nbsp;return f\"ShoppingCart({self._items!r})\"<br>\u200b<br>\u200b<br>cart = ShoppingCart()<br>cart.append(\"apple\")<br>cart.append(\"banana\")<br>cart.append(\"orange\")<br>\u200b<br>print(len(cart)) &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; # 3<br>print(cart&#91;0]) &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; # apple<br>print(cart&#91;1:]) &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;# &#91;'banana', 'orange'] \u2014\u2014 \u652f\u6301\u5207\u7247<br>print(\"apple\" in cart) &nbsp; &nbsp; # True<br>\u200b<br>for item in cart:<br> &nbsp; &nbsp;print(item)<br># apple<br># banana<br># orange<\/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\u578b\u8f6c\u6362<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">\u5b9e\u73b0\u7c7b\u578b\u8f6c\u6362\u9b54\u672f\u65b9\u6cd5\uff0c\u8ba9\u5bf9\u8c61\u652f\u6301 <code>int()<\/code>\u3001<code>float()<\/code>\u3001<code>bool()<\/code> \u7b49\u8f6c\u6362\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>class Money:<br> &nbsp; &nbsp;def __init__(self, amount):<br> &nbsp; &nbsp; &nbsp; &nbsp;self.amount = amount<br>\u200b<br> &nbsp; &nbsp;def __int__(self):<br> &nbsp; &nbsp; &nbsp; &nbsp;return int(self.amount)<br>\u200b<br> &nbsp; &nbsp;def __float__(self):<br> &nbsp; &nbsp; &nbsp; &nbsp;return float(self.amount)<br>\u200b<br> &nbsp; &nbsp;def __bool__(self):<br> &nbsp; &nbsp; &nbsp; &nbsp;return self.amount != 0<br>\u200b<br> &nbsp; &nbsp;def __repr__(self):<br> &nbsp; &nbsp; &nbsp; &nbsp;return f\"Money({self.amount})\"<br>\u200b<br>\u200b<br>m = Money(100.5)<br>print(int(m)) &nbsp; &nbsp; &nbsp;# 100<br>print(float(m)) &nbsp; &nbsp;# 100.5<br>print(bool(m)) &nbsp; &nbsp; # True<br>\u200b<br>m0 = Money(0)<br>print(bool(m0)) &nbsp; &nbsp;# False<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\u5c5e\u6027\u8bbf\u95ee\u62e6\u622a<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">\u901a\u8fc7\u5b9e\u73b0\u5c5e\u6027\u8bbf\u95ee\u76f8\u5173\u7684\u9b54\u672f\u65b9\u6cd5\uff0c\u53ef\u4ee5\u62e6\u622a\u5bf9\u5bf9\u8c61\u5c5e\u6027\u7684<strong>\u8bfb\u53d6<\/strong>\u3001<strong>\u8bbe\u7f6e<\/strong>\u548c<strong>\u5220\u9664<\/strong>\u64cd\u4f5c\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>class Config:<br> &nbsp; &nbsp;def __init__(self):<br> &nbsp; &nbsp; &nbsp; &nbsp;# \u5fc5\u987b\u7528 object.__setattr__\uff0c\u5426\u5219\u4f1a\u65e0\u9650\u9012\u5f52<br> &nbsp; &nbsp; &nbsp; &nbsp;object.__setattr__(self, \"_data\", {})<br>\u200b<br> &nbsp; &nbsp;def __getattr__(self, name):<br> &nbsp; &nbsp; &nbsp; &nbsp;\"\"\"\u8bbf\u95ee\u4e0d\u5b58\u5728\u7684\u5c5e\u6027\u65f6\u89e6\u53d1\"\"\"<br> &nbsp; &nbsp; &nbsp; &nbsp;if name in self._data:<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;return self._data&#91;name]<br> &nbsp; &nbsp; &nbsp; &nbsp;raise AttributeError(f\"'{type(self).__name__}' \u5bf9\u8c61\u6ca1\u6709\u5c5e\u6027 '{name}'\")<br>\u200b<br> &nbsp; &nbsp;def __setattr__(self, name, value):<br> &nbsp; &nbsp; &nbsp; &nbsp;\"\"\"\u8bbe\u7f6e\u4efb\u610f\u5c5e\u6027\u65f6\u89e6\u53d1\"\"\"<br> &nbsp; &nbsp; &nbsp; &nbsp;if name.startswith(\"_\"):<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;# \u5185\u90e8\u5c5e\u6027\u76f4\u63a5\u8bbe\u7f6e\uff0c\u907f\u514d\u9012\u5f52<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;object.__setattr__(self, name, value)<br> &nbsp; &nbsp; &nbsp; &nbsp;else:<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;self._data&#91;name] = value<br>\u200b<br> &nbsp; &nbsp;def __delattr__(self, name):<br> &nbsp; &nbsp; &nbsp; &nbsp;\"\"\"\u5220\u9664\u5c5e\u6027\u65f6\u89e6\u53d1\"\"\"<br> &nbsp; &nbsp; &nbsp; &nbsp;if name in self._data:<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;del self._data&#91;name]<br> &nbsp; &nbsp; &nbsp; &nbsp;else:<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;raise AttributeError(f\"'{type(self).__name__}' \u5bf9\u8c61\u6ca1\u6709\u5c5e\u6027 '{name}'\")<br>\u200b<br> &nbsp; &nbsp;def __repr__(self):<br> &nbsp; &nbsp; &nbsp; &nbsp;return f\"Config({self._data!r})\"<br>\u200b<br>\u200b<br>cfg = Config()<br>cfg.debug = True &nbsp; &nbsp; &nbsp;# \u8c03\u7528 __setattr__<br>cfg.port = 8080 &nbsp; &nbsp; &nbsp; # \u8c03\u7528 __setattr__<br>print(cfg.debug) &nbsp; &nbsp; &nbsp;# True \u2014\u2014 \u8c03\u7528 __getattr__<br>print(cfg.port) &nbsp; &nbsp; &nbsp; # 8080 \u2014\u2014 \u8c03\u7528 __getattr__<br>del cfg.debug &nbsp; &nbsp; &nbsp; &nbsp; # \u8c03\u7528 __delattr__<br>print(cfg) &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;# Config({'port': 8080})<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u6ce8\u610f\uff1a<\/strong> <code>__setattr__<\/code> \u62e6\u622a<strong>\u6240\u6709<\/strong>\u5c5e\u6027\u8bbe\u7f6e\u3002\u5982\u679c\u5728\u5176\u5185\u90e8\u4f7f\u7528 <code>self.xxx = value<\/code> \u7684\u65b9\u5f0f\u8d4b\u503c\uff0c\u4f1a\u518d\u6b21\u89e6\u53d1 <code>__setattr__<\/code>\uff0c\u5bfc\u81f4<strong>\u65e0\u9650\u9012\u5f52<\/strong>\u3002\u5e94\u4f7f\u7528 <code>object.__setattr__(self, name, value)<\/code> \u6765\u7ed5\u8fc7\u62e6\u622a\u3002<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\"><code>__getattr__<\/code> vs <code>__getattribute__<\/code><\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>__getattr__<\/code>\uff1a<strong>\u4ec5<\/strong>\u5728\u8bbf\u95ee<strong>\u4e0d\u5b58\u5728<\/strong>\u7684\u5c5e\u6027\u65f6\u89e6\u53d1<\/li>\n\n\n\n<li><code>__getattribute__<\/code>\uff1a\u8bbf\u95ee<strong>\u4efb\u4f55<\/strong>\u5c5e\u6027\u65f6\u90fd\u4f1a\u89e6\u53d1\uff08\u66f4\u5e95\u5c42\uff0c\u4f18\u5148\u7ea7\u66f4\u9ad8\uff09<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>class Demo:<br> &nbsp; &nbsp;def __init__(self):<br> &nbsp; &nbsp; &nbsp; &nbsp;self.existing = 100<br>\u200b<br> &nbsp; &nbsp;def __getattribute__(self, name):<br> &nbsp; &nbsp; &nbsp; &nbsp;\"\"\"\u6240\u6709\u5c5e\u6027\u8bbf\u95ee\u90fd\u4f1a\u7ecf\u8fc7\u8fd9\u91cc\"\"\"<br> &nbsp; &nbsp; &nbsp; &nbsp;print(f\"\u6b63\u5728\u8bbf\u95ee: {name}\")<br> &nbsp; &nbsp; &nbsp; &nbsp;# \u5fc5\u987b\u7528 object.__getattribute__\uff0c\u5426\u5219\u4f1a\u65e0\u9650\u9012\u5f52<br> &nbsp; &nbsp; &nbsp; &nbsp;return object.__getattribute__(self, name)<br>\u200b<br> &nbsp; &nbsp;def __getattr__(self, name):<br> &nbsp; &nbsp; &nbsp; &nbsp;\"\"\"\u53ea\u6709\u8bbf\u95ee\u4e0d\u5b58\u5728\u7684\u5c5e\u6027\u65f6\u624d\u5230\u8fd9\u91cc\"\"\"<br> &nbsp; &nbsp; &nbsp; &nbsp;return f\"'{name}' \u4e0d\u5b58\u5728\uff0c\u8fd4\u56de\u9ed8\u8ba4\u503c\"<br>\u200b<br>\u200b<br>d = Demo()<br>print(d.existing) &nbsp; # \u5148\u89e6\u53d1 __getattribute__\uff0c\u8fd4\u56de 100<br>print(d.missing) &nbsp; &nbsp;# \u5148\u89e6\u53d1 __getattribute__\uff0c\u627e\u4e0d\u5230\uff0c\u518d\u89e6\u53d1 __getattr__<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u26a0\ufe0f \u8b66\u544a\uff1a<\/strong> \u5728 <code>__getattribute__<\/code> \u4e2d\u518d\u6b21\u8bbf\u95ee <code>self.xxx<\/code> \u4e5f\u4f1a\u89e6\u53d1\u81ea\u8eab\uff0c\u5fc5\u987b\u4f7f\u7528 <code>object.__getattribute__(self, name)<\/code>\u3002<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\u5bf9\u8c61\u751f\u547d\u5468\u671f<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">\u9664\u4e86 <code>__init__<\/code>\uff0c\u8fd8\u6709 <code>__del__<\/code> \u5728\u5bf9\u8c61\u88ab\u9500\u6bc1\u65f6\u8c03\u7528\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>class DatabaseConnection:<br> &nbsp; &nbsp;def __init__(self, db_name):<br> &nbsp; &nbsp; &nbsp; &nbsp;self.db_name = db_name<br> &nbsp; &nbsp; &nbsp; &nbsp;print(f\"\u8fde\u63a5\u5230\u6570\u636e\u5e93: {db_name}\")<br>\u200b<br> &nbsp; &nbsp;def __del__(self):<br> &nbsp; &nbsp; &nbsp; &nbsp;\"\"\"\u5bf9\u8c61\u88ab\u9500\u6bc1\u65f6\u8c03\u7528\"\"\"<br> &nbsp; &nbsp; &nbsp; &nbsp;print(f\"\u5173\u95ed\u6570\u636e\u5e93\u8fde\u63a5: {self.db_name}\")<br>\u200b<br>\u200b<br>conn = DatabaseConnection(\"test_db\")<br>del conn &nbsp;# \u5173\u95ed\u6570\u636e\u5e93\u8fde\u63a5: test_db<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u6ce8\u610f\uff1a<\/strong> <code>__del__<\/code> \u7684\u8c03\u7528\u65f6\u673a\u4e0d\u786e\u5b9a\uff08\u53d6\u51b3\u4e8e\u5783\u573e\u56de\u6536\uff09\uff0c\u4e0d\u5e94\u4f9d\u8d56\u5b83\u505a\u5173\u952e\u6e05\u7406\u3002\u5bf9\u4e8e\u8d44\u6e90\u7ba1\u7406\uff0c\u5e94\u4f7f\u7528<strong>\u4e0a\u4e0b\u6587\u7ba1\u7406\u5668<\/strong>\u3002<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\u5e38\u7528\u9b54\u672f\u65b9\u6cd5\u901f\u67e5<\/h2>\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\">\u7c7b\u522b<\/th><th class=\"has-text-align-left\" data-align=\"left\">\u65b9\u6cd5<\/th><th class=\"has-text-align-left\" data-align=\"left\">\u89e6\u53d1\u573a\u666f<\/th><\/tr><\/thead><tbody><tr><td class=\"has-text-align-left\" data-align=\"left\">\u6784\u9020<\/td><td class=\"has-text-align-left\" data-align=\"left\"><code>__init__<\/code><\/td><td class=\"has-text-align-left\" data-align=\"left\">\u521b\u5efa\u5bf9\u8c61\u540e\u521d\u59cb\u5316<\/td><\/tr><tr><td class=\"has-text-align-left\" data-align=\"left\">\u6784\u9020<\/td><td class=\"has-text-align-left\" data-align=\"left\"><code>__new__<\/code><\/td><td class=\"has-text-align-left\" data-align=\"left\">\u521b\u5efa\u5bf9\u8c61\uff08\u5df2\u8bb2\u8fc7\uff09<\/td><\/tr><tr><td class=\"has-text-align-left\" data-align=\"left\">\u5b57\u7b26\u4e32<\/td><td class=\"has-text-align-left\" data-align=\"left\"><code>__str__<\/code><\/td><td class=\"has-text-align-left\" data-align=\"left\"><code>print()<\/code>\u3001<code>str()<\/code><\/td><\/tr><tr><td class=\"has-text-align-left\" data-align=\"left\">\u5b57\u7b26\u4e32<\/td><td class=\"has-text-align-left\" data-align=\"left\"><code>__repr__<\/code><\/td><td class=\"has-text-align-left\" data-align=\"left\"><code>repr()<\/code>\u3001\u4ea4\u4e92\u5f0f\u663e\u793a<\/td><\/tr><tr><td class=\"has-text-align-left\" data-align=\"left\">\u6bd4\u8f83<\/td><td class=\"has-text-align-left\" data-align=\"left\"><code>__eq__<\/code><\/td><td class=\"has-text-align-left\" data-align=\"left\"><code>==<\/code><\/td><\/tr><tr><td class=\"has-text-align-left\" data-align=\"left\">\u6bd4\u8f83<\/td><td class=\"has-text-align-left\" data-align=\"left\"><code>__lt__<\/code><\/td><td class=\"has-text-align-left\" data-align=\"left\"><code>&lt;<\/code><\/td><\/tr><tr><td class=\"has-text-align-left\" data-align=\"left\">\u6bd4\u8f83<\/td><td class=\"has-text-align-left\" data-align=\"left\"><code>__gt__<\/code><\/td><td class=\"has-text-align-left\" data-align=\"left\"><code>&gt;<\/code><\/td><\/tr><tr><td class=\"has-text-align-left\" data-align=\"left\">\u6bd4\u8f83<\/td><td class=\"has-text-align-left\" data-align=\"left\"><code>__le__<\/code><\/td><td class=\"has-text-align-left\" data-align=\"left\"><code>&lt;=<\/code><\/td><\/tr><tr><td class=\"has-text-align-left\" data-align=\"left\">\u6bd4\u8f83<\/td><td class=\"has-text-align-left\" data-align=\"left\"><code>__ge__<\/code><\/td><td class=\"has-text-align-left\" data-align=\"left\"><code>&gt;=<\/code><\/td><\/tr><tr><td class=\"has-text-align-left\" data-align=\"left\">\u6bd4\u8f83<\/td><td class=\"has-text-align-left\" data-align=\"left\"><code>__ne__<\/code><\/td><td class=\"has-text-align-left\" data-align=\"left\"><code>!=<\/code><\/td><\/tr><tr><td class=\"has-text-align-left\" data-align=\"left\">\u7b97\u672f<\/td><td class=\"has-text-align-left\" data-align=\"left\"><code>__add__<\/code><\/td><td class=\"has-text-align-left\" data-align=\"left\"><code>+<\/code><\/td><\/tr><tr><td class=\"has-text-align-left\" data-align=\"left\">\u7b97\u672f<\/td><td class=\"has-text-align-left\" data-align=\"left\"><code>__sub__<\/code><\/td><td class=\"has-text-align-left\" data-align=\"left\"><code>-<\/code><\/td><\/tr><tr><td class=\"has-text-align-left\" data-align=\"left\">\u7b97\u672f<\/td><td class=\"has-text-align-left\" data-align=\"left\"><code>__mul__<\/code><\/td><td class=\"has-text-align-left\" data-align=\"left\"><code>*<\/code><\/td><\/tr><tr><td class=\"has-text-align-left\" data-align=\"left\">\u7b97\u672f<\/td><td class=\"has-text-align-left\" data-align=\"left\"><code>__truediv__<\/code><\/td><td class=\"has-text-align-left\" data-align=\"left\"><code>\/<\/code><\/td><\/tr><tr><td class=\"has-text-align-left\" data-align=\"left\">\u5bb9\u5668<\/td><td class=\"has-text-align-left\" data-align=\"left\"><code>__len__<\/code><\/td><td class=\"has-text-align-left\" data-align=\"left\"><code>len()<\/code><\/td><\/tr><tr><td class=\"has-text-align-left\" data-align=\"left\">\u5bb9\u5668<\/td><td class=\"has-text-align-left\" data-align=\"left\"><code>__getitem__<\/code><\/td><td class=\"has-text-align-left\" data-align=\"left\"><code>obj[key]<\/code><\/td><\/tr><tr><td class=\"has-text-align-left\" data-align=\"left\">\u5bb9\u5668<\/td><td class=\"has-text-align-left\" data-align=\"left\"><code>__setitem__<\/code><\/td><td class=\"has-text-align-left\" data-align=\"left\"><code>obj[key] = value<\/code><\/td><\/tr><tr><td class=\"has-text-align-left\" data-align=\"left\">\u5bb9\u5668<\/td><td class=\"has-text-align-left\" data-align=\"left\"><code>__delitem__<\/code><\/td><td class=\"has-text-align-left\" data-align=\"left\"><code>del obj[key]<\/code><\/td><\/tr><tr><td class=\"has-text-align-left\" data-align=\"left\">\u5bb9\u5668<\/td><td class=\"has-text-align-left\" data-align=\"left\"><code>__contains__<\/code><\/td><td class=\"has-text-align-left\" data-align=\"left\"><code>in<\/code><\/td><\/tr><tr><td class=\"has-text-align-left\" data-align=\"left\">\u5bb9\u5668<\/td><td class=\"has-text-align-left\" data-align=\"left\"><code>__iter__<\/code><\/td><td class=\"has-text-align-left\" data-align=\"left\"><code>for...in<\/code><\/td><\/tr><tr><td class=\"has-text-align-left\" data-align=\"left\">\u8f6c\u6362<\/td><td class=\"has-text-align-left\" data-align=\"left\"><code>__int__<\/code><\/td><td class=\"has-text-align-left\" data-align=\"left\"><code>int()<\/code><\/td><\/tr><tr><td class=\"has-text-align-left\" data-align=\"left\">\u8f6c\u6362<\/td><td class=\"has-text-align-left\" data-align=\"left\"><code>__float__<\/code><\/td><td class=\"has-text-align-left\" data-align=\"left\"><code>float()<\/code><\/td><\/tr><tr><td class=\"has-text-align-left\" data-align=\"left\">\u8f6c\u6362<\/td><td class=\"has-text-align-left\" data-align=\"left\"><code>__bool__<\/code><\/td><td class=\"has-text-align-left\" data-align=\"left\"><code>bool()<\/code><\/td><\/tr><tr><td class=\"has-text-align-left\" data-align=\"left\">\u53ef\u8c03\u7528<\/td><td class=\"has-text-align-left\" data-align=\"left\"><code>__call__<\/code><\/td><td class=\"has-text-align-left\" data-align=\"left\"><code>obj()<\/code><\/td><\/tr><tr><td class=\"has-text-align-left\" data-align=\"left\">\u5c5e\u6027<\/td><td class=\"has-text-align-left\" data-align=\"left\"><code>__getattr__<\/code><\/td><td class=\"has-text-align-left\" data-align=\"left\">\u8bbf\u95ee\u4e0d\u5b58\u5728\u7684\u5c5e\u6027<\/td><\/tr><tr><td class=\"has-text-align-left\" data-align=\"left\">\u5c5e\u6027<\/td><td class=\"has-text-align-left\" data-align=\"left\"><code>__getattribute__<\/code><\/td><td class=\"has-text-align-left\" data-align=\"left\">\u8bbf\u95ee\u4efb\u610f\u5c5e\u6027<\/td><\/tr><tr><td class=\"has-text-align-left\" data-align=\"left\">\u5c5e\u6027<\/td><td class=\"has-text-align-left\" data-align=\"left\"><code>__setattr__<\/code><\/td><td class=\"has-text-align-left\" data-align=\"left\">\u8bbe\u7f6e\u5c5e\u6027<\/td><\/tr><tr><td class=\"has-text-align-left\" data-align=\"left\">\u5c5e\u6027<\/td><td class=\"has-text-align-left\" data-align=\"left\"><code>__delattr__<\/code><\/td><td class=\"has-text-align-left\" data-align=\"left\">\u5220\u9664\u5c5e\u6027<\/td><\/tr><tr><td class=\"has-text-align-left\" data-align=\"left\">\u751f\u547d\u5468\u671f<\/td><td class=\"has-text-align-left\" data-align=\"left\"><code>__del__<\/code><\/td><td class=\"has-text-align-left\" data-align=\"left\">\u5bf9\u8c61\u9500\u6bc1<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\"><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u9b54\u672f\u65b9\u6cd5\uff08Magic Methods\uff09\u662f Pyth..<\/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":[54],"class_list":["post-680","post","type-post","status-publish","format-standard","hentry","category-python","category-back","tag-python"],"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\/680","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=680"}],"version-history":[{"count":1,"href":"https:\/\/mitongxue.cn\/index.php\/wp-json\/wp\/v2\/posts\/680\/revisions"}],"predecessor-version":[{"id":681,"href":"https:\/\/mitongxue.cn\/index.php\/wp-json\/wp\/v2\/posts\/680\/revisions\/681"}],"wp:attachment":[{"href":"https:\/\/mitongxue.cn\/index.php\/wp-json\/wp\/v2\/media?parent=680"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mitongxue.cn\/index.php\/wp-json\/wp\/v2\/categories?post=680"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mitongxue.cn\/index.php\/wp-json\/wp\/v2\/tags?post=680"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}