MyTetra Share
Делитесь знаниями!
ContactList
Время создания: 12.09.2017 13:50
Раздел: Python - Задачник - OOP
Запись: xintrea/mytetra_db_mcold/master/base/1505213434pr0rdc6p8q/text.html на raw.githubusercontent.com
class ContactList(list):
"""
Class - list of contacts
"""
# method search
def search(self, name):
"""
Search name in list of contacts
:param name:
:return: list
"""
matching_contacts = []
for contact in self:
if name in contact.name:
matching_contacts.append(contact)
return matching_contacts


class Contact:
"""
Class - contact
"""
all_contacts = ContactList()

def __init__(self, name, email):
"""
Constructor
:param name:
:param email:
"""
self.name = name
self.email = email
self.all_contacts.append(self)

if __name__ == '__main__':
c1 = Contact(
"John A", "johna@example.net")
c2 = Contact(
"John B", "johnb@example.net")
c3 = Contact(
"Jenna C", "jennac@example.net")
print([c.name for c in Contact.all_contacts.search('John')])
 
MyTetra Share v.0.65
Яндекс индекс цитирования