为什么我不能从第二次创建对象中获得价值? -Python

时间:2018-11-04 18:21:20

标签: python python-3.x oop

我正在网上进行以下练习:

http://gsl.mit.edu/media/programs/sri-lanka-summer-2012/materials/t_hw1.pdf

它将使我更加了解要完成的工作。

当我在search_contact上的lastname上调用<-(Simpson)函数时,我确实得到了第一个对象:

Homer, Simpson, -- Phone Number: 5559355899, --Email: homer@mail.com 

但是我也想获取第二个对象,所以输出如下:

Homer, Simpson, -- Phone Number: 5559355899, --Email: homer@mail.com
Marge, Simpson, -- Phone Number: 5559352365, --Email: marge@mail.com

仍然要练习掌握面向对象的编程范例。 让我知道是否不清楚。 亲爱的,感谢您的帮助。

这是我到目前为止所拥有的:

class Person:
   def __init__(self, firstname, lastname, phone_number, email):
      self.firstname= firstname
      self.lastname= lastname
      self.phone_number = phone_number
      self.email = email

   def __str__(self):
      template = '{}, {}, -- Phone Number: {}, --Email: {}'.format(self.firstname, self.lastname, self.phone_number, self.email)
      template = template.replace('[','').replace(']','').replace("'", '')
      return template.format(self)

class AddressBook(Person):
   def __init__(self):
      self.book = {}

   def add_contact(self, p):
      self.book[p] = p
      return self.book

   def search_contact(self, lastname):
      for p in self.book:
         if p.lastname == lastname:
             template = '{}, {}, -- Phone Number: {}, --Email: {}'.format(p.firstname, p.lastname, p.phone_number, p.email)
             return template

if __name__ == '__main__':
   #     Bob = Person('Bob', 'Lop', '5559358150', 'bob@mail.com)
   #     print(Bob)
   #     Joe = Person('Joe', 'Roe', '5551940325',['joe@mail.com', 'roe@mail.com'])
   #     print(Joe)

   a = AddressBook()
   added = a.add_contact(Person('Homer', 'Simpson', '5559355899', 'homer@mail.com'))
 # print(added)
   added_1 = a.add_contact(Person('Marge', 'Simpson', '5559352365', 'marge@mail.com'))
 # print(added_1)
   search = a.search_contact('Simpson')
   print(search)

1 个答案:

答案 0 :(得分:1)

您必须修复Person,__init__()中的错误-参数顺序不正确(或提供创建Person(...)时翻转的名称)。

   def search_contact(self, lastname):
      for p in self.book:
         if p.lastname == lastname:
             template = '{}, {}, -- Phone Number: {}, --Email: {}'.format(p.lastname, p.firstname, p.phone_number, p.email)
             return template

return s。

如果您通过某个功能return完成了操作。您可以yield(使其成为生成器),也可以在listreturn中收集所有匹配项。无论哪种方式,您都需要调整您的打印代码,因为现在您将获得一个列表/生成器。


列表示例(生成器更为复杂):

class Person:
    def __init__(self,  firstname, lastname, phone_number, email): # FIX
        self.lastname = lastname
        self.firstname = firstname
        self.phone_number = phone_number
        self.email = email

    def __str__(self):
        template = '{}, {}, -- Phone Number: {}, --Email: {}'.format(
            self.lastname, self.firstname, self.phone_number, self.email)
        template = template.replace('[','').replace(']','').replace("'", '')
        return template.format(self)

class AddressBook:
    def __init__(self):
        self.book = {}

    def add_contact(self, p):
        self.book[p] = p
        return self.book

    def search_contact(self, lastname):
        hits = []
        for p in self.book:
            if p.lastname == lastname:
                hits.append(
                    '{}, {}, -- Phone Number: {}, --Email: {}'.format(
                    p.lastname, p.firstname, p.phone_number, p.email))
        return hits

if __name__ == '__main__': 
    a = AddressBook()
    added = a.add_contact(Person('Homer', 'Simpson', '5559355899', 
                                 'homer@mail.com'))
    # print(added)
    added_1 = a.add_contact(Person('Marge', 'Simpson', '5559352365', 
                                   'marge@mail.com'))
    # print(added_1)
    search = a.search_contact('Simpson')
    for s in search:
        print(s)

输出:

Simpson, Homer, -- Phone Number: 5559355899, --Email: homer@mail.com
Simpson, Marge, -- Phone Number: 5559352365, --Email: marge@mail.com