Sobes.tech
Junior — Senior

Retrieving selected attributes from a class object

livecode

Task condition

Create a function that takes an object of any class and an array of attribute names. The function should return a dictionary where the keys are the specified attribute names, and the values are the corresponding attribute values of the given object.

def extract_fields(instance, fields):
    # your code here
    pass

Example usage:

class User:
    def __init__(self, name, age, email):
        self.name = name
        self.age = age
        self.email = email

u = User('Alice', 30, 'alice@example.com')
result = extract_fields(u, ['name', 'email'])
# result should be {'name': 'Alice', 'email': 'alice@example.com'}