{"id":1148205,"date":"2025-01-13T16:34:23","date_gmt":"2025-01-13T08:34:23","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1148205.html"},"modified":"2025-01-13T16:34:25","modified_gmt":"2025-01-13T08:34:25","slug":"python%e4%b8%ad%e5%a6%82%e4%bd%95%e8%b0%83%e7%94%a8%e5%af%b9%e8%b1%a1","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1148205.html","title":{"rendered":"python\u4e2d\u5982\u4f55\u8c03\u7528\u5bf9\u8c61"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25170408\/dd701a42-bedd-42af-b207-6606642da0cb.webp\" alt=\"python\u4e2d\u5982\u4f55\u8c03\u7528\u5bf9\u8c61\" \/><\/p>\n<p><p> <strong>Python\u4e2d\u8c03\u7528\u5bf9\u8c61\u7684\u65b9\u6cd5\u6709\uff1a\u8c03\u7528\u5bf9\u8c61\u7684\u5c5e\u6027\u3001\u8c03\u7528\u5bf9\u8c61\u7684\u65b9\u6cd5\u3001\u4f7f\u7528\u5bf9\u8c61\u4f5c\u4e3a\u53c2\u6570\u3001\u7ee7\u627f\u5bf9\u8c61\u3001\u7ec4\u5408\u5bf9\u8c61\u7b49\u3002<\/strong>\u8fd9\u4e9b\u65b9\u6cd5\u4e2d\u7684\u6bcf\u4e00\u79cd\u90fd\u53ef\u4ee5\u7528\u4e8e\u4e0d\u540c\u7684\u7f16\u7a0b\u9700\u6c42\uff0c\u5e2e\u52a9\u5f00\u53d1\u8005\u6709\u6548\u5730\u7ec4\u7ec7\u548c\u4f7f\u7528\u4ee3\u7801\u3002<\/p>\n<\/p>\n<p><p>\u8be6\u7ec6\u63cf\u8ff0\uff1a\u8c03\u7528\u5bf9\u8c61\u7684\u5c5e\u6027\u662f\u6307\u901a\u8fc7\u70b9\u64cd\u4f5c\u7b26\u8bbf\u95ee\u5bf9\u8c61\u5185\u90e8\u7684\u5c5e\u6027\u503c\u3002\u6bd4\u5982\uff0c\u5982\u679c\u6211\u4eec\u6709\u4e00\u4e2a\u5bf9\u8c61<code>person<\/code>\uff0c\u5b83\u6709\u4e00\u4e2a\u5c5e\u6027<code>name<\/code>\uff0c\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7<code>person.name<\/code>\u6765\u83b7\u53d6\u6216\u4fee\u6539\u8fd9\u4e2a\u5c5e\u6027\u7684\u503c\u3002<\/p>\n<\/p>\n<hr>\n<h2><strong>\u4e00\u3001\u8c03\u7528\u5bf9\u8c61\u7684\u5c5e\u6027<\/strong><\/h2>\n<p><p>\u5728Python\u4e2d\uff0c\u7c7b\u662f\u7528\u6765\u521b\u5efa\u5bf9\u8c61\u7684\u84dd\u56fe\u3002\u6bcf\u4e2a\u7c7b\u53ef\u4ee5\u5b9a\u4e49\u4e0d\u540c\u7684\u5c5e\u6027\uff0c\u8fd9\u4e9b\u5c5e\u6027\u53ef\u4ee5\u901a\u8fc7\u5bf9\u8c61\u6765\u8bbf\u95ee\u3002\u4e0b\u9762\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u4f8b\u5b50\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">class Person:<\/p>\n<p>    def __init__(self, name, age):<\/p>\n<p>        self.name = name<\/p>\n<p>        self.age = age<\/p>\n<h2><strong>\u521b\u5efa\u5bf9\u8c61<\/strong><\/h2>\n<p>person1 = Person(&quot;John&quot;, 30)<\/p>\n<p>person2 = Person(&quot;Jane&quot;, 25)<\/p>\n<h2><strong>\u8c03\u7528\u5bf9\u8c61\u7684\u5c5e\u6027<\/strong><\/h2>\n<p>print(person1.name)  # \u8f93\u51fa\uff1aJohn<\/p>\n<p>print(person2.age)   # \u8f93\u51fa\uff1a25<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u5b9a\u4e49\u4e86\u4e00\u4e2a<code>Person<\/code>\u7c7b\uff0c\u5e76\u901a\u8fc7<code>__init__<\/code>\u65b9\u6cd5\u521d\u59cb\u5316\u4e86<code>name<\/code>\u548c<code>age<\/code>\u4e24\u4e2a\u5c5e\u6027\u3002\u7136\u540e\uff0c\u6211\u4eec\u521b\u5efa\u4e86\u4e24\u4e2a\u5bf9\u8c61<code>person1<\/code>\u548c<code>person2<\/code>\uff0c\u5e76\u901a\u8fc7\u5bf9\u8c61\u540d\u52a0\u70b9\u64cd\u4f5c\u7b26\u8bbf\u95ee\u5b83\u4eec\u7684\u5c5e\u6027\u3002<\/p>\n<\/p>\n<h2><strong>\u4e8c\u3001\u8c03\u7528\u5bf9\u8c61\u7684\u65b9\u6cd5<\/strong><\/h2>\n<p><p>\u7c7b\u4e0d\u4ec5\u53ef\u4ee5\u5305\u542b\u5c5e\u6027\uff0c\u8fd8\u53ef\u4ee5\u5305\u542b\u65b9\u6cd5\u3002\u65b9\u6cd5\u662f\u5b9a\u4e49\u5728\u7c7b\u5185\u90e8\u7684\u51fd\u6570\uff0c\u53ef\u4ee5\u901a\u8fc7\u5bf9\u8c61\u6765\u8c03\u7528\u3002\u4e0b\u9762\u662f\u4e00\u4e2a\u4f8b\u5b50\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">class Dog:<\/p>\n<p>    def __init__(self, name):<\/p>\n<p>        self.name = name<\/p>\n<p>    def bark(self):<\/p>\n<p>        print(f&quot;{self.name} is barking!&quot;)<\/p>\n<h2><strong>\u521b\u5efa\u5bf9\u8c61<\/strong><\/h2>\n<p>dog1 = Dog(&quot;Buddy&quot;)<\/p>\n<p>dog2 = Dog(&quot;Lucy&quot;)<\/p>\n<h2><strong>\u8c03\u7528\u5bf9\u8c61\u7684\u65b9\u6cd5<\/strong><\/h2>\n<p>dog1.bark()  # \u8f93\u51fa\uff1aBuddy is barking!<\/p>\n<p>dog2.bark()  # \u8f93\u51fa\uff1aLucy is barking!<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u5b9a\u4e49\u4e86\u4e00\u4e2a<code>Dog<\/code>\u7c7b\uff0c\u5e76\u5728\u5176\u4e2d\u5b9a\u4e49\u4e86\u4e00\u4e2a\u65b9\u6cd5<code>bark<\/code>\u3002\u6211\u4eec\u521b\u5efa\u4e86\u4e24\u4e2a\u5bf9\u8c61<code>dog1<\/code>\u548c<code>dog2<\/code>\uff0c\u5e76\u901a\u8fc7\u5bf9\u8c61\u540d\u52a0\u70b9\u64cd\u4f5c\u7b26\u8c03\u7528\u5b83\u4eec\u7684\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<h2><strong>\u4e09\u3001\u4f7f\u7528\u5bf9\u8c61\u4f5c\u4e3a\u53c2\u6570<\/strong><\/h2>\n<p><p>\u5728Python\u4e2d\uff0c\u6211\u4eec\u53ef\u4ee5\u5c06\u5bf9\u8c61\u4f5c\u4e3a\u53c2\u6570\u4f20\u9012\u7ed9\u51fd\u6570\u6216\u65b9\u6cd5\u3002\u8fd9\u6837\u53ef\u4ee5\u5b9e\u73b0\u66f4\u7075\u6d3b\u7684\u4ee3\u7801\u8bbe\u8ba1\u3002\u4e0b\u9762\u662f\u4e00\u4e2a\u4f8b\u5b50\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">class Car:<\/p>\n<p>    def __init__(self, brand, model):<\/p>\n<p>        self.brand = brand<\/p>\n<p>        self.model = model<\/p>\n<p>def print_car_info(car):<\/p>\n<p>    print(f&quot;Brand: {car.brand}, Model: {car.model}&quot;)<\/p>\n<h2><strong>\u521b\u5efa\u5bf9\u8c61<\/strong><\/h2>\n<p>car1 = Car(&quot;Toyota&quot;, &quot;Camry&quot;)<\/p>\n<p>car2 = Car(&quot;Honda&quot;, &quot;Civic&quot;)<\/p>\n<h2><strong>\u4f7f\u7528\u5bf9\u8c61\u4f5c\u4e3a\u53c2\u6570<\/strong><\/h2>\n<p>print_car_info(car1)  # \u8f93\u51fa\uff1aBrand: Toyota, Model: Camry<\/p>\n<p>print_car_info(car2)  # \u8f93\u51fa\uff1aBrand: Honda, Model: Civic<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u5b9a\u4e49\u4e86\u4e00\u4e2a<code>Car<\/code>\u7c7b\uff0c\u5e76\u5728\u5176\u4e2d\u5b9a\u4e49\u4e86<code>brand<\/code>\u548c<code>model<\/code>\u4e24\u4e2a\u5c5e\u6027\u3002\u6211\u4eec\u8fd8\u5b9a\u4e49\u4e86\u4e00\u4e2a\u51fd\u6570<code>print_car_info<\/code>\uff0c\u5b83\u63a5\u53d7\u4e00\u4e2a<code>Car<\/code>\u5bf9\u8c61\u4f5c\u4e3a\u53c2\u6570\uff0c\u5e76\u6253\u5370\u8be5\u5bf9\u8c61\u7684\u5c5e\u6027\u4fe1\u606f\u3002<\/p>\n<\/p>\n<h2><strong>\u56db\u3001\u7ee7\u627f\u5bf9\u8c61<\/strong><\/h2>\n<p><p>\u7ee7\u627f\u662f\u9762\u5411\u5bf9\u8c61\u7f16\u7a0b\u7684\u91cd\u8981\u6982\u5ff5\u3002\u901a\u8fc7\u7ee7\u627f\uff0c\u6211\u4eec\u53ef\u4ee5\u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u7c7b\uff0c\u8fd9\u4e2a\u7c7b\u53ef\u4ee5\u7ee7\u627f\u53e6\u4e00\u4e2a\u7c7b\u7684\u5c5e\u6027\u548c\u65b9\u6cd5\u3002\u4e0b\u9762\u662f\u4e00\u4e2a\u4f8b\u5b50\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">class Animal:<\/p>\n<p>    def __init__(self, name):<\/p>\n<p>        self.name = name<\/p>\n<p>    def speak(self):<\/p>\n<p>        pass<\/p>\n<p>class Dog(Animal):<\/p>\n<p>    def speak(self):<\/p>\n<p>        print(f&quot;{self.name} says Woof!&quot;)<\/p>\n<p>class Cat(Animal):<\/p>\n<p>    def speak(self):<\/p>\n<p>        print(f&quot;{self.name} says Meow!&quot;)<\/p>\n<h2><strong>\u521b\u5efa\u5bf9\u8c61<\/strong><\/h2>\n<p>dog = Dog(&quot;Buddy&quot;)<\/p>\n<p>cat = Cat(&quot;Whiskers&quot;)<\/p>\n<h2><strong>\u8c03\u7528\u7ee7\u627f\u7684\u65b9\u6cd5<\/strong><\/h2>\n<p>dog.speak()  # \u8f93\u51fa\uff1aBuddy says Woof!<\/p>\n<p>cat.speak()  # \u8f93\u51fa\uff1aWhiskers says Meow!<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u5b9a\u4e49\u4e86\u4e00\u4e2a<code>Animal<\/code>\u7c7b\uff0c\u5e76\u5728\u5176\u4e2d\u5b9a\u4e49\u4e86\u4e00\u4e2a\u65b9\u6cd5<code>speak<\/code>\u3002\u7136\u540e\uff0c\u6211\u4eec\u521b\u5efa\u4e86\u4e24\u4e2a\u5b50\u7c7b<code>Dog<\/code>\u548c<code>Cat<\/code>\uff0c\u5b83\u4eec\u5206\u522b\u7ee7\u627f\u4e86<code>Animal<\/code>\u7c7b\uff0c\u5e76\u91cd\u5199\u4e86<code>speak<\/code>\u65b9\u6cd5\u3002\u901a\u8fc7\u7ee7\u627f\uff0c\u6211\u4eec\u53ef\u4ee5\u66f4\u597d\u5730\u7ec4\u7ec7\u4ee3\u7801\uff0c\u4f7f\u5176\u66f4\u5177\u53ef\u91cd\u7528\u6027\u548c\u6269\u5c55\u6027\u3002<\/p>\n<\/p>\n<h2><strong>\u4e94\u3001\u7ec4\u5408\u5bf9\u8c61<\/strong><\/h2>\n<p><p>\u7ec4\u5408\u662f\u53e6\u4e00\u79cd\u9762\u5411\u5bf9\u8c61\u7f16\u7a0b\u7684\u8bbe\u8ba1\u6a21\u5f0f\uff0c\u901a\u8fc7\u7ec4\u5408\uff0c\u6211\u4eec\u53ef\u4ee5\u5c06\u4e00\u4e2a\u7c7b\u7684\u5bf9\u8c61\u5d4c\u5165\u5230\u53e6\u4e00\u4e2a\u7c7b\u4e2d\uff0c\u4ece\u800c\u6784\u5efa\u66f4\u590d\u6742\u7684\u5bf9\u8c61\u3002\u4e0b\u9762\u662f\u4e00\u4e2a\u4f8b\u5b50\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">class Engine:<\/p>\n<p>    def __init__(self, horsepower):<\/p>\n<p>        self.horsepower = horsepower<\/p>\n<p>class Car:<\/p>\n<p>    def __init__(self, brand, model, engine):<\/p>\n<p>        self.brand = brand<\/p>\n<p>        self.model = model<\/p>\n<p>        self.engine = engine<\/p>\n<h2><strong>\u521b\u5efa\u5bf9\u8c61<\/strong><\/h2>\n<p>engine1 = Engine(150)<\/p>\n<p>car1 = Car(&quot;Toyota&quot;, &quot;Camry&quot;, engine1)<\/p>\n<h2><strong>\u8c03\u7528\u7ec4\u5408\u5bf9\u8c61\u7684\u5c5e\u6027<\/strong><\/h2>\n<p>print(car1.brand)         # \u8f93\u51fa\uff1aToyota<\/p>\n<p>print(car1.engine.horsepower)  # \u8f93\u51fa\uff1a150<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u5b9a\u4e49\u4e86\u4e00\u4e2a<code>Engine<\/code>\u7c7b\uff0c\u5e76\u5728\u5176\u4e2d\u5b9a\u4e49\u4e86\u4e00\u4e2a\u5c5e\u6027<code>horsepower<\/code>\u3002\u7136\u540e\uff0c\u6211\u4eec\u5b9a\u4e49\u4e86\u4e00\u4e2a<code>Car<\/code>\u7c7b\uff0c\u5e76\u5728\u5176\u4e2d\u5b9a\u4e49\u4e86\u4e00\u4e2a\u5c5e\u6027<code>engine<\/code>\uff0c\u5b83\u662f<code>Engine<\/code>\u7c7b\u7684\u4e00\u4e2a\u5b9e\u4f8b\u3002\u901a\u8fc7\u7ec4\u5408\uff0c\u6211\u4eec\u53ef\u4ee5\u5c06<code>Engine<\/code>\u5bf9\u8c61\u5d4c\u5165\u5230<code>Car<\/code>\u5bf9\u8c61\u4e2d\uff0c\u4ece\u800c\u521b\u5efa\u4e00\u4e2a\u66f4\u590d\u6742\u7684\u5bf9\u8c61\u3002<\/p>\n<\/p>\n<h2><strong>\u516d\u3001\u5bf9\u8c61\u7684\u9b54\u672f\u65b9\u6cd5<\/strong><\/h2>\n<p><p>Python\u63d0\u4f9b\u4e86\u4e00\u4e9b\u7279\u6b8a\u7684\u65b9\u6cd5\uff0c\u8fd9\u4e9b\u65b9\u6cd5\u4ee5\u53cc\u4e0b\u5212\u7ebf\u5f00\u5934\u548c\u7ed3\u5c3e\uff0c\u901a\u5e38\u79f0\u4e3a\u9b54\u672f\u65b9\u6cd5\u6216\u7279\u6b8a\u65b9\u6cd5\u3002\u8fd9\u4e9b\u65b9\u6cd5\u5141\u8bb8\u6211\u4eec\u5b9a\u4e49\u5bf9\u8c61\u7684\u884c\u4e3a\uff0c\u4f7f\u5176\u66f4\u5177\u8868\u73b0\u529b\u3002\u4e0b\u9762\u662f\u51e0\u4e2a\u5e38\u89c1\u7684\u9b54\u672f\u65b9\u6cd5\uff1a<\/p>\n<\/p>\n<p><h2>1\u3001<code>__str__<\/code>\u548c<code>__repr__<\/code>\u65b9\u6cd5<\/h2>\n<\/p>\n<p><p><code>__str__<\/code>\u65b9\u6cd5\u7528\u4e8e\u5b9a\u4e49\u5bf9\u8c61\u7684\u5b57\u7b26\u4e32\u8868\u793a\uff0c\u5f53\u6211\u4eec\u4f7f\u7528<code>print<\/code>\u51fd\u6570\u6216<code>str<\/code>\u51fd\u6570\u65f6\u4f1a\u8c03\u7528\u8fd9\u4e2a\u65b9\u6cd5\u3002<code>__repr__<\/code>\u65b9\u6cd5\u7528\u4e8e\u5b9a\u4e49\u5bf9\u8c61\u7684\u6b63\u5f0f\u5b57\u7b26\u4e32\u8868\u793a\uff0c\u4e3b\u8981\u7528\u4e8e\u8c03\u8bd5\u548c\u5f00\u53d1\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">class Person:<\/p>\n<p>    def __init__(self, name, age):<\/p>\n<p>        self.name = name<\/p>\n<p>        self.age = age<\/p>\n<p>    def __str__(self):<\/p>\n<p>        return f&quot;Person(name={self.name}, age={self.age})&quot;<\/p>\n<p>    def __repr__(self):<\/p>\n<p>        return f&quot;Person(name={self.name}, age={self.age})&quot;<\/p>\n<h2><strong>\u521b\u5efa\u5bf9\u8c61<\/strong><\/h2>\n<p>person = Person(&quot;John&quot;, 30)<\/p>\n<h2><strong>\u8c03\u7528\u9b54\u672f\u65b9\u6cd5<\/strong><\/h2>\n<p>print(person)  # \u8f93\u51fa\uff1aPerson(name=John, age=30)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u5b9a\u4e49\u4e86<code>__str__<\/code>\u548c<code>__repr__<\/code>\u65b9\u6cd5\uff0c\u4f7f\u5f97<code>Person<\/code>\u5bf9\u8c61\u5728\u6253\u5370\u65f6\u5177\u6709\u66f4\u53cb\u597d\u7684\u5b57\u7b26\u4e32\u8868\u793a\u3002<\/p>\n<\/p>\n<p><h2>2\u3001<code>__add__<\/code>\u65b9\u6cd5<\/h2>\n<\/p>\n<p><p><code>__add__<\/code>\u65b9\u6cd5\u7528\u4e8e\u5b9a\u4e49\u5bf9\u8c61\u7684\u52a0\u6cd5\u884c\u4e3a\uff0c\u4f7f\u5176\u652f\u6301\u4f7f\u7528<code>+<\/code>\u8fd0\u7b97\u7b26\u8fdb\u884c\u52a0\u6cd5\u64cd\u4f5c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">class Vector:<\/p>\n<p>    def __init__(self, x, y):<\/p>\n<p>        self.x = x<\/p>\n<p>        self.y = y<\/p>\n<p>    def __add__(self, other):<\/p>\n<p>        return Vector(self.x + other.x, self.y + other.y)<\/p>\n<p>    def __repr__(self):<\/p>\n<p>        return f&quot;Vector({self.x}, {self.y})&quot;<\/p>\n<h2><strong>\u521b\u5efa\u5bf9\u8c61<\/strong><\/h2>\n<p>v1 = Vector(1, 2)<\/p>\n<p>v2 = Vector(3, 4)<\/p>\n<h2><strong>\u8c03\u7528\u9b54\u672f\u65b9\u6cd5<\/strong><\/h2>\n<p>v3 = v1 + v2<\/p>\n<p>print(v3)  # \u8f93\u51fa\uff1aVector(4, 6)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u5b9a\u4e49\u4e86<code>__add__<\/code>\u65b9\u6cd5\uff0c\u4f7f\u5f97<code>Vector<\/code>\u5bf9\u8c61\u652f\u6301\u4f7f\u7528<code>+<\/code>\u8fd0\u7b97\u7b26\u8fdb\u884c\u52a0\u6cd5\u64cd\u4f5c\u3002<\/p>\n<\/p>\n<p><h2>3\u3001<code>__getitem__<\/code>\u548c<code>__setitem__<\/code>\u65b9\u6cd5<\/h2>\n<\/p>\n<p><p><code>__getitem__<\/code>\u65b9\u6cd5\u7528\u4e8e\u5b9a\u4e49\u5bf9\u8c61\u7684\u7d22\u5f15\u8bbf\u95ee\u884c\u4e3a\uff0c\u4f7f\u5176\u652f\u6301\u4f7f\u7528\u7d22\u5f15\u8bbf\u95ee\u5143\u7d20\u3002<code>__setitem__<\/code>\u65b9\u6cd5\u7528\u4e8e\u5b9a\u4e49\u5bf9\u8c61\u7684\u7d22\u5f15\u8d4b\u503c\u884c\u4e3a\uff0c\u4f7f\u5176\u652f\u6301\u4f7f\u7528\u7d22\u5f15\u4fee\u6539\u5143\u7d20\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">class MyList:<\/p>\n<p>    def __init__(self, items):<\/p>\n<p>        self.items = items<\/p>\n<p>    def __getitem__(self, index):<\/p>\n<p>        return self.items[index]<\/p>\n<p>    def __setitem__(self, index, value):<\/p>\n<p>        self.items[index] = value<\/p>\n<p>    def __repr__(self):<\/p>\n<p>        return f&quot;MyList({self.items})&quot;<\/p>\n<h2><strong>\u521b\u5efa\u5bf9\u8c61<\/strong><\/h2>\n<p>my_list = MyList([1, 2, 3, 4])<\/p>\n<h2><strong>\u8c03\u7528\u9b54\u672f\u65b9\u6cd5<\/strong><\/h2>\n<p>print(my_list[2])  # \u8f93\u51fa\uff1a3<\/p>\n<p>my_list[2] = 10<\/p>\n<p>print(my_list)     # \u8f93\u51fa\uff1aMyList([1, 2, 10, 4])<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u5b9a\u4e49\u4e86<code>__getitem__<\/code>\u548c<code>__setitem__<\/code>\u65b9\u6cd5\uff0c\u4f7f\u5f97<code>MyList<\/code>\u5bf9\u8c61\u652f\u6301\u4f7f\u7528\u7d22\u5f15\u8bbf\u95ee\u548c\u4fee\u6539\u5143\u7d20\u3002<\/p>\n<\/p>\n<h2><strong>\u4e03\u3001\u5bf9\u8c61\u7684\u5c5e\u6027\u548c\u65b9\u6cd5\u88c5\u9970\u5668<\/strong><\/h2>\n<p><p>Python\u63d0\u4f9b\u4e86\u4e00\u4e9b\u88c5\u9970\u5668\uff0c\u53ef\u4ee5\u7528\u4e8e\u6539\u53d8\u5bf9\u8c61\u7684\u5c5e\u6027\u548c\u65b9\u6cd5\u7684\u884c\u4e3a\u3002\u8fd9\u4e9b\u88c5\u9970\u5668\u53ef\u4ee5\u8ba9\u4ee3\u7801\u66f4\u52a0\u7b80\u6d01\u548c\u5177\u6709\u8868\u73b0\u529b\u3002\u4e0b\u9762\u662f\u51e0\u4e2a\u5e38\u89c1\u7684\u88c5\u9970\u5668\uff1a<\/p>\n<\/p>\n<p><h2>1\u3001<code>@property<\/code>\u88c5\u9970\u5668<\/h2>\n<\/p>\n<p><p><code>@property<\/code>\u88c5\u9970\u5668\u7528\u4e8e\u5c06\u4e00\u4e2a\u65b9\u6cd5\u8f6c\u6362\u4e3a\u5c5e\u6027\uff0c\u4f7f\u5176\u53ef\u4ee5\u50cf\u8bbf\u95ee\u5c5e\u6027\u4e00\u6837\u8bbf\u95ee\u65b9\u6cd5\u7684\u8fd4\u56de\u503c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">class Circle:<\/p>\n<p>    def __init__(self, radius):<\/p>\n<p>        self._radius = radius<\/p>\n<p>    @property<\/p>\n<p>    def radius(self):<\/p>\n<p>        return self._radius<\/p>\n<p>    @radius.setter<\/p>\n<p>    def radius(self, value):<\/p>\n<p>        if value &lt; 0:<\/p>\n<p>            r<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>se ValueError(&quot;Radius cannot be negative&quot;)<\/p>\n<p>        self._radius = value<\/p>\n<p>    @property<\/p>\n<p>    def area(self):<\/p>\n<p>        return 3.14159 * self._radius  2<\/p>\n<h2><strong>\u521b\u5efa\u5bf9\u8c61<\/strong><\/h2>\n<p>circle = Circle(5)<\/p>\n<h2><strong>\u8c03\u7528\u88c5\u9970\u5668\u65b9\u6cd5<\/strong><\/h2>\n<p>print(circle.radius)  # \u8f93\u51fa\uff1a5<\/p>\n<p>print(circle.area)    # \u8f93\u51fa\uff1a78.53975<\/p>\n<p>circle.radius = 10<\/p>\n<p>print(circle.area)    # \u8f93\u51fa\uff1a314.159<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u5b9a\u4e49\u4e86\u4e00\u4e2a<code>Circle<\/code>\u7c7b\uff0c\u5e76\u4f7f\u7528<code>@property<\/code>\u88c5\u9970\u5668\u5c06<code>radius<\/code>\u548c<code>area<\/code>\u65b9\u6cd5\u8f6c\u6362\u4e3a\u5c5e\u6027\u3002\u8fd9\u6837\uff0c\u6211\u4eec\u53ef\u4ee5\u50cf\u8bbf\u95ee\u5c5e\u6027\u4e00\u6837\u8bbf\u95ee\u548c\u4fee\u6539<code>radius<\/code>\uff0c\u5e76\u8ba1\u7b97<code>area<\/code>\u3002<\/p>\n<\/p>\n<p><h2>2\u3001<code>@staticmethod<\/code>\u548c<code>@classmethod<\/code>\u88c5\u9970\u5668<\/h2>\n<\/p>\n<p><p><code>@staticmethod<\/code>\u88c5\u9970\u5668\u7528\u4e8e\u5b9a\u4e49\u9759\u6001\u65b9\u6cd5\uff0c\u9759\u6001\u65b9\u6cd5\u4e0d\u9700\u8981\u8bbf\u95ee\u5b9e\u4f8b\u6216\u7c7b\u7684\u5c5e\u6027\u548c\u65b9\u6cd5\u3002<code>@classmethod<\/code>\u88c5\u9970\u5668\u7528\u4e8e\u5b9a\u4e49\u7c7b\u65b9\u6cd5\uff0c\u7c7b\u65b9\u6cd5\u53ef\u4ee5\u8bbf\u95ee\u7c7b\u7684\u5c5e\u6027\u548c\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">class MathUtils:<\/p>\n<p>    @staticmethod<\/p>\n<p>    def add(a, b):<\/p>\n<p>        return a + b<\/p>\n<p>    @classmethod<\/p>\n<p>    def multiply(cls, a, b):<\/p>\n<p>        return a * b<\/p>\n<h2><strong>\u8c03\u7528\u9759\u6001\u65b9\u6cd5\u548c\u7c7b\u65b9\u6cd5<\/strong><\/h2>\n<p>print(MathUtils.add(3, 4))        # \u8f93\u51fa\uff1a7<\/p>\n<p>print(MathUtils.multiply(3, 4))   # \u8f93\u51fa\uff1a12<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u5b9a\u4e49\u4e86\u4e00\u4e2a<code>MathUtils<\/code>\u7c7b\uff0c\u5e76\u4f7f\u7528<code>@staticmethod<\/code>\u548c<code>@classmethod<\/code>\u88c5\u9970\u5668\u5b9a\u4e49\u4e86\u9759\u6001\u65b9\u6cd5<code>add<\/code>\u548c\u7c7b\u65b9\u6cd5<code>multiply<\/code>\u3002\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u7c7b\u540d\u76f4\u63a5\u8c03\u7528\u8fd9\u4e9b\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<h2><strong>\u516b\u3001\u5bf9\u8c61\u7684\u5e8f\u5217\u5316\u4e0e\u53cd\u5e8f\u5217\u5316<\/strong><\/h2>\n<p><p>\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0c\u6211\u4eec\u9700\u8981\u5c06\u5bf9\u8c61\u8f6c\u6362\u4e3a\u5b57\u7b26\u4e32\u6216\u4e8c\u8fdb\u5236\u6570\u636e\uff0c\u4ee5\u4fbf\u5b58\u50a8\u6216\u4f20\u8f93\u3002\u8fd9\u4e2a\u8fc7\u7a0b\u79f0\u4e3a\u5e8f\u5217\u5316\u3002\u76f8\u53cd\uff0c\u5c06\u5b57\u7b26\u4e32\u6216\u4e8c\u8fdb\u5236\u6570\u636e\u8f6c\u6362\u56de\u5bf9\u8c61\u7684\u8fc7\u7a0b\u79f0\u4e3a\u53cd\u5e8f\u5217\u5316\u3002Python\u63d0\u4f9b\u4e86\u591a\u79cd\u5e8f\u5217\u5316\u548c\u53cd\u5e8f\u5217\u5316\u7684\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><h2>1\u3001\u4f7f\u7528<code>pickle<\/code>\u6a21\u5757<\/h2>\n<\/p>\n<p><p><code>pickle<\/code>\u6a21\u5757\u63d0\u4f9b\u4e86\u5e8f\u5217\u5316\u548c\u53cd\u5e8f\u5217\u5316Python\u5bf9\u8c61\u7684\u529f\u80fd\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pickle<\/p>\n<p>class Person:<\/p>\n<p>    def __init__(self, name, age):<\/p>\n<p>        self.name = name<\/p>\n<p>        self.age = age<\/p>\n<h2><strong>\u521b\u5efa\u5bf9\u8c61<\/strong><\/h2>\n<p>person = Person(&quot;John&quot;, 30)<\/p>\n<h2><strong>\u5e8f\u5217\u5316\u5bf9\u8c61<\/strong><\/h2>\n<p>with open(&quot;person.pkl&quot;, &quot;wb&quot;) as f:<\/p>\n<p>    pickle.dump(person, f)<\/p>\n<h2><strong>\u53cd\u5e8f\u5217\u5316\u5bf9\u8c61<\/strong><\/h2>\n<p>with open(&quot;person.pkl&quot;, &quot;rb&quot;) as f:<\/p>\n<p>    loaded_person = pickle.load(f)<\/p>\n<p>print(loaded_person.name)  # \u8f93\u51fa\uff1aJohn<\/p>\n<p>print(loaded_person.age)   # \u8f93\u51fa\uff1a30<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528<code>pickle<\/code>\u6a21\u5757\u5c06\u4e00\u4e2a<code>Person<\/code>\u5bf9\u8c61\u5e8f\u5217\u5316\u4e3a\u4e8c\u8fdb\u5236\u6570\u636e\u5e76\u4fdd\u5b58\u5230\u6587\u4ef6\u4e2d\uff0c\u7136\u540e\u518d\u5c06\u5176\u53cd\u5e8f\u5217\u5316\u56de\u5bf9\u8c61\u3002<\/p>\n<\/p>\n<p><h2>2\u3001\u4f7f\u7528<code>json<\/code>\u6a21\u5757<\/h2>\n<\/p>\n<p><p><code>json<\/code>\u6a21\u5757\u63d0\u4f9b\u4e86\u5c06\u5bf9\u8c61\u8f6c\u6362\u4e3aJSON\u5b57\u7b26\u4e32\u548c\u4eceJSON\u5b57\u7b26\u4e32\u8f6c\u6362\u56de\u5bf9\u8c61\u7684\u529f\u80fd\u3002\u901a\u5e38\u7528\u4e8e\u7b80\u5355\u7684\u6570\u636e\u7ed3\u6784\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import json<\/p>\n<p>class Person:<\/p>\n<p>    def __init__(self, name, age):<\/p>\n<p>        self.name = name<\/p>\n<p>        self.age = age<\/p>\n<p>    def to_dict(self):<\/p>\n<p>        return {&quot;name&quot;: self.name, &quot;age&quot;: self.age}<\/p>\n<p>    @classmethod<\/p>\n<p>    def from_dict(cls, data):<\/p>\n<p>        return cls(data[&quot;name&quot;], data[&quot;age&quot;])<\/p>\n<h2><strong>\u521b\u5efa\u5bf9\u8c61<\/strong><\/h2>\n<p>person = Person(&quot;John&quot;, 30)<\/p>\n<h2><strong>\u5e8f\u5217\u5316\u5bf9\u8c61<\/strong><\/h2>\n<p>person_json = json.dumps(person.to_dict())<\/p>\n<p>print(person_json)  # \u8f93\u51fa\uff1a{&quot;name&quot;: &quot;John&quot;, &quot;age&quot;: 30}<\/p>\n<h2><strong>\u53cd\u5e8f\u5217\u5316\u5bf9\u8c61<\/strong><\/h2>\n<p>loaded_person = Person.from_dict(json.loads(person_json))<\/p>\n<p>print(loaded_person.name)  # \u8f93\u51fa\uff1aJohn<\/p>\n<p>print(loaded_person.age)   # \u8f93\u51fa\uff1a30<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528<code>json<\/code>\u6a21\u5757\u5c06\u4e00\u4e2a<code>Person<\/code>\u5bf9\u8c61\u8f6c\u6362\u4e3aJSON\u5b57\u7b26\u4e32\u5e76\u8fdb\u884c\u5e8f\u5217\u5316\uff0c\u7136\u540e\u518d\u5c06\u5176\u53cd\u5e8f\u5217\u5316\u56de\u5bf9\u8c61\u3002<\/p>\n<\/p>\n<h2><strong>\u4e5d\u3001\u5bf9\u8c61\u7684\u751f\u547d\u5468\u671f\u7ba1\u7406<\/strong><\/h2>\n<p><p>\u5bf9\u8c61\u7684\u751f\u547d\u5468\u671f\u6307\u7684\u662f\u5bf9\u8c61\u4ece\u521b\u5efa\u5230\u9500\u6bc1\u7684\u8fc7\u7a0b\u3002\u5728Python\u4e2d\uff0c\u5bf9\u8c61\u7684\u751f\u547d\u5468\u671f\u4e3b\u8981\u7531\u5783\u573e\u56de\u6536\u673a\u5236\u7ba1\u7406\u3002<\/p>\n<\/p>\n<p><h2>1\u3001\u5bf9\u8c61\u7684\u521b\u5efa\u548c\u9500\u6bc1<\/h2>\n<\/p>\n<p><p>\u5bf9\u8c61\u7684\u521b\u5efa\u901a\u8fc7\u8c03\u7528\u7c7b\u7684\u6784\u9020\u65b9\u6cd5\uff08\u901a\u5e38\u662f<code>__init__<\/code>\u65b9\u6cd5\uff09\u6765\u5b9e\u73b0\u3002\u5bf9\u8c61\u7684\u9500\u6bc1\u7531Python\u7684\u5783\u573e\u56de\u6536\u673a\u5236\u81ea\u52a8\u7ba1\u7406\uff0c\u5f53\u5bf9\u8c61\u4e0d\u518d\u88ab\u5f15\u7528\u65f6\uff0c\u5783\u573e\u56de\u6536\u5668\u4f1a\u81ea\u52a8\u91ca\u653e\u5176\u5360\u7528\u7684\u5185\u5b58\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">class MyClass:<\/p>\n<p>    def __init__(self):<\/p>\n<p>        print(&quot;Object created&quot;)<\/p>\n<p>    def __del__(self):<\/p>\n<p>        print(&quot;Object destroyed&quot;)<\/p>\n<h2><strong>\u521b\u5efa\u5bf9\u8c61<\/strong><\/h2>\n<p>obj = MyClass()<\/p>\n<h2><strong>\u5220\u9664\u5bf9\u8c61<\/strong><\/h2>\n<p>del obj<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u5b9a\u4e49\u4e86\u4e00\u4e2a<code>MyClass<\/code>\u7c7b\uff0c\u5e76\u5728\u5176\u4e2d\u5b9a\u4e49\u4e86<code>__init__<\/code>\u548c<code>__del__<\/code>\u65b9\u6cd5\u3002<code>__init__<\/code>\u65b9\u6cd5\u5728\u5bf9\u8c61\u521b\u5efa\u65f6\u8c03\u7528\uff0c<code>__del__<\/code>\u65b9\u6cd5\u5728\u5bf9\u8c61\u9500\u6bc1\u65f6\u8c03\u7528\u3002<\/p>\n<\/p>\n<p><h2>2\u3001\u5f15\u7528\u8ba1\u6570\u548c\u5783\u573e\u56de\u6536<\/h2>\n<\/p>\n<p><p>Python\u4f7f\u7528\u5f15\u7528\u8ba1\u6570\u6765\u7ba1\u7406\u5bf9\u8c61\u7684\u751f\u547d\u5468\u671f\u3002\u5f53\u5bf9\u8c61\u7684\u5f15\u7528\u8ba1\u6570\u964d\u4e3a\u96f6\u65f6\uff0c\u5783\u573e\u56de\u6536\u5668\u4f1a\u81ea\u52a8\u9500\u6bc1\u8be5\u5bf9\u8c61\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import sys<\/p>\n<p>class MyClass:<\/p>\n<p>    pass<\/p>\n<h2><strong>\u521b\u5efa\u5bf9\u8c61<\/strong><\/h2>\n<p>obj = MyClass()<\/p>\n<p>print(sys.getrefcount(obj))  # \u8f93\u51fa\uff1a2<\/p>\n<h2><strong>\u589e\u52a0\u5f15\u7528<\/strong><\/h2>\n<p>obj_ref = obj<\/p>\n<p>print(sys.getrefcount(obj))  # \u8f93\u51fa\uff1a3<\/p>\n<h2><strong>\u51cf\u5c11\u5f15\u7528<\/strong><\/h2>\n<p>del obj_ref<\/p>\n<p>print(sys.getrefcount(obj))  # \u8f93\u51fa\uff1a2<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528<code>sys.getrefcount<\/code>\u51fd\u6570\u6765\u83b7\u53d6\u5bf9\u8c61\u7684\u5f15\u7528\u8ba1\u6570\u3002\u6bcf\u5f53\u6211\u4eec\u589e\u52a0\u6216\u51cf\u5c11\u5bf9\u8c61\u7684\u5f15\u7528\u65f6\uff0c\u5f15\u7528\u8ba1\u6570\u4f1a\u76f8\u5e94\u53d8\u5316\u3002<\/p>\n<\/p>\n<h2><strong>\u5341\u3001\u5bf9\u8c61\u7684\u6df1\u62f7\u8d1d\u548c\u6d45\u62f7\u8d1d<\/strong><\/h2>\n<p><p>\u5728Python\u4e2d\uff0c\u62f7\u8d1d\u5bf9\u8c61\u65f6\u53ef\u4ee5\u9009\u62e9\u6d45\u62f7\u8d1d\u6216\u6df1\u62f7\u8d1d\u3002\u6d45\u62f7\u8d1d\u53ea\u590d\u5236\u5bf9\u8c61\u7684\u5f15\u7528\uff0c\u800c\u6df1\u62f7\u8d1d\u5219\u9012\u5f52\u590d\u5236\u5bf9\u8c61\u53ca\u5176\u5f15\u7528\u7684\u6240\u6709\u5bf9\u8c61\u3002<\/p>\n<\/p>\n<p><h2>1\u3001\u6d45\u62f7\u8d1d<\/h2>\n<\/p>\n<p><p>\u6d45\u62f7\u8d1d\u53ef\u4ee5\u4f7f\u7528<code>copy<\/code>\u6a21\u5757\u4e2d\u7684<code>copy<\/code>\u51fd\u6570\u6765\u5b9e\u73b0\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import copy<\/p>\n<p>class MyClass:<\/p>\n<p>    def __init__(self, value):<\/p>\n<p>        self.value = value<\/p>\n<h2><strong>\u521b\u5efa\u5bf9\u8c61<\/strong><\/h2>\n<p>obj1 = MyClass([1, 2, 3])<\/p>\n<h2><strong>\u6d45\u62f7\u8d1d\u5bf9\u8c61<\/strong><\/h2>\n<p>obj2 = copy.copy(obj1)<\/p>\n<h2><strong>\u4fee\u6539\u539f\u5bf9\u8c61\u7684\u5c5e\u6027<\/strong><\/h2>\n<p>obj1.value.append(4)<\/p>\n<p>print(obj1.value)  # \u8f93\u51fa\uff1a[1, 2, 3, 4]<\/p>\n<p>print(obj2.value)  # \u8f93\u51fa\uff1a[1, 2, 3, 4]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528<code>copy.copy<\/code>\u51fd\u6570\u5bf9<code>MyClass<\/code>\u5bf9\u8c61\u8fdb\u884c\u4e86\u6d45\u62f7\u8d1d\u3002\u4fee\u6539\u539f\u5bf9\u8c61\u7684\u5c5e\u6027\u4f1a\u5f71\u54cd\u6d45\u62f7\u8d1d\u5bf9\u8c61\u7684\u5c5e\u6027\uff0c\u56e0\u4e3a\u5b83\u4eec\u5171\u4eab\u76f8\u540c\u7684\u5f15\u7528\u3002<\/p>\n<\/p>\n<p><h2>2\u3001\u6df1\u62f7\u8d1d<\/h2>\n<\/p>\n<p><p>\u6df1\u62f7\u8d1d\u53ef\u4ee5\u4f7f\u7528<code>copy<\/code>\u6a21\u5757\u4e2d\u7684<code>deepcopy<\/code>\u51fd\u6570\u6765\u5b9e\u73b0\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import copy<\/p>\n<p>class MyClass:<\/p>\n<p>    def __init__(self, value):<\/p>\n<p>        self.value = value<\/p>\n<h2><strong>\u521b\u5efa\u5bf9\u8c61<\/strong><\/h2>\n<p>obj1 = MyClass([1, 2, 3])<\/p>\n<h2><strong>\u6df1\u62f7\u8d1d\u5bf9\u8c61<\/strong><\/h2>\n<p>obj2 = copy.deepcopy(obj1)<\/p>\n<h2><strong>\u4fee\u6539\u539f\u5bf9\u8c61\u7684\u5c5e\u6027<\/strong><\/h2>\n<p>obj1.value.append(4)<\/p>\n<p>print(obj1.value)  # \u8f93\u51fa\uff1a[1, 2, 3, 4]<\/p>\n<p>print(obj2.value)  # \u8f93\u51fa\uff1a[1, 2, 3]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528<code>copy.deepcopy<\/code>\u51fd\u6570\u5bf9<code>MyClass<\/code>\u5bf9\u8c61\u8fdb\u884c\u4e86\u6df1\u62f7\u8d1d\u3002\u4fee\u6539\u539f\u5bf9\u8c61\u7684\u5c5e\u6027\u4e0d\u4f1a\u5f71\u54cd\u6df1\u62f7\u8d1d\u5bf9\u8c61\u7684\u5c5e\u6027\uff0c\u56e0\u4e3a\u5b83\u4eec\u662f\u72ec\u7acb\u7684\u526f\u672c\u3002<\/p>\n<\/p>\n<h2><strong>\u5341\u4e00\u3001\u5bf9\u8c61<\/strong><\/h2>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u521b\u5efa\u4e00\u4e2a\u5bf9\u8c61\u5e76\u8c03\u7528\u5176\u65b9\u6cd5\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u521b\u5efa\u5bf9\u8c61\u7684\u8fc7\u7a0b\u901a\u5e38\u6d89\u53ca\u5b9a\u4e49\u4e00\u4e2a\u7c7b\uff0c\u5e76\u901a\u8fc7\u8be5\u7c7b\u5b9e\u4f8b\u5316\u5bf9\u8c61\u3002\u901a\u8fc7\u5b9e\u4f8b\u5316\u7684\u5bf9\u8c61\uff0c\u53ef\u4ee5\u8c03\u7528\u7c7b\u4e2d\u5b9a\u4e49\u7684\u65b9\u6cd5\u3002\u4f8b\u5982\uff0c\u5b9a\u4e49\u4e00\u4e2a\u7b80\u5355\u7684\u7c7b\u5982\u4e0b\uff1a<\/p>\n<pre><code class=\"language-python\">class Dog:\n    def bark(self):\n        return &quot;Woof!&quot;\n<\/code><\/pre>\n<p>\u7136\u540e\u53ef\u4ee5\u521b\u5efa\u4e00\u4e2aDog\u7c7b\u7684\u5bf9\u8c61\uff0c\u5e76\u8c03\u7528\u5176<code>bark<\/code>\u65b9\u6cd5\uff1a<\/p>\n<pre><code class=\"language-python\">my_dog = Dog()\nprint(my_dog.bark())  # \u8f93\u51fa: Woof!\n<\/code><\/pre>\n<p><strong>\u5728Python\u4e2d\u8c03\u7528\u5bf9\u8c61\u7684\u5c5e\u6027\u65f6\u5e94\u8be5\u6ce8\u610f\u4ec0\u4e48\uff1f<\/strong><br \/>\u8c03\u7528\u5bf9\u8c61\u7684\u5c5e\u6027\u65f6\uff0c\u786e\u4fdd\u5bf9\u8c61\u5df2\u88ab\u6b63\u786e\u5b9e\u4f8b\u5316\uff0c\u5e76\u4e14\u5c5e\u6027\u540d\u62fc\u5199\u65e0\u8bef\u3002\u5982\u679c\u5c5e\u6027\u662f\u79c1\u6709\u7684\uff08\u4ee5\u53cc\u4e0b\u5212\u7ebf\u5f00\u5934\uff09\uff0c\u5219\u4e0d\u80fd\u76f4\u63a5\u8bbf\u95ee\u3002\u53ef\u4ee5\u901a\u8fc7\u5b9a\u4e49\u516c\u5171\u65b9\u6cd5\u6216\u4f7f\u7528\u88c5\u9970\u5668\u6765\u8bbf\u95ee\u79c1\u6709\u5c5e\u6027\u3002\u793a\u4f8b\u5982\u4e0b\uff1a<\/p>\n<pre><code class=\"language-python\">class Cat:\n    def __init__(self, name):\n        self.__name = name  # \u79c1\u6709\u5c5e\u6027\n    \n    def get_name(self):\n        return self.__name  # \u901a\u8fc7\u516c\u5171\u65b9\u6cd5\u8bbf\u95ee\n    \nmy_cat = Cat(&quot;Whiskers&quot;)\nprint(my_cat.get_name())  # \u8f93\u51fa: Whiskers\n<\/code><\/pre>\n<p><strong>\u5bf9\u8c61\u8c03\u7528\u7684\u6027\u80fd\u5982\u4f55\u4f18\u5316\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u8c03\u7528\u5bf9\u8c61\u7684\u65b9\u6cd5\u6216\u5c5e\u6027\u901a\u5e38\u662f\u9ad8\u6548\u7684\uff0c\u4f46\u53ef\u4ee5\u901a\u8fc7\u4e00\u4e9b\u65b9\u5f0f\u6765\u4f18\u5316\u6027\u80fd\u3002\u4f8b\u5982\uff0c\u907f\u514d\u5728\u5faa\u73af\u4e2d\u91cd\u590d\u8bbf\u95ee\u5c5e\u6027\uff0c\u800c\u662f\u5c06\u5176\u5b58\u50a8\u5728\u5c40\u90e8\u53d8\u91cf\u4e2d\u3002\u6b64\u5916\uff0c\u4f7f\u7528\u5185\u7f6e\u51fd\u6570\u548c\u5e93\u6765\u5904\u7406\u590d\u6742\u7684\u6570\u636e\u64cd\u4f5c\u4e5f\u53ef\u4ee5\u63d0\u5347\u6027\u80fd\u3002\u8003\u8651\u4f7f\u7528<code>@property<\/code>\u88c5\u9970\u5668\u6765\u4f18\u5316\u5c5e\u6027\u8bbf\u95ee\uff0c\u8fd9\u6837\u53ef\u4ee5\u5b9e\u73b0\u61d2\u52a0\u8f7d\u548c\u7f13\u5b58\u3002<\/p>\n<pre><code class=\"language-python\">class Square:\n    def __init__(self, side_length):\n        self.side_length = side_length\n\n    @property\n    def area(self):\n        return self.side_length ** 2\n\nmy_square = Square(4)\nprint(my_square.area)  # \u8f93\u51fa: 16\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"Python\u4e2d\u8c03\u7528\u5bf9\u8c61\u7684\u65b9\u6cd5\u6709\uff1a\u8c03\u7528\u5bf9\u8c61\u7684\u5c5e\u6027\u3001\u8c03\u7528\u5bf9\u8c61\u7684\u65b9\u6cd5\u3001\u4f7f\u7528\u5bf9\u8c61\u4f5c\u4e3a\u53c2\u6570\u3001\u7ee7\u627f\u5bf9\u8c61\u3001\u7ec4\u5408\u5bf9\u8c61\u7b49\u3002\u8fd9\u4e9b\u65b9 [&hellip;]","protected":false},"author":3,"featured_media":1148212,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[37],"tags":[],"acf":[],"_links":{"self":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1148205"}],"collection":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/comments?post=1148205"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1148205\/revisions"}],"predecessor-version":[{"id":1148214,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1148205\/revisions\/1148214"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1148212"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1148205"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1148205"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1148205"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}