E
E
erma1232021-06-30 10:43:04
Python
erma123, 2021-06-30 10:43:04

How to make a method from a list subclass in which there are several dictionaries that will display the value from the key?

class ContactList(list):
    def __init__(self, **kwargs):
        self.kwargs = kwargs

    def search_by_name(self, name):
        if name in self.kwargs:
            return self.kwargs.values()

all_contacts = ContactList(a=1, b=2, c=3)
print(all_contacts)
all_contacts.search_by_name('c')

#Метод search_by_contact должен вывести 3, но он ничего не выводит а в переменной all_contacts пустой список
#Подскажите как можно решить эту задачу

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
Maxim, 2021-06-30
@erma123

return self.kwargs.values() will return you the values ​​of all keys in the dictionary. You need to change the method like this:

def search_by_name(self, name):
    return self.kwargs.get(name)

In this case, you will either return the value of the name key, if it exists, or None if it does not exist.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question