{"id":1012287,"date":"2024-12-27T11:37:33","date_gmt":"2024-12-27T03:37:33","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1012287.html"},"modified":"2024-12-27T11:37:37","modified_gmt":"2024-12-27T03:37:37","slug":"python%e5%a6%82%e4%bd%95%e5%88%9b%e5%bb%ba%e5%a4%9a%e4%b8%aa%e5%af%b9%e8%b1%a1","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1012287.html","title":{"rendered":"python\u5982\u4f55\u521b\u5efa\u591a\u4e2a\u5bf9\u8c61"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25090116\/8bd14451-e3e6-4746-96b2-302603d8480f.webp\" alt=\"python\u5982\u4f55\u521b\u5efa\u591a\u4e2a\u5bf9\u8c61\" \/><\/p>\n<p><p> <strong>\u5728Python\u4e2d\u521b\u5efa\u591a\u4e2a\u5bf9\u8c61\u7684\u65b9\u6cd5\u5305\u62ec\uff1a\u4f7f\u7528\u5faa\u73af\u521b\u5efa\u591a\u4e2a\u5bf9\u8c61\u3001\u4f7f\u7528\u5217\u8868\u6765\u5b58\u50a8\u591a\u4e2a\u5bf9\u8c61\u3001\u4ee5\u53ca\u5b9a\u4e49\u5de5\u5382\u51fd\u6570\u6765\u751f\u6210\u591a\u4e2a\u5bf9\u8c61\u3002<\/strong>\u5176\u4e2d\uff0c\u4f7f\u7528\u5faa\u73af\u521b\u5efa\u5bf9\u8c61\u662f\u4e00\u79cd\u5e38\u89c1\u4e14\u9ad8\u6548\u7684\u65b9\u6cd5\u3002\u5728\u4e00\u4e2a\u5faa\u73af\u4e2d\uff0c\u6211\u4eec\u53ef\u4ee5\u6839\u636e\u9700\u8981\u521b\u5efa\u4efb\u610f\u6570\u91cf\u7684\u5bf9\u8c61\uff0c\u5e76\u5c06\u8fd9\u4e9b\u5bf9\u8c61\u5b58\u50a8\u5728\u4e00\u4e2a\u6570\u636e\u7ed3\u6784\u4e2d\uff08\u5982\u5217\u8868\u6216\u5b57\u5178\uff09\u4ee5\u4fbf\u540e\u7eed\u4f7f\u7528\u3002\u4e0b\u9762\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u8fd9\u51e0\u79cd\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><p>\u4e00\u3001\u4f7f\u7528\u5faa\u73af\u521b\u5efa\u591a\u4e2a\u5bf9\u8c61<\/p>\n<\/p>\n<p><p>\u4f7f\u7528\u5faa\u73af\u521b\u5efa\u591a\u4e2a\u5bf9\u8c61\u662f\u4e00\u79cd\u7b80\u5355\u800c\u6709\u6548\u7684\u65b9\u6cd5\u3002\u901a\u8fc7\u5faa\u73af\uff0c\u6211\u4eec\u53ef\u4ee5\u8f7b\u677e\u521b\u5efa\u4efb\u610f\u6570\u91cf\u7684\u5bf9\u8c61\u5e76\u5c06\u5b83\u4eec\u5b58\u50a8\u5728\u4e00\u4e2a\u53ef\u8fed\u4ee3\u7684\u6570\u636e\u7ed3\u6784\u4e2d\uff0c\u5982\u5217\u8868\u6216\u5b57\u5178\u3002\u8fd9\u79cd\u65b9\u6cd5\u5728\u9700\u8981\u6279\u91cf\u521b\u5efa\u5bf9\u8c61\u65f6\u7279\u522b\u6709\u7528\u3002<\/p>\n<\/p>\n<ol>\n<li><strong>\u4f7f\u7528for\u5faa\u73af\u521b\u5efa\u5bf9\u8c61<\/strong><\/li>\n<\/ol>\n<p><p>\u5728Python\u4e2d\uff0cfor\u5faa\u73af\u662f\u4e00\u79cd\u5f3a\u5927\u7684\u5de5\u5177\uff0c\u53ef\u4ee5\u5728\u521b\u5efa\u5bf9\u8c61\u65f6\u4f7f\u7528\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u793a\u4f8b\uff0c\u5c55\u793a\u5982\u4f55\u4f7f\u7528for\u5faa\u73af\u521b\u5efa\u591a\u4e2a\u5bf9\u8c61\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<h2><strong>\u521b\u5efa\u4e00\u4e2a\u7a7a\u5217\u8868\u6765\u5b58\u50a8\u72d7\u5bf9\u8c61<\/strong><\/h2>\n<p>dogs = []<\/p>\n<h2><strong>\u4f7f\u7528for\u5faa\u73af\u521b\u5efa\u591a\u4e2a\u72d7\u5bf9\u8c61<\/strong><\/h2>\n<p>for i in range(5):<\/p>\n<p>    dog = Dog(f&quot;Dog{i}&quot;)<\/p>\n<p>    dogs.append(dog)<\/p>\n<h2><strong>\u6253\u5370\u6bcf\u4e2a\u72d7\u5bf9\u8c61\u7684\u540d\u5b57<\/strong><\/h2>\n<p>for dog in dogs:<\/p>\n<p>    print(dog.name)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u5b9a\u4e49\u4e86\u4e00\u4e2a<code>Dog<\/code>\u7c7b\uff0c\u5e76\u4f7f\u7528for\u5faa\u73af\u521b\u5efa5\u4e2a<code>Dog<\/code>\u5bf9\u8c61\u3002\u6bcf\u4e2a\u5bf9\u8c61\u7684\u540d\u5b57\u90fd\u662f\u552f\u4e00\u7684\uff0c\u5e76\u88ab\u5b58\u50a8\u5728<code>dogs<\/code>\u5217\u8868\u4e2d\u3002<\/p>\n<\/p>\n<ol start=\"2\">\n<li><strong>\u4f7f\u7528while\u5faa\u73af\u521b\u5efa\u5bf9\u8c61<\/strong><\/li>\n<\/ol>\n<p><p>\u9664\u4e86for\u5faa\u73af\uff0c\u6211\u4eec\u4e5f\u53ef\u4ee5\u4f7f\u7528while\u5faa\u73af\u6765\u521b\u5efa\u591a\u4e2a\u5bf9\u8c61\u3002while\u5faa\u73af\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\u66f4\u4e3a\u7075\u6d3b\uff0c\u56e0\u4e3a\u5b83\u5141\u8bb8\u6211\u4eec\u6839\u636e\u7279\u5b9a\u6761\u4ef6\u6765\u63a7\u5236\u5faa\u73af\u7684\u6267\u884c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">class Cat:<\/p>\n<p>    def __init__(self, name):<\/p>\n<p>        self.name = name<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2a\u7a7a\u5217\u8868\u6765\u5b58\u50a8\u732b\u5bf9\u8c61<\/strong><\/h2>\n<p>cats = []<\/p>\n<h2><strong>\u4f7f\u7528while\u5faa\u73af\u521b\u5efa\u591a\u4e2a\u732b\u5bf9\u8c61<\/strong><\/h2>\n<p>i = 0<\/p>\n<p>while i &lt; 5:<\/p>\n<p>    cat = Cat(f&quot;Cat{i}&quot;)<\/p>\n<p>    cats.append(cat)<\/p>\n<p>    i += 1<\/p>\n<h2><strong>\u6253\u5370\u6bcf\u4e2a\u732b\u5bf9\u8c61\u7684\u540d\u5b57<\/strong><\/h2>\n<p>for cat in cats:<\/p>\n<p>    print(cat.name)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0cwhile\u5faa\u73af\u7684\u903b\u8f91\u7c7b\u4f3c\u4e8efor\u5faa\u73af\uff0c\u6211\u4eec\u521b\u5efa5\u4e2a<code>Cat<\/code>\u5bf9\u8c61\uff0c\u5e76\u5c06\u5b83\u4eec\u5b58\u50a8\u5728\u4e00\u4e2a\u5217\u8868\u4e2d\u3002<\/p>\n<\/p>\n<p><p>\u4e8c\u3001\u4f7f\u7528\u5217\u8868\u6216\u5b57\u5178\u5b58\u50a8\u5bf9\u8c61<\/p>\n<\/p>\n<p><p>\u5728\u521b\u5efa\u591a\u4e2a\u5bf9\u8c61\u540e\uff0c\u6211\u4eec\u901a\u5e38\u9700\u8981\u4e00\u79cd\u65b9\u6cd5\u6765\u5b58\u50a8\u548c\u7ba1\u7406\u8fd9\u4e9b\u5bf9\u8c61\u3002\u5217\u8868\u548c\u5b57\u5178\u662fPython\u4e2d\u6700\u5e38\u7528\u7684\u6570\u636e\u7ed3\u6784\uff0c\u53ef\u4ee5\u6709\u6548\u5730\u5b58\u50a8\u548c\u7ba1\u7406\u591a\u4e2a\u5bf9\u8c61\u3002<\/p>\n<\/p>\n<ol>\n<li><strong>\u4f7f\u7528\u5217\u8868\u5b58\u50a8\u5bf9\u8c61<\/strong><\/li>\n<\/ol>\n<p><p>\u5217\u8868\u662f\u4e00\u4e2a\u6709\u5e8f\u7684\u96c6\u5408\uff0c\u9002\u5408\u5b58\u50a8\u4e00\u7ec4\u5bf9\u8c61\u3002\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u7d22\u5f15\u8bbf\u95ee\u5217\u8868\u4e2d\u7684\u6bcf\u4e2a\u5bf9\u8c61\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">class Bird:<\/p>\n<p>    def __init__(self, name):<\/p>\n<p>        self.name = name<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2a\u7a7a\u5217\u8868\u6765\u5b58\u50a8\u9e1f\u5bf9\u8c61<\/strong><\/h2>\n<p>birds = []<\/p>\n<h2><strong>\u4f7f\u7528for\u5faa\u73af\u521b\u5efa\u591a\u4e2a\u9e1f\u5bf9\u8c61<\/strong><\/h2>\n<p>for i in range(5):<\/p>\n<p>    bird = Bird(f&quot;Bird{i}&quot;)<\/p>\n<p>    birds.append(bird)<\/p>\n<h2><strong>\u6253\u5370\u6bcf\u4e2a\u9e1f\u5bf9\u8c61\u7684\u540d\u5b57<\/strong><\/h2>\n<p>for bird in birds:<\/p>\n<p>    print(bird.name)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528\u5217\u8868<code>birds<\/code>\u5b58\u50a8\u591a\u4e2a<code>Bird<\/code>\u5bf9\u8c61\u3002\u5217\u8868\u63d0\u4f9b\u4e86\u7075\u6d3b\u7684\u64cd\u4f5c\u65b9\u6cd5\uff0c\u5982\u6dfb\u52a0\u3001\u5220\u9664\u548c\u8fed\u4ee3\u3002<\/p>\n<\/p>\n<ol start=\"2\">\n<li><strong>\u4f7f\u7528\u5b57\u5178\u5b58\u50a8\u5bf9\u8c61<\/strong><\/li>\n<\/ol>\n<p><p>\u5b57\u5178\u662f\u4e00\u4e2a\u65e0\u5e8f\u7684\u96c6\u5408\uff0c\u9002\u5408\u4f7f\u7528\u952e\u503c\u5bf9\u6765\u5b58\u50a8\u5bf9\u8c61\u3002\u5b57\u5178\u5141\u8bb8\u6211\u4eec\u901a\u8fc7\u952e\u6765\u5feb\u901f\u8bbf\u95ee\u5bf9\u8c61\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">class Fish:<\/p>\n<p>    def __init__(self, name):<\/p>\n<p>        self.name = name<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2a\u7a7a\u5b57\u5178\u6765\u5b58\u50a8\u9c7c\u5bf9\u8c61<\/strong><\/h2>\n<p>fishes = {}<\/p>\n<h2><strong>\u4f7f\u7528for\u5faa\u73af\u521b\u5efa\u591a\u4e2a\u9c7c\u5bf9\u8c61\uff0c\u5e76\u4f7f\u7528\u5b57\u5178\u5b58\u50a8<\/strong><\/h2>\n<p>for i in range(5):<\/p>\n<p>    fish = Fish(f&quot;Fish{i}&quot;)<\/p>\n<p>    fishes[f&quot;Fish{i}&quot;] = fish<\/p>\n<h2><strong>\u6253\u5370\u6bcf\u4e2a\u9c7c\u5bf9\u8c61\u7684\u540d\u5b57<\/strong><\/h2>\n<p>for key, fish in fishes.items():<\/p>\n<p>    print(f&quot;{key}: {fish.name}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528\u5b57\u5178<code>fishes<\/code>\u5b58\u50a8\u591a\u4e2a<code>Fish<\/code>\u5bf9\u8c61\u3002\u5b57\u5178\u7684\u952e\u4e3a\u5bf9\u8c61\u7684\u540d\u5b57\uff0c\u503c\u4e3a\u5bf9\u8c61\u5b9e\u4f8b\u3002\u8fd9\u79cd\u65b9\u6cd5\u5728\u9700\u8981\u5feb\u901f\u67e5\u627e\u548c\u8bbf\u95ee\u5bf9\u8c61\u65f6\u975e\u5e38\u6709\u7528\u3002<\/p>\n<\/p>\n<p><p>\u4e09\u3001\u5b9a\u4e49\u5de5\u5382\u51fd\u6570\u6765\u751f\u6210\u5bf9\u8c61<\/p>\n<\/p>\n<p><p>\u5de5\u5382\u51fd\u6570\u662f\u4e00\u79cd\u8bbe\u8ba1\u6a21\u5f0f\uff0c\u53ef\u4ee5\u7528\u4e8e\u6279\u91cf\u751f\u6210\u5bf9\u8c61\u3002\u901a\u8fc7\u5b9a\u4e49\u4e00\u4e2a\u5de5\u5382\u51fd\u6570\uff0c\u6211\u4eec\u53ef\u4ee5\u5c06\u5bf9\u8c61\u7684\u521b\u5efa\u903b\u8f91\u5c01\u88c5\u5728\u4e00\u4e2a\u51fd\u6570\u4e2d\uff0c\u5e76\u6839\u636e\u9700\u8981\u751f\u6210\u591a\u4e2a\u5bf9\u8c61\u3002<\/p>\n<\/p>\n<ol>\n<li><strong>\u5b9a\u4e49\u7b80\u5355\u7684\u5de5\u5382\u51fd\u6570<\/strong><\/li>\n<\/ol>\n<p><p>\u4e00\u4e2a\u7b80\u5355\u7684\u5de5\u5382\u51fd\u6570\u53ef\u4ee5\u63a5\u53d7\u53c2\u6570\uff0c\u5e76\u8fd4\u56de\u4e00\u4e2a\u6216\u591a\u4e2a\u5bf9\u8c61\u3002\u8fd9\u79cd\u65b9\u6cd5\u5728\u9700\u8981\u6839\u636e\u4e0d\u540c\u6761\u4ef6\u521b\u5efa\u5bf9\u8c61\u65f6\u975e\u5e38\u6709\u7528\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">class Car:<\/p>\n<p>    def __init__(self, model):<\/p>\n<p>        self.model = model<\/p>\n<h2><strong>\u5b9a\u4e49\u5de5\u5382\u51fd\u6570\u6765\u521b\u5efa\u591a\u4e2aCar\u5bf9\u8c61<\/strong><\/h2>\n<p>def car_factory(models):<\/p>\n<p>    return [Car(model) for model in models]<\/p>\n<h2><strong>\u4f7f\u7528\u5de5\u5382\u51fd\u6570\u521b\u5efa\u591a\u4e2aCar\u5bf9\u8c61<\/strong><\/h2>\n<p>car_models = [&quot;Toyota&quot;, &quot;Ford&quot;, &quot;BMW&quot;, &quot;Audi&quot;, &quot;Tesla&quot;]<\/p>\n<p>cars = car_factory(car_models)<\/p>\n<h2><strong>\u6253\u5370\u6bcf\u4e2aCar\u5bf9\u8c61\u7684\u578b\u53f7<\/strong><\/h2>\n<p>for car in cars:<\/p>\n<p>    print(car.model)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c<code>car_factory<\/code>\u51fd\u6570\u63a5\u53d7\u4e00\u4e2a\u8f66\u578b\u5217\u8868\uff0c\u5e76\u8fd4\u56de\u4e00\u4e2a\u5305\u542b\u591a\u4e2a<code>Car<\/code>\u5bf9\u8c61\u7684\u5217\u8868\u3002\u5de5\u5382\u51fd\u6570\u4f7f\u5f97\u5bf9\u8c61\u7684\u521b\u5efa\u903b\u8f91\u66f4\u52a0\u6e05\u6670\u548c\u53ef\u7ef4\u62a4\u3002<\/p>\n<\/p>\n<ol start=\"2\">\n<li><strong>\u9ad8\u7ea7\u5de5\u5382\u51fd\u6570<\/strong><\/li>\n<\/ol>\n<p><p>\u9ad8\u7ea7\u5de5\u5382\u51fd\u6570\u53ef\u4ee5\u5305\u542b\u66f4\u591a\u7684\u903b\u8f91\u548c\u53c2\u6570\uff0c\u4ee5\u6ee1\u8db3\u66f4\u590d\u6742\u7684\u9700\u6c42\u3002\u4f8b\u5982\uff0c\u6211\u4eec\u53ef\u4ee5\u5b9a\u4e49\u4e00\u4e2a\u5de5\u5382\u51fd\u6570\uff0c\u6839\u636e\u4e0d\u540c\u7684\u6761\u4ef6\u521b\u5efa\u4e0d\u540c\u7c7b\u578b\u7684\u5bf9\u8c61\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">class Animal:<\/p>\n<p>    def __init__(self, species, name):<\/p>\n<p>        self.species = species<\/p>\n<p>        self.name = name<\/p>\n<h2><strong>\u5b9a\u4e49\u9ad8\u7ea7\u5de5\u5382\u51fd\u6570\u6765\u521b\u5efa\u4e0d\u540c\u79cd\u7c7b\u7684Animal\u5bf9\u8c61<\/strong><\/h2>\n<p>def animal_factory(animals_info):<\/p>\n<p>    return [Animal(species, name) for species, name in animals_info]<\/p>\n<h2><strong>\u4f7f\u7528\u5de5\u5382\u51fd\u6570\u521b\u5efa\u591a\u4e2aAnimal\u5bf9\u8c61<\/strong><\/h2>\n<p>animals_info = [(&quot;Dog&quot;, &quot;Rover&quot;), (&quot;Cat&quot;, &quot;Whiskers&quot;), (&quot;Bird&quot;, &quot;Tweety&quot;), (&quot;Fish&quot;, &quot;Nemo&quot;), (&quot;Horse&quot;, &quot;Spirit&quot;)]<\/p>\n<p>animals = animal_factory(animals_info)<\/p>\n<h2><strong>\u6253\u5370\u6bcf\u4e2aAnimal\u5bf9\u8c61\u7684\u79cd\u7c7b\u548c\u540d\u5b57<\/strong><\/h2>\n<p>for animal in animals:<\/p>\n<p>    print(f&quot;{animal.species}: {animal.name}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c<code>animal_factory<\/code>\u51fd\u6570\u63a5\u53d7\u4e00\u4e2a\u5305\u542b\u52a8\u7269\u79cd\u7c7b\u548c\u540d\u5b57\u7684\u5143\u7ec4\u5217\u8868\uff0c\u5e76\u8fd4\u56de\u4e00\u4e2a\u5305\u542b\u591a\u4e2a<code>Animal<\/code>\u5bf9\u8c61\u7684\u5217\u8868\u3002\u901a\u8fc7\u5de5\u5382\u51fd\u6570\uff0c\u6211\u4eec\u53ef\u4ee5\u8f7b\u677e\u751f\u6210\u5404\u79cd\u7c7b\u578b\u7684\u5bf9\u8c61\u3002<\/p>\n<\/p>\n<p><p>\u56db\u3001\u4f7f\u7528\u751f\u6210\u5668\u521b\u5efa\u5bf9\u8c61<\/p>\n<\/p>\n<p><p>\u751f\u6210\u5668\u662f\u4e00\u79cd\u7279\u6b8a\u7684\u8fed\u4ee3\u5668\uff0c\u53ef\u4ee5\u7528\u4e8e\u60f0\u6027\u521b\u5efa\u5bf9\u8c61\u3002\u751f\u6210\u5668\u5728\u9700\u8981\u9010\u4e2a\u751f\u6210\u5bf9\u8c61\u65f6\u7279\u522b\u6709\u7528\uff0c\u56e0\u4e3a\u5b83\u4eec\u53ef\u4ee5\u8282\u7701\u5185\u5b58\u5e76\u63d0\u9ad8\u6027\u80fd\u3002<\/p>\n<\/p>\n<ol>\n<li><strong>\u4f7f\u7528\u751f\u6210\u5668\u51fd\u6570\u521b\u5efa\u5bf9\u8c61<\/strong><\/li>\n<\/ol>\n<p><p>\u751f\u6210\u5668\u51fd\u6570\u4f7f\u7528<code>yield<\/code>\u8bed\u53e5\u6765\u9010\u4e2a\u751f\u6210\u5bf9\u8c61\u3002\u751f\u6210\u5668\u5728\u9700\u8981\u65f6\u52a8\u6001\u751f\u6210\u5bf9\u8c61\uff0c\u5e76\u5728\u5b8c\u6210\u540e\u91ca\u653e\u8d44\u6e90\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">class Tree:<\/p>\n<p>    def __init__(self, type):<\/p>\n<p>        self.type = type<\/p>\n<h2><strong>\u5b9a\u4e49\u751f\u6210\u5668\u51fd\u6570\u6765\u521b\u5efa\u591a\u4e2aTree\u5bf9\u8c61<\/strong><\/h2>\n<p>def tree_generator(types):<\/p>\n<p>    for type in types:<\/p>\n<p>        yield Tree(type)<\/p>\n<h2><strong>\u4f7f\u7528\u751f\u6210\u5668\u51fd\u6570\u521b\u5efa\u591a\u4e2aTree\u5bf9\u8c61<\/strong><\/h2>\n<p>tree_types = [&quot;Oak&quot;, &quot;Pine&quot;, &quot;Maple&quot;, &quot;Birch&quot;, &quot;Willow&quot;]<\/p>\n<p>trees = tree_generator(tree_types)<\/p>\n<h2><strong>\u6253\u5370\u6bcf\u4e2aTree\u5bf9\u8c61\u7684\u7c7b\u578b<\/strong><\/h2>\n<p>for tree in trees:<\/p>\n<p>    print(tree.type)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c<code>tree_generator<\/code>\u51fd\u6570\u4f7f\u7528<code>yield<\/code>\u8bed\u53e5\u9010\u4e2a\u751f\u6210<code>Tree<\/code>\u5bf9\u8c61\u3002\u751f\u6210\u5668\u5728\u9700\u8981\u65f6\u751f\u6210\u5bf9\u8c61\uff0c\u5e76\u5728\u8fed\u4ee3\u5b8c\u6210\u540e\u81ea\u52a8\u91ca\u653e\u5185\u5b58\u3002<\/p>\n<\/p>\n<ol start=\"2\">\n<li><strong>\u4f7f\u7528\u751f\u6210\u5668\u8868\u8fbe\u5f0f\u521b\u5efa\u5bf9\u8c61<\/strong><\/li>\n<\/ol>\n<p><p>\u751f\u6210\u5668\u8868\u8fbe\u5f0f\u662f\u4e00\u79cd\u7b80\u6d01\u7684\u8bed\u6cd5\uff0c\u7528\u4e8e\u521b\u5efa\u751f\u6210\u5668\u3002\u751f\u6210\u5668\u8868\u8fbe\u5f0f\u7c7b\u4f3c\u4e8e\u5217\u8868\u63a8\u5bfc\u5f0f\uff0c\u4f46\u5b83\u8fd4\u56de\u7684\u662f\u4e00\u4e2a\u751f\u6210\u5668\u5bf9\u8c61\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">class Flower:<\/p>\n<p>    def __init__(self, name):<\/p>\n<p>        self.name = name<\/p>\n<h2><strong>\u4f7f\u7528\u751f\u6210\u5668\u8868\u8fbe\u5f0f\u521b\u5efa\u591a\u4e2aFlower\u5bf9\u8c61<\/strong><\/h2>\n<p>flower_names = [&quot;Rose&quot;, &quot;Lily&quot;, &quot;Tulip&quot;, &quot;D<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>sy&quot;, &quot;Sunflower&quot;]<\/p>\n<p>flowers = (Flower(name) for name in flower_names)<\/p>\n<h2><strong>\u6253\u5370\u6bcf\u4e2aFlower\u5bf9\u8c61\u7684\u540d\u5b57<\/strong><\/h2>\n<p>for flower in flowers:<\/p>\n<p>    print(flower.name)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528\u751f\u6210\u5668\u8868\u8fbe\u5f0f\u521b\u5efa<code>Flower<\/code>\u5bf9\u8c61\u3002\u8fd9\u79cd\u65b9\u6cd5\u5728\u9700\u8981\u7b80\u5355\u800c\u9ad8\u6548\u5730\u751f\u6210\u5bf9\u8c61\u65f6\u975e\u5e38\u6709\u7528\u3002<\/p>\n<\/p>\n<p><p>\u901a\u8fc7\u4e0a\u8ff0\u65b9\u6cd5\uff0c\u6211\u4eec\u53ef\u4ee5\u5728Python\u4e2d\u7075\u6d3b\u5730\u521b\u5efa\u548c\u7ba1\u7406\u591a\u4e2a\u5bf9\u8c61\u3002\u6839\u636e\u5177\u4f53\u9700\u6c42\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\uff0c\u53ef\u4ee5\u5e2e\u52a9\u6211\u4eec\u7f16\u5199\u66f4\u9ad8\u6548\u548c\u53ef\u7ef4\u62a4\u7684\u4ee3\u7801\u3002\u65e0\u8bba\u662f\u4f7f\u7528\u5faa\u73af\u3001\u5217\u8868\u3001\u5b57\u5178\uff0c\u8fd8\u662f\u5de5\u5382\u51fd\u6570\u548c\u751f\u6210\u5668\uff0c\u6bcf\u79cd\u65b9\u6cd5\u90fd\u6709\u5176\u72ec\u7279\u7684\u4f18\u52bf\u548c\u9002\u7528\u573a\u666f\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u521b\u5efa\u591a\u4e2a\u5bf9\u8c61\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7\u5b9a\u4e49\u7c7b\u5e76\u4f7f\u7528\u5faa\u73af\u6216\u5217\u8868\u63a8\u5bfc\u5f0f\u6765\u521b\u5efa\u591a\u4e2a\u5bf9\u8c61\u3002\u5b9a\u4e49\u4e00\u4e2a\u7c7b\u540e\uff0c\u53ef\u4ee5\u5728\u5faa\u73af\u4e2d\u5b9e\u4f8b\u5316\u8be5\u7c7b\uff0c\u4ece\u800c\u751f\u6210\u591a\u4e2a\u5bf9\u8c61\u3002\u4f8b\u5982\uff0c\u5047\u8bbe\u6709\u4e00\u4e2a\u540d\u4e3a<code>Car<\/code>\u7684\u7c7b\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u4ee3\u7801\u521b\u5efa\u591a\u4e2a<code>Car<\/code>\u5bf9\u8c61\uff1a<\/p>\n<pre><code class=\"language-python\">class Car:\n    def __init__(self, make, model):\n        self.make = make\n        self.model = model\n\n# \u521b\u5efa\u591a\u4e2aCar\u5bf9\u8c61\ncars = [Car(&#39;Toyota&#39;, &#39;Camry&#39;), Car(&#39;Honda&#39;, &#39;Civic&#39;), Car(&#39;Ford&#39;, &#39;Mustang&#39;)]\n<\/code><\/pre>\n<p><strong>\u4f7f\u7528\u5217\u8868\u63a8\u5bfc\u5f0f\u521b\u5efa\u5bf9\u8c61\u7684\u4f18\u52bf\u662f\u4ec0\u4e48\uff1f<\/strong><br \/>\u5217\u8868\u63a8\u5bfc\u5f0f\u662f\u4e00\u79cd\u7b80\u6d01\u7684\u65b9\u5f0f\uff0c\u53ef\u4ee5\u5728\u4e00\u884c\u4ee3\u7801\u4e2d\u521b\u5efa\u591a\u4e2a\u5bf9\u8c61\u3002\u8fd9\u79cd\u65b9\u6cd5\u4e0d\u4ec5\u63d0\u9ad8\u4e86\u4ee3\u7801\u7684\u53ef\u8bfb\u6027\uff0c\u8fd8\u51cf\u5c11\u4e86\u4ee3\u7801\u7684\u884c\u6570\u3002\u4f8b\u5982\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528\u5217\u8868\u63a8\u5bfc\u5f0f\u6765\u751f\u6210\u591a\u4e2a<code>Car<\/code>\u5bf9\u8c61\uff0c\u65b9\u4fbf\u7ba1\u7406\u548c\u7ef4\u62a4\u3002<\/p>\n<pre><code class=\"language-python\">makes = [&#39;Toyota&#39;, &#39;Honda&#39;, &#39;Ford&#39;]\nmodels = [&#39;Camry&#39;, &#39;Civic&#39;, &#39;Mustang&#39;]\ncars = [Car(make, model) for make, model in zip(makes, models)]\n<\/code><\/pre>\n<p><strong>\u5728\u521b\u5efa\u591a\u4e2a\u5bf9\u8c61\u65f6\uff0c\u5982\u4f55\u7ba1\u7406\u5b83\u4eec\u7684\u5c5e\u6027\uff1f<\/strong><br \/>\u53ef\u4ee5\u901a\u8fc7\u4f7f\u7528\u6570\u636e\u7ed3\u6784\uff08\u5982\u5b57\u5178\u6216\u5217\u8868\uff09\u6765\u7ba1\u7406\u591a\u4e2a\u5bf9\u8c61\u7684\u5c5e\u6027\u3002\u5c06\u6bcf\u4e2a\u5bf9\u8c61\u7684\u5c5e\u6027\u5b58\u50a8\u5728\u7ed3\u6784\u4e2d\uff0c\u4fbf\u4e8e\u540e\u7eed\u8bbf\u95ee\u548c\u4fee\u6539\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u521b\u5efa\u4e00\u4e2a\u5b57\u5178\uff0c\u5176\u4e2d\u952e\u662f\u5bf9\u8c61\u7684\u6807\u8bc6\u7b26\uff0c\u503c\u662f\u5bf9\u8c61\u672c\u8eab\uff1a<\/p>\n<pre><code class=\"language-python\">car_data = {\n    &#39;car1&#39;: Car(&#39;Toyota&#39;, &#39;Camry&#39;),\n    &#39;car2&#39;: Car(&#39;Honda&#39;, &#39;Civic&#39;),\n    &#39;car3&#39;: Car(&#39;Ford&#39;, &#39;Mustang&#39;)\n}\n<\/code><\/pre>\n<p>\u8fd9\u6837\uff0c\u60a8\u53ef\u4ee5\u901a\u8fc7\u952e\u540d\u8f7b\u677e\u8bbf\u95ee\u548c\u64cd\u4f5c\u5404\u4e2a\u5bf9\u8c61\u7684\u5c5e\u6027\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\u521b\u5efa\u591a\u4e2a\u5bf9\u8c61\u7684\u65b9\u6cd5\u5305\u62ec\uff1a\u4f7f\u7528\u5faa\u73af\u521b\u5efa\u591a\u4e2a\u5bf9\u8c61\u3001\u4f7f\u7528\u5217\u8868\u6765\u5b58\u50a8\u591a\u4e2a\u5bf9\u8c61\u3001\u4ee5\u53ca\u5b9a\u4e49\u5de5\u5382\u51fd\u6570\u6765\u751f\u6210\u591a [&hellip;]","protected":false},"author":3,"featured_media":1012298,"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\/1012287"}],"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=1012287"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1012287\/revisions"}],"predecessor-version":[{"id":1012301,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1012287\/revisions\/1012301"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1012298"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1012287"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1012287"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1012287"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}