{"id":105977,"date":"2026-09-16T13:50:38","date_gmt":"2026-09-16T05:50:38","guid":{"rendered":"https:\/\/www.wsisp.com\/helps\/105977.html"},"modified":"2026-09-16T13:50:38","modified_gmt":"2026-09-16T05:50:38","slug":"python-%e9%ad%94%e6%9c%af%e6%96%b9%e6%b3%95","status":"publish","type":"post","link":"https:\/\/www.wsisp.com\/helps\/105977.html","title":{"rendered":"Python\u2014\u2014\u9b54\u672f\u65b9\u6cd5"},"content":{"rendered":"<h3>1. \u57fa\u7840\u6982\u5ff5<\/h3>\n<h4>1.1 \u4ec0\u4e48\u662f\u9b54\u672f\u65b9\u6cd5<\/h4>\n<p>\u9b54\u672f\u65b9\u6cd5\u662f Python \u5b98\u65b9\u5185\u7f6e\u7684\u7279\u6b8a\u65b9\u6cd5&#xff0c;\u7edf\u4e00\u8bed\u6cd5\u683c\u5f0f\u4e3a \u524d\u540e\u53cc\u4e0b\u5212\u7ebf\u5305\u88f9&#xff1a;__xxx__&#xff0c;\u662f\u9762\u5411\u5bf9\u8c61\u7f16\u7a0b\u7684\u6838\u5fc3\u5e95\u5c42\u8bed\u6cd5\u3002<\/p>\n<p>\u5b83\u65e0\u9700\u5f00\u53d1\u8005\u624b\u52a8\u8c03\u7528&#xff0c;\u4f1a\u5728\u5bf9\u5e94\u4ee3\u7801\u573a\u666f\u89e6\u53d1\u65f6\u81ea\u52a8\u6267\u884c&#xff0c;\u4e13<span style=\"color:#fe2c24\">\u95e8\u7528\u4e8e\u6539\u5199\u81ea\u5b9a\u4e49\u7c7b\u7684\u9ed8\u8ba4\u5e95\u5c42\u884c\u4e3a<\/span>\u3002<\/p>\n<h4>1.2 \u4e3a\u4ec0\u4e48\u9700\u8981\u9b54\u672f\u65b9\u6cd5<\/h4>\n<p>Python \u539f\u751f\u4ec5\u652f\u6301\u5b57\u7b26\u4e32\u3001\u5217\u8868\u3001\u6570\u5b57\u7b49\u5185\u7f6e\u6570\u636e\u7c7b\u578b\u7684\u6253\u5370\u3001\u8fd0\u7b97\u3001\u4e0b\u6807\u53d6\u503c\u3001\u957f\u5ea6\u83b7\u53d6\u7b49\u64cd\u4f5c\u3002\u800c\u6211\u4eec\u81ea\u5df1\u5b9a\u4e49\u7684\u7c7b\u5bf9\u8c61&#xff0c;\u9ed8\u8ba4\u4e0d\u5177\u5907\u8fd9\u4e9b\u80fd\u529b&#xff0c;\u76f4\u63a5\u64cd\u4f5c\u4f1a\u62a5\u9519\u3002<\/p>\n<p>\u9b54\u672f\u65b9\u6cd5\u7684\u6838\u5fc3\u4f5c\u7528&#xff1a;<span style=\"color:#fe2c24\">\u7ed9\u81ea\u5b9a\u4e49\u5bf9\u8c61\u8d4b\u4e88\u539f\u751f\u8bed\u6cd5\u80fd\u529b<\/span><span style=\"color:null\">&#xff0c;\u901a\u8fc7\u91cd\u5199\u5bf9\u5e94\u9b54\u672f\u65b9\u6cd5&#xff0c;\u81ea\u5b9a\u4e49\u5bf9\u8c61\u53ef\u4ee5\u50cf\u5185\u7f6e\u5bf9\u8c61\u4e00\u6837&#xff0c;\u5b9e\u73b0\u6253\u5370\u3001\u52a0\u51cf\u8fd0\u7b97\u3001\u5927\u5c0f\u6bd4\u8f83\u3001\u4e0b\u6807\u64cd\u4f5c\u3001\u8fed\u4ee3\u7b49\u529f\u80fd<\/span>\u3002<\/p>\n<h3>2. \u6838\u5fc3\u9b54\u672f\u65b9\u6cd5<\/h3>\n<h4>2.1 __init__ \u521d\u59cb\u5316\u65b9\u6cd5<\/h4>\n<p>\u89e6\u53d1\u65f6\u673a&#xff1a;\u5b9e\u4f8b\u5316\u5bf9\u8c61\u65f6 \u81ea\u52a8\u89e6\u53d1<\/p>\n<p>\u4f5c\u7528&#xff1a;\u7ed9\u5bf9\u8c61\u521d\u59cb\u5316\u5c5e\u6027&#xff08;\u8d4b\u503c&#xff09;<\/p>\n<p>class Student:<br \/>\n    # \u521b\u5efa\u5bf9\u8c61\u81ea\u52a8\u6267\u884c<br \/>\n    def __init__(self, name, age):<br \/>\n        self.name &#061; name<br \/>\n        self.age &#061; age<\/p>\n<p>s &#061; Student(&#034;\u5c0f\u660e&#034;, 18)  # \u81ea\u52a8\u89e6\u53d1 __init__<br \/>\nprint(s.name, s.age)<\/p>\n<h4>2.2 __new__ \u6784\u9020\u65b9\u6cd5<\/h4>\n<p>\u89e6\u53d1\u65f6\u673a&#xff1a;\u521b\u5efa\u5bf9\u8c61\u7684\u7b2c\u4e00\u6b65&#xff0c;\u6700\u5148\u6267\u884c&#xff0c;\u4f18\u5148\u7ea7\u9ad8\u4e8e __init__<\/p>\n<p>\u6838\u5fc3\u4f5c\u7528&#xff1a;\u5411\u5185\u5b58\u5f00\u8f9f\u7a7a\u95f4\u3001\u521b\u5efa\u5e76\u8fd4\u56de\u7a7a\u767d\u5bf9\u8c61<\/p>\n<p>\u6267\u884c\u987a\u5e8f&#xff1a;__new__ \u5148\u9020\u5bf9\u8c61 \u2192 __init__ \u540e\u8d4b\u503c<\/p>\n<p>__new__\u7cfb\u7edf\u9ed8\u8ba4\u5b9e\u73b0&#xff0c;\u4e0d\u7528\u81ea\u5df1\u5199<\/p>\n<h4>2.3 __str__ \u6253\u5370\u5bf9\u8c61\u65b9\u6cd5<\/h4>\n<p>\u89e6\u53d1\u65f6\u673a&#xff1a;print(\u5bf9\u8c61) \u65f6\u81ea\u52a8\u89e6\u53d1<\/p>\n<p>\u4f5c\u7528&#xff1a;\u81ea\u5b9a\u4e49\u6253\u5370\u5bf9\u8c61\u7684\u5185\u5bb9&#xff0c;\u4e0d\u518d\u8f93\u51fa\u9ed8\u8ba4\u5185\u5b58\u5730\u5740<\/p>\n<p>\u65e0 __str__&#xff08;\u539f\u751f\u9ed8\u8ba4\u6548\u679c&#xff09;\u2192 \u6253\u5370\u5185\u5b58\u5730\u5740<\/p>\n<p>class Student:<br \/>\n    def __init__(self, name, age):<br \/>\n        self.name &#061; name<br \/>\n        self.age &#061; age<\/p>\n<p># \u6ca1\u6709\u91cd\u5199 __str__<br \/>\ns &#061; Student(&#034;\u5c0f\u7ea2&#034;, 17)<br \/>\n# \u76f4\u63a5\u6253\u5370&#xff1a;\u8f93\u51fa\u5783\u573e\u5185\u5b58\u5730\u5740&#xff0c;\u770b\u4e0d\u5230\u5c5e\u6027\u6570\u636e<br \/>\nprint(s)<br \/>\n# \u793a\u4f8b\u8f93\u51fa&#xff1a;&lt;__main__.Student object at 0x0000023xxxxxxx&gt;<\/p>\n<p>\u6709__str__<\/p>\n<p>class Student:<br \/>\n    def __init__(self, name, age):<br \/>\n        self.name &#061; name<br \/>\n        self.age &#061; age<\/p>\n<p>    # \u91cd\u5199\u6253\u5370\u89c4\u5219<br \/>\n    def __str__(self):<br \/>\n        return f&#034;\u5b66\u751f\u59d3\u540d&#xff1a;{self.name}&#xff0c;\u5e74\u9f84&#xff1a;{self.age}&#034;<\/p>\n<p>s &#061; Student(&#034;\u5c0f\u7ea2&#034;, 17)<br \/>\nprint(s)  # \u81ea\u52a8\u89e6\u53d1 __str__<br \/>\n# \u8f93\u51fa&#xff1a;\u5b66\u751f\u59d3\u540d&#xff1a;\u5c0f\u7ea2&#xff0c;\u5e74\u9f84&#xff1a;17<\/p>\n<h4>2.4 __del__ \u6790\u6784\u65b9\u6cd5<\/h4>\n<p>\u89e6\u53d1\u65f6\u673a&#xff1a;\u5bf9\u8c61\u88ab\u9500\u6bc1\u3001\u7a0b\u5e8f\u7ed3\u675f\u3001\u4e3b\u52a8 del \u5220\u9664\u65f6\u81ea\u52a8\u89e6\u53d1<\/p>\n<p>\u4f5c\u7528&#xff1a;\u8d44\u6e90\u91ca\u653e&#xff08;\u5173\u95ed\u6587\u4ef6\u3001\u5173\u95ed\u94fe\u63a5&#xff09;<\/p>\n<p>class Demo:<br \/>\n    def __del__(self):<br \/>\n        print(&#034;\u5bf9\u8c61\u88ab\u9500\u6bc1\u4e86&#xff0c;\u91ca\u653e\u8d44\u6e90&#xff01;&#034;)<\/p>\n<p>d &#061; Demo()<br \/>\ndel d   # \u4e3b\u52a8\u5220\u9664\u5bf9\u8c61&#xff0c;\u89e6\u53d1 __del__<br \/>\nprint(&#034;\u7a0b\u5e8f\u7ed3\u675f&#034;)<\/p>\n<h3>3. \u8fd0\u7b97\u7b26\u91cd\u8f7d\u9b54\u672f\u65b9\u6cd5<\/h3>\n<p>\u9ed8\u8ba4\u60c5\u51b5\u4e0b&#xff0c;\u81ea\u5b9a\u4e49\u7c7b\u5bf9\u8c61\u4e0d\u652f\u6301\u4efb\u4f55\u6570\u5b66\u8fd0\u7b97&#xff0c;\u76f4\u63a5\u5bf9\u4e24\u4e2a\u5bf9\u8c61\u8fdb\u884c\u52a0\u51cf\u4e58\u9664\u4f1a\u76f4\u63a5\u62a5\u9519\u3002<\/p>\n<p>\u8fd0\u7b97\u7b26\u91cd\u8f7d\u9b54\u672f\u65b9\u6cd5\u4f5c\u7528&#xff1a;\u91cd\u5199\u8fd0\u7b97\u5e95\u5c42\u89c4\u5219&#xff0c;\u8ba9\u81ea\u5b9a\u4e49\u5bf9\u8c61\u652f\u6301 &#043; &#8211; * \/ \u5927\u5c0f\u6bd4\u8f83\u3002<\/p>\n<h4>3.1 \u7b97\u672f\u8fd0\u7b97\u7b26\u91cd\u8f7d&#xff08;\u4ee5__add__\u4e3a\u4f8b&#xff09;<\/h4>\n<p>\u6ca1\u6709\u91cd\u5199 __add__&#xff0c;\u5bf9\u8c61\u4e4b\u95f4\u65e0\u6cd5\u76f8\u52a0&#xff0c;\u4ee3\u7801\u8fd0\u884c\u76f4\u63a5\u629b\u5f02\u5e38\u3002<span style=\"color:null\">\u00a0<\/span><\/p>\n<p>class Person:<br \/>\n    def __init__(self, height):<br \/>\n        self.height &#061; height<\/p>\n<p>p1 &#061; Person(170)<br \/>\np2 &#061; Person(180)<\/p>\n<p>print(p1 &#043; p2)<br \/>\n# \u76f4\u63a5\u5bf9\u8c61\u76f8\u52a0&#xff1a;\u65e0\u91cd\u8f7d \u3010\u5fc5\u62a5\u9519\u3011<br \/>\n# TypeError: unsupported operand type(s) for &#043;: &#039;Person&#039; and &#039;Person&#039;<\/p>\n<p><span style=\"color:null\">\u6709\u91cd\u8f7d&#xff08;\u91cd\u5199__add__&#xff09;\u2192 \u6b63\u5e38\u8fd0\u884c<\/span><\/p>\n<p>class Person:<br \/>\n    def __init__(self, height):<br \/>\n        self.height &#061; height<\/p>\n<p>    # \u91cd\u8f7d\u52a0\u6cd5\u8fd0\u7b97&#xff1a;\u81ea\u5b9a\u4e49\u5bf9\u8c61\u76f8\u52a0\u89c4\u5219<br \/>\n    def __add__(self, other):<br \/>\n        return self.height &#043; other.height<\/p>\n<p>p1 &#061; Person(170)<br \/>\np2 &#061; Person(180)<br \/>\nprint(&#034;\u91cd\u8f7d\u540e\u5bf9\u8c61\u76f8\u52a0\u7ed3\u679c&#xff1a;&#034;, p1 &#043; p2)  # \u81ea\u52a8\u89e6\u53d1 __add__<\/p>\n<h4>3.2\u903b\u8f91\u8fd0\u7b97\u7b26\u91cd\u8f7d&#xff08;\u4ee5__gt__\u4e3a\u4f8b&#xff09;<\/h4>\n<p>\u9664\u6570\u5b66\u8fd0\u7b97\u5916&#xff0c;\u81ea\u5b9a\u4e49\u5bf9\u8c61\u9ed8\u8ba4\u4e5f\u65e0\u6cd5\u6b63\u5e38\u6bd4\u8f83\u5927\u5c0f\u3001\u5224\u65ad\u76f8\u7b49&#xff0c;\u9700\u8981\u91cd\u8f7d\u5bf9\u5e94\u9b54\u672f\u65b9\u6cd5\u5b9e\u73b0\u529f\u80fd\u3002<\/p>\n<p>class Person:<br \/>\n    def __init__(self, age):<br \/>\n        self.age &#061; age<\/p>\n<p>    def __gt__(self, other):<br \/>\n        return self.age &gt; other.age<\/p>\n<p>p1 &#061; Person(20)<br \/>\np2 &#061; Person(18)<br \/>\nprint(p1 &gt; p2)  # True<\/p>\n<p>\u5bf9\u8c61\u9ed8\u8ba4\u7528\u5185\u5b58\u5730\u5740\u5224\u65ad\u76f8\u7b49&#xff0c;\u54ea\u6015\u5c5e\u6027\u5b8c\u5168\u4e00\u81f4\u4e5f\u5224\u5b9a\u4e3a\u4e0d\u76f8\u7b49&#xff1b;\u91cd\u5199 __eq__ \u53ef\u5b9e\u73b0\u6309\u5c5e\u6027\u5224\u7b49\u3002<\/p>\n<ul>\n<li>\n<p>__lt__&#xff1a;\u9002\u914d\u5c0f\u4e8e\u53f7 &lt; \u6bd4\u8f83<\/p>\n<\/li>\n<li>\n<p>__eq__&#xff1a;\u81ea\u5b9a\u4e49\u5bf9\u8c61\u76f8\u7b49\u5224\u65ad\u89c4\u5219&#xff0c;\u5224\u65ad\u4e24\u4e2a\u5bf9\u8c61\u662f\u5426\u76f8\u7b49&#xff08;\u9ed8\u8ba4\u6bd4\u5185\u5b58\u5730\u5740&#xff0c;\u91cd\u5199\u53ef\u6bd4\u5c5e\u6027&#xff09;<\/p>\n<\/li>\n<\/ul>\n<hr \/>\n<h3>4. \u5bb9\u5668\u7c7b\u9b54\u672f\u65b9\u6cd5&#xff08;\u5217\u8868\/\u5b57\u5178\u884c\u4e3a\u6a21\u62df&#xff09;<\/h3>\n<h4>4.1 __len__ \u957f\u5ea6<\/h4>\n<p>len(\u5bf9\u8c61) \u81ea\u52a8\u89e6\u53d1&#xff0c;\u5fc5\u987b\u8fd4\u56de\u6570\u5b57<\/p>\n<p>class MyList:<br \/>\n    def __init__(self, data):<br \/>\n        self.data &#061; data<\/p>\n<p>    def __len__(self):<br \/>\n        return len(self.data)<\/p>\n<p>obj &#061; MyList([1,2,3,4])<br \/>\nprint(len(obj)) # 4<\/p>\n<h4>4.2 __getitem__ \u53d6\u503c []<\/h4>\n<p>\u5bf9\u8c61[\u4e0b\u6807] \u81ea\u52a8\u89e6\u53d1<\/p>\n<p>class MyList:<br \/>\n    def __init__(self):<br \/>\n        self.data &#061; [10, 20, 30]<\/p>\n<p>    # \u5f00\u542f\u4e0b\u6807\u53d6\u503c\u529f\u80fd<br \/>\n    def __getitem__(self, index):<br \/>\n        # index \u5c31\u662f\u4f60\u4f20\u5165\u7684\u4e0b\u6807<br \/>\n        return self.data[index]<\/p>\n<p>obj &#061; MyList()<br \/>\nprint(obj[0])  # \u81ea\u52a8\u89e6\u53d1 __getitem__&#xff0c;\u8f93\u51fa 10<br \/>\nprint(obj[1])  # \u8f93\u51fa 20<br \/>\nprint(obj[2])  # \u8f93\u51fa 30<\/p>\n<h4>4.3 __setitem__ \u8d4b\u503c []<\/h4>\n<p>\u5bf9\u8c61[\u4e0b\u6807] &#061; \u503c \u81ea\u52a8\u89e6\u53d1<\/p>\n<p>class MyList:<br \/>\n    def __init__(self):<br \/>\n        self.arr &#061; [10,20,30]<\/p>\n<p>    # \u53d6\u503c<br \/>\n    def __getitem__(self, index):<br \/>\n        return self.arr[index]<\/p>\n<p>    # \u8d4b\u503c<br \/>\n    def __setitem__(self, index, value):<br \/>\n        self.arr[index] &#061; value<\/p>\n<p>m &#061; MyList()<br \/>\nprint(m[0])     # 10<br \/>\nm[1] &#061; 999<br \/>\nprint(m.arr)    # [10, 999, 30]<\/p>\n<hr \/>\n<h3>5. \u8fed\u4ee3\/\u4e0a\u4e0b\u6587\u9b54\u672f\u65b9\u6cd5<\/h3>\n<h4>5.1 __iter__ \u8fed\u4ee3\u5668<\/h4>\n<p>\u6838\u5fc3\u4f5c\u7528&#xff1a;\u8ba9\u81ea\u5b9a\u4e49\u7c7b\u5bf9\u8c61\u652f\u6301 for \u5faa\u73af\u904d\u5386&#xff0c;\u9ed8\u8ba4\u81ea\u5b9a\u4e49\u5bf9\u8c61\u65e0\u6cd5\u88ab\u8fed\u4ee3&#xff0c;\u91cd\u5199 __iter__ \u5373\u53ef\u62e5\u6709\u5217\u8868\u3001\u5143\u7ec4\u4e00\u6837\u7684\u904d\u5386\u80fd\u529b\u3002<\/p>\n<p>\u89e6\u53d1\u65f6\u673a&#xff1a;\u5bf9\u5bf9\u8c61\u6267\u884c for \u5faa\u73af\u8fed\u4ee3\u65f6\u81ea\u52a8\u89e6\u53d1<\/p>\n<p>\u4f7f\u7528\u89c4\u5219&#xff1a;__iter__ \u9700\u8981\u8fd4\u56de\u53ef\u8fed\u4ee3\u5bf9\u8c61&#xff0c;\u901a\u5e38\u914d\u5408 iter() \u51fd\u6570\u5b9e\u73b0<\/p>\n<p>class MyList:<br \/>\n    def __init__(self):<br \/>\n        self.data &#061; [11, 22, 33, 44]<\/p>\n<p>    # \u5f00\u542f\u5bf9\u8c61\u8fed\u4ee3\u80fd\u529b<br \/>\n    def __iter__(self):<br \/>\n        # \u8fd4\u56de\u53ef\u8fed\u4ee3\u5bf9\u8c61<br \/>\n        return iter(self.data)<\/p>\n<p># \u5b9e\u4f8b\u5316\u5bf9\u8c61<br \/>\nobj &#061; MyList()<\/p>\n<p># \u666e\u901afor\u5faa\u73af\u904d\u5386&#xff0c;\u81ea\u52a8\u89e6\u53d1 __iter__<br \/>\nfor item in obj:<br \/>\n    print(item)<\/p>\n<h4>5.2 __enter__ \/ __exit__ \u4e0a\u4e0b\u6587\u7ba1\u7406\u5668<\/h4>\n<p>\u6838\u5fc3\u4f5c\u7528&#xff1a;\u8ba9\u81ea\u5b9a\u4e49\u5bf9\u8c61\u652f\u6301 with \u8bed\u53e5 \u8bed\u6cd5&#xff0c;\u81ea\u52a8\u5b8c\u6210\u300c\u8d44\u6e90\u5f00\u542f &#043; \u8d44\u6e90\u5173\u95ed\u300d&#xff0c;\u675c\u7edd\u8d44\u6e90\u6cc4\u9732&#xff08;\u6587\u4ef6\u3001\u7f51\u7edc\u8fde\u63a5\u5e38\u7528&#xff09;\u3002<\/p>\n<p>\u89e6\u53d1\u65f6\u673a&#xff1a;<\/p>\n<ul>\n<li>\n<p>__enter__&#xff1a;\u8fdb\u5165 with \u4ee3\u7801\u5757\u65f6\u81ea\u52a8\u89e6\u53d1&#xff08;\u5f00\u542f\u8d44\u6e90&#xff09;<\/p>\n<\/li>\n<li>\n<p>__exit__&#xff1a;\u9000\u51fa with \u4ee3\u7801\u5757\u65f6\u81ea\u52a8\u89e6\u53d1&#xff08;\u5173\u95ed\u8d44\u6e90&#xff0c;\u54ea\u6015\u4ee3\u7801\u62a5\u9519\u4e5f\u4f1a\u6267\u884c&#xff09;<\/p>\n<\/li>\n<\/ul>\n<p>\u6838\u5fc3\u4f18\u52bf&#xff1a;\u65e0\u9700\u624b\u52a8\u5199\u5173\u95ed\u4ee3\u7801&#xff0c;\u81ea\u52a8\u6536\u5c3e&#xff0c;\u5b89\u5168\u6027\u6781\u9ad8\u3002<\/p>\n<p>class FileTool:<br \/>\n    # \u8fdb\u5165with\u5757\u81ea\u52a8\u6267\u884c&#xff1a;\u5f00\u542f\u8d44\u6e90<br \/>\n    def __enter__(self):<br \/>\n        print(&#034;1. \u81ea\u52a8\u5f00\u542f\u8d44\u6e90&#xff1a;\u6253\u5f00\u6587\u4ef6&#034;)<br \/>\n        return self  # \u8fd4\u56de\u5bf9\u8c61&#xff0c;\u7ed9as\u53d8\u91cf\u63a5\u6536<\/p>\n<p>    # \u9000\u51fawith\u5757\u81ea\u52a8\u6267\u884c&#xff1a;\u5173\u95ed\u8d44\u6e90<br \/>\n    def __exit__(self, exc_type, exc_val, exc_tb):<br \/>\n        print(&#034;2. \u81ea\u52a8\u5173\u95ed\u8d44\u6e90&#xff1a;\u5173\u95ed\u6587\u4ef6&#034;)<br \/>\n        # \u65e0\u8bba\u4ee3\u7801\u662f\u5426\u62a5\u9519&#xff0c;\u90fd\u4f1a\u6267\u884c\u6536\u5c3e\u64cd\u4f5c<\/p>\n<p>    def read_data(self):<br \/>\n        print(&#034;\u8bfb\u53d6\u6587\u4ef6\u6570\u636e&#034;)<\/p>\n<p># with\u8bed\u6cd5\u8fd0\u884c<br \/>\nwith FileTool() as tool:<br \/>\n    tool.read_data()<br \/>\n    print(&#034;\u6267\u884c\u6587\u4ef6\u64cd\u4f5c\u903b\u8f91&#034;)<\/p>\n<p>print(&#034;with\u4ee3\u7801\u5757\u7ed3\u675f&#xff0c;\u8d44\u6e90\u81ea\u52a8\u91ca\u653e\u5b8c\u6bd5&#034;)<\/p>\n<p>\u8fd0\u884c\u6d41\u7a0b&#xff1a;\u8fdb\u5165with\u89e6\u53d1__enter__ \u2192 \u6267\u884c\u5185\u90e8\u4e1a\u52a1\u4ee3\u7801 \u2192 \u9000\u51fawith\u89e6\u53d1__exit__\u81ea\u52a8\u6536\u5c3e<\/p>\n<p>\u573a\u666f&#xff1a;\u6587\u4ef6\u8bfb\u5199\u3001\u6570\u636e\u5e93\u8fde\u63a5\u3001\u7f51\u7edc\u8bf7\u6c42\u8fde\u63a5&#xff0c;\u7edf\u4e00\u7528\u4e0a\u4e0b\u6587\u7ba1\u7406\u5668\u81ea\u52a8\u91ca\u653e\u8d44\u6e90\u3002<\/p>\n<hr \/>\n<h3>6. \u9b54\u672f\u65b9\u6cd5\u6c47\u603b<\/h3>\n<table>\n<tr>\n<p>\u9b54\u672f\u65b9\u6cd5<\/p>\n<p>\u89e6\u53d1\u573a\u666f<\/p>\n<p>\u6838\u5fc3\u4f5c\u7528<\/p>\n<\/tr>\n<tbody>\n<tr>\n<td colspan=\"1\" rowspan=\"1\">\n<p>__init__<\/p>\n<\/td>\n<td colspan=\"1\" rowspan=\"1\">\n<p>\u521b\u5efa\u5bf9\u8c61<\/p>\n<\/td>\n<td colspan=\"1\" rowspan=\"1\">\n<p>\u521d\u59cb\u5316\u5c5e\u6027<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td colspan=\"1\" rowspan=\"1\">\n<p>__new__<\/p>\n<\/td>\n<td colspan=\"1\" rowspan=\"1\">\n<p>\u521b\u5efa\u5bf9\u8c61\u6700\u5148\u6267\u884c<\/p>\n<\/td>\n<td colspan=\"1\" rowspan=\"1\">\n<p>\u5f00\u8f9f\u5185\u5b58\u3001\u9020\u5bf9\u8c61<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td colspan=\"1\" rowspan=\"1\">\n<p>__str__<\/p>\n<\/td>\n<td colspan=\"1\" rowspan=\"1\">\n<p>print(\u5bf9\u8c61)<\/p>\n<\/td>\n<td colspan=\"1\" rowspan=\"1\">\n<p>\u81ea\u5b9a\u4e49\u6253\u5370\u5185\u5bb9<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td colspan=\"1\" rowspan=\"1\">\n<p>__del__<\/p>\n<\/td>\n<td colspan=\"1\" rowspan=\"1\">\n<p>\u5bf9\u8c61\u9500\u6bc1<\/p>\n<\/td>\n<td colspan=\"1\" rowspan=\"1\">\n<p>\u91ca\u653e\u8d44\u6e90<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td colspan=\"1\" rowspan=\"1\">\n<p>__add__<\/p>\n<\/td>\n<td colspan=\"1\" rowspan=\"1\">\n<p>\u5bf9\u8c61 &#043; \u5bf9\u8c61<\/p>\n<\/td>\n<td colspan=\"1\" rowspan=\"1\">\n<p>\u52a0\u6cd5\u91cd\u8f7d<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td colspan=\"1\" rowspan=\"1\">\n<p>__sub__<\/p>\n<\/td>\n<td colspan=\"1\" rowspan=\"1\">\n<p>\u5bf9\u8c61 &#8211; \u5bf9\u8c61<\/p>\n<\/td>\n<td colspan=\"1\" rowspan=\"1\">\n<p>\u51cf\u6cd5\u91cd\u8f7d<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td colspan=\"1\" rowspan=\"1\">\n<p>__gt__<\/p>\n<\/td>\n<td colspan=\"1\" rowspan=\"1\">\n<p>\u5bf9\u8c61 &gt; \u5bf9\u8c61<\/p>\n<\/td>\n<td colspan=\"1\" rowspan=\"1\">\n<p>\u5927\u4e8e\u6bd4\u8f83<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td colspan=\"1\" rowspan=\"1\">\n<p>__eq__<\/p>\n<\/td>\n<td colspan=\"1\" rowspan=\"1\">\n<p>\u5bf9\u8c61 &#061;&#061; \u5bf9\u8c61<\/p>\n<\/td>\n<td colspan=\"1\" rowspan=\"1\">\n<p>\u76f8\u7b49\u5224\u65ad<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td colspan=\"1\" rowspan=\"1\">\n<p>__len__<\/p>\n<\/td>\n<td colspan=\"1\" rowspan=\"1\">\n<p>len(\u5bf9\u8c61)<\/p>\n<\/td>\n<td colspan=\"1\" rowspan=\"1\">\n<p>\u83b7\u53d6\u81ea\u5b9a\u4e49\u957f\u5ea6<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td colspan=\"1\" rowspan=\"1\">\n<p>__getitem__<\/p>\n<\/td>\n<td colspan=\"1\" rowspan=\"1\">\n<p>\u5bf9\u8c61[x]<\/p>\n<\/td>\n<td colspan=\"1\" rowspan=\"1\">\n<p>\u4e0b\u6807\u53d6\u503c<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td colspan=\"1\" rowspan=\"1\">\n<p>__setitem__<\/p>\n<\/td>\n<td colspan=\"1\" rowspan=\"1\">\n<p>\u5bf9\u8c61[x]&#061;y<\/p>\n<\/td>\n<td colspan=\"1\" rowspan=\"1\">\n<p>\u4e0b\u6807\u8d4b\u503c<\/p>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<ul>\n<li>\n<p>\u9b54\u672f\u65b9\u6cd5 \u81ea\u52a8\u89e6\u53d1&#xff0c;\u4e0d\u7528\u624b\u52a8\u8c03\u7528<\/p>\n<\/li>\n<li>\n<p>\u9ed8\u8ba4\u60c5\u51b5\u4e0b&#xff0c;&#061;&#061; \u5bf9\u6bd4\u7684\u662f\u5185\u5b58\u5730\u5740&#xff1b;\u91cd\u5199 __eq__ \u624d\u80fd\u5bf9\u6bd4\u5c5e\u6027<\/p>\n<\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>1. \u57fa\u7840\u6982\u5ff51.1 \u4ec0\u4e48\u662f\u9b54\u672f\u65b9\u6cd5\u9b54\u672f\u65b9\u6cd5\u662f Python \u5b98\u65b9\u5185\u7f6e\u7684\u7279\u6b8a\u65b9\u6cd5&#xff0c;\u7edf\u4e00\u8bed\u6cd5\u683c\u5f0f\u4e3a \u524d\u540e\u53cc\u4e0b\u5212\u7ebf\u5305\u88f9&#xff1a;__xxx__&#xff0c;\u662f\u9762\u5411\u5bf9\u8c61\u7f16\u7a0b\u7684\u6838\u5fc3\u5e95\u5c42\u8bed\u6cd5\u3002\u5b83\u65e0\u9700\u5f00\u53d1\u8005\u624b\u52a8\u8c03\u7528&#xff0c;\u4f1a\u5728\u5bf9\u5e94\u4ee3\u7801\u573a\u666f\u89e6\u53d1\u65f6\u81ea\u52a8\u6267\u884c&#xff0c;\u4e13\u95e8\u7528\u4e8e\u6539\u5199\u81ea\u5b9a\u4e49\u7c7b\u7684\u9ed8\u8ba4\u5e95\u5c42\u884c\u4e3a\u30021.2 \u4e3a\u4ec0\u4e48\u9700\u8981\u9b54\u672f\u65b9\u6cd5Python \u539f\u751f\u4ec5\u652f\u6301\u5b57\u7b26\u4e32\u3001\u5217\u8868\u3001\u6570\u5b57\u7b49\u5185\u7f6e\u6570\u636e\u7c7b\u578b\u7684\u6253\u5370\u3001\u8fd0\u7b97\u3001\u4e0b\u6807\u53d6\u503c\u3001\u957f\u5ea6\u83b7\u53d6\u7b49\u64cd\u4f5c\u3002\u800c\u6211\u4eec\u81ea\u5df1\u5b9a\u4e49\u7684\u7c7b\u5bf9\u8c61&amp;#xff0c<\/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,305],"topic":[],"class_list":["post-105977","post","type-post","status-publish","format-standard","hentry","category-server","tag-python","tag-305"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v20.3 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Python\u2014\u2014\u9b54\u672f\u65b9\u6cd5 - \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\/105977.html\" \/>\n<meta property=\"og:locale\" content=\"zh_CN\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Python\u2014\u2014\u9b54\u672f\u65b9\u6cd5 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"og:description\" content=\"1. \u57fa\u7840\u6982\u5ff51.1 \u4ec0\u4e48\u662f\u9b54\u672f\u65b9\u6cd5\u9b54\u672f\u65b9\u6cd5\u662f Python \u5b98\u65b9\u5185\u7f6e\u7684\u7279\u6b8a\u65b9\u6cd5&#xff0c;\u7edf\u4e00\u8bed\u6cd5\u683c\u5f0f\u4e3a \u524d\u540e\u53cc\u4e0b\u5212\u7ebf\u5305\u88f9&#xff1a;__xxx__&#xff0c;\u662f\u9762\u5411\u5bf9\u8c61\u7f16\u7a0b\u7684\u6838\u5fc3\u5e95\u5c42\u8bed\u6cd5\u3002\u5b83\u65e0\u9700\u5f00\u53d1\u8005\u624b\u52a8\u8c03\u7528&#xff0c;\u4f1a\u5728\u5bf9\u5e94\u4ee3\u7801\u573a\u666f\u89e6\u53d1\u65f6\u81ea\u52a8\u6267\u884c&#xff0c;\u4e13\u95e8\u7528\u4e8e\u6539\u5199\u81ea\u5b9a\u4e49\u7c7b\u7684\u9ed8\u8ba4\u5e95\u5c42\u884c\u4e3a\u30021.2 \u4e3a\u4ec0\u4e48\u9700\u8981\u9b54\u672f\u65b9\u6cd5Python \u539f\u751f\u4ec5\u652f\u6301\u5b57\u7b26\u4e32\u3001\u5217\u8868\u3001\u6570\u5b57\u7b49\u5185\u7f6e\u6570\u636e\u7c7b\u578b\u7684\u6253\u5370\u3001\u8fd0\u7b97\u3001\u4e0b\u6807\u53d6\u503c\u3001\u957f\u5ea6\u83b7\u53d6\u7b49\u64cd\u4f5c\u3002\u800c\u6211\u4eec\u81ea\u5df1\u5b9a\u4e49\u7684\u7c7b\u5bf9\u8c61&amp;#xff0c\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.wsisp.com\/helps\/105977.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-16T05:50:38+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=\"3 \u5206\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/105977.html\",\"url\":\"https:\/\/www.wsisp.com\/helps\/105977.html\",\"name\":\"Python\u2014\u2014\u9b54\u672f\u65b9\u6cd5 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\",\"isPartOf\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#website\"},\"datePublished\":\"2026-09-16T05:50:38+00:00\",\"dateModified\":\"2026-09-16T05:50:38+00:00\",\"author\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41\"},\"breadcrumb\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/105977.html#breadcrumb\"},\"inLanguage\":\"zh-Hans\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.wsisp.com\/helps\/105977.html\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/105977.html#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"\u9996\u9875\",\"item\":\"https:\/\/www.wsisp.com\/helps\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Python\u2014\u2014\u9b54\u672f\u65b9\u6cd5\"}]},{\"@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\u2014\u2014\u9b54\u672f\u65b9\u6cd5 - \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\/105977.html","og_locale":"zh_CN","og_type":"article","og_title":"Python\u2014\u2014\u9b54\u672f\u65b9\u6cd5 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","og_description":"1. \u57fa\u7840\u6982\u5ff51.1 \u4ec0\u4e48\u662f\u9b54\u672f\u65b9\u6cd5\u9b54\u672f\u65b9\u6cd5\u662f Python \u5b98\u65b9\u5185\u7f6e\u7684\u7279\u6b8a\u65b9\u6cd5&#xff0c;\u7edf\u4e00\u8bed\u6cd5\u683c\u5f0f\u4e3a \u524d\u540e\u53cc\u4e0b\u5212\u7ebf\u5305\u88f9&#xff1a;__xxx__&#xff0c;\u662f\u9762\u5411\u5bf9\u8c61\u7f16\u7a0b\u7684\u6838\u5fc3\u5e95\u5c42\u8bed\u6cd5\u3002\u5b83\u65e0\u9700\u5f00\u53d1\u8005\u624b\u52a8\u8c03\u7528&#xff0c;\u4f1a\u5728\u5bf9\u5e94\u4ee3\u7801\u573a\u666f\u89e6\u53d1\u65f6\u81ea\u52a8\u6267\u884c&#xff0c;\u4e13\u95e8\u7528\u4e8e\u6539\u5199\u81ea\u5b9a\u4e49\u7c7b\u7684\u9ed8\u8ba4\u5e95\u5c42\u884c\u4e3a\u30021.2 \u4e3a\u4ec0\u4e48\u9700\u8981\u9b54\u672f\u65b9\u6cd5Python \u539f\u751f\u4ec5\u652f\u6301\u5b57\u7b26\u4e32\u3001\u5217\u8868\u3001\u6570\u5b57\u7b49\u5185\u7f6e\u6570\u636e\u7c7b\u578b\u7684\u6253\u5370\u3001\u8fd0\u7b97\u3001\u4e0b\u6807\u53d6\u503c\u3001\u957f\u5ea6\u83b7\u53d6\u7b49\u64cd\u4f5c\u3002\u800c\u6211\u4eec\u81ea\u5df1\u5b9a\u4e49\u7684\u7c7b\u5bf9\u8c61&amp;#xff0c","og_url":"https:\/\/www.wsisp.com\/helps\/105977.html","og_site_name":"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","article_published_time":"2026-09-16T05:50:38+00:00","author":"admin","twitter_card":"summary_large_image","twitter_misc":{"\u4f5c\u8005":"admin","\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4":"3 \u5206"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.wsisp.com\/helps\/105977.html","url":"https:\/\/www.wsisp.com\/helps\/105977.html","name":"Python\u2014\u2014\u9b54\u672f\u65b9\u6cd5 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","isPartOf":{"@id":"https:\/\/www.wsisp.com\/helps\/#website"},"datePublished":"2026-09-16T05:50:38+00:00","dateModified":"2026-09-16T05:50:38+00:00","author":{"@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41"},"breadcrumb":{"@id":"https:\/\/www.wsisp.com\/helps\/105977.html#breadcrumb"},"inLanguage":"zh-Hans","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.wsisp.com\/helps\/105977.html"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.wsisp.com\/helps\/105977.html#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"\u9996\u9875","item":"https:\/\/www.wsisp.com\/helps"},{"@type":"ListItem","position":2,"name":"Python\u2014\u2014\u9b54\u672f\u65b9\u6cd5"}]},{"@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\/105977","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=105977"}],"version-history":[{"count":0,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts\/105977\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/media?parent=105977"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/categories?post=105977"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/tags?post=105977"},{"taxonomy":"topic","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/topic?post=105977"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}