Menu

Sign in to track your progress and unlock all features.

Theme style

Log in

Full lesson preview

Modify Instance Attributes

Learn to change attributes on an object instance after it's created.

Python practice6 minClasses & Objects FundamentalsBeginnerLast updated April 7, 2026

Problem statement

You are given a Person class with instance attributes name and age. Implement the function modify_person(name, age, new_name=None, new_age=None) that: - Creates a Person with the provided name and age. - If new_name is not None, update the person's name to new_name. - If new_age is not None, update the person's age to new_age. - Return a tuple (person.name, person.age) representing the updated state. This exercise practices creating instances and modifying instance attributes directly.

Task

Create an object from a class and modify its instance attributes directly; return the updated state.

Examples

Change both name and age

Input

modify_person("Alice", 30, "Alicia", 31)

Output

('Alicia', 31)

A Person is created with ('Alice', 30). Both attributes are updated and the new tuple is returned.

Input format

Four parameters: name (str), age (int), new_name (str or None), new_age (int or None).

Output format

A tuple (name, age) representing the person's updated attributes.

Constraints

name and new_name are strings; age and new_age are integers or None. Do not print; return the tuple.

Samples

Sample 1

Input

modify_person("Bob", 20, None, 21)

Output

('Bob', 21)

Only the age is updated because new_name is None.