Bird
0
0

Given this class, how can you add a property full_name that combines first_name and last_name?

hard📝 Application Q8 of 15
Python - Encapsulation and Data Protection

Given this class, how can you add a property full_name that combines first_name and last_name?

class User:
    def __init__(self, first_name, last_name):
        self.first_name = first_name
        self.last_name = last_name

    # Add full_name property here
Adef full_name(self): return self.first_name + self.last_name
Bdef full_name(self): return f"{self.first_name} {self.last_name}"
C@property def full_name(self): return self.first_name + self.last_name
D@property def full_name(self): return f"{self.first_name} {self.last_name}"
Step-by-Step Solution
Solution:
  1. Step 1: Define a property method

    Use @property decorator to define full_name so it can be accessed like an attribute.
  2. Step 2: Return combined string with space

    Use an f-string to combine first_name and last_name with a space between.
  3. Final Answer:

    @property def full_name(self): return f"{self.first_name} {self.last_name}" -> Option D
  4. Quick Check:

    Property returns combined string with space [OK]
Quick Trick: Use @property and f-string to combine names [OK]
Common Mistakes:
  • Missing @property decorator
  • Concatenating without space
  • Defining method without decorator

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Python Quizzes