🐍 Python for Everyone

← Back to exercises

Chapter 15 Practice Bank 70-90 min, hands-on

Practice Bank: OOP Deeper

A deeper set of practice problems, organized by topic, for extra reps beyond the main exercises — including scenario-based problems written in the same style you'll meet in real interviews. This is the chapter where @staticmethod, @classmethod (alternative constructors), and @property (getters and setters) become allowed, on top of everything from Chapters 1-14 (including general classes, __init__, instance attributes, instance methods, and __str__/__repr__). No inheritance (Chapter 16), or any import beyond math/datetime/random/csv.

How to run these Download starter.py below and run python3 starter.py from inside the same folder.

Topic 1: @staticmethod

1 A utility calculation on the class

Find # TODO 1.1. Write a TemperatureConverter class with a @staticmethod celsius_to_fahrenheit(celsius) callable directly on the class.

2 A pure text check

Find # TODO 1.2. Write a StringHelper class with a @staticmethod is_palindrome(text).

3 A number check

Find # TODO 1.3. Write a MathHelper class with a @staticmethod is_prime(number).

4 A format check

Find # TODO 1.4. Write a Validator class with a @staticmethod is_valid_email(email).

5 Debug the Code

Find # TODO 1.5. This @staticmethod mistakenly includes self as a parameter. Fix it.

💼 Scenario

A shared tax calculator utility

Find # TODO 1.A. Write TaxCalculator with a @staticmethod calculate_tax(subtotal, tax_rate).

💼 Interview Prep

Why @staticmethod instead of a plain function?

Find # TODO 1.B. Write explain_staticmethod_purpose(), explaining why you'd use @staticmethod instead of a standalone function.

Topic 2: @classmethod and Alternative Constructors

1 A named alternative constructor

Find # TODO 2.1. Write a Pizza class with a @classmethod margherita(cls) alternative constructor.

2 Parsing a string into an instance

Find # TODO 2.2. Write a Date class with a @classmethod from_string(cls, date_string) that parses "YYYY-MM-DD".

3 A convenience constructor

Find # TODO 2.3. Write an Employee class with a @classmethod entry_level(cls, name).

4 A special-case constructor

Find # TODO 2.4. Write a Rectangle class with a @classmethod square(cls, side_length).

5 Debug the Code

Find # TODO 2.5. This @classmethod hardcodes the class name instead of using cls(...). Fix it.

💼 Scenario

Building a user from an API response

Find # TODO 2.A. Write UserProfile with a @classmethod from_api_response(cls, data).

💼 Interview Prep

Why more than one way to construct an instance?

Find # TODO 2.B. Write explain_classmethod_alternative_constructors(), explaining why a class needs more than one way to build an instance.

Topic 3: Encapsulation & Privacy Conventions

1 A single-underscore convention

Find # TODO 3.1. Write a BankAccount class using a single-underscore _balance convention with a get_balance(self) method.

2 Name-mangling in action

Find # TODO 3.2. Write a Config class demonstrating double-underscore name-mangling with __secret_key.

3 Consenting adults

Find # TODO 3.3. Write a Person class showing that a single-underscore attribute is still reachable from outside.

4 Using a mangled attribute internally

Find # TODO 3.4. Write a Wallet class using __balance, fully usable from inside the class's own methods.

5 Debug the Code

Find # TODO 3.5. This code tries to read a mangled double-underscore attribute with the wrong name. Fix it.

💼 Scenario

Protecting an internal cache

Find # TODO 3.A. Write ReportCache, hiding a __cache dict behind store/get methods.

💼 Interview Prep

The "consenting adults" philosophy

Find # TODO 3.B. Write explain_consenting_adults_philosophy(), explaining Python's approach to private attributes.

Topic 4: @property Getters

1 Reading a method like an attribute

Find # TODO 4.1. Write a Circle class with a @property radius read like a plain attribute.

2 A computed property

Find # TODO 4.2. Write a Rectangle class with a computed @property area.

3 Combining two attributes

Find # TODO 4.3. Write a Person class with a @property full_name combining two stored attributes.

4 A unit-converted property

Find # TODO 4.4. Write a Temperature class with a computed @property fahrenheit.

5 Debug the Code

Find # TODO 4.5. This code wrongly calls a @property with parentheses. Fix it.

💼 Scenario

A read-only derived field on a user profile

Find # TODO 4.A. Write UserProfile with a computed @property age.

💼 Interview Prep

Why @property instead of get_area()?

Find # TODO 4.B. Write explain_property_getters(), explaining why you'd use @property instead of a regular getter method.

Topic 5: @x.setter and Validation on Assignment

1 Rejecting a negative radius

Find # TODO 5.1. Write a Circle class with a radius setter that rejects negative values.

2 Rejecting a negative age

Find # TODO 5.2. Write a Person class with an age setter that rejects negative values.

3 Rejecting a negative price

Find # TODO 5.3. Write a Product class with a price setter that rejects negative values.

4 Validating a format

Find # TODO 5.4. Write an Email class with an address setter that validates the presence of "@".

5 Debug the Code

Find # TODO 5.5. This Thermostat setter's absolute-zero validation condition is backwards. Fix it.

💼 Scenario

Validating signup form data

Find # TODO 5.A. Write UserAccount with a username setter that rejects blank input.

💼 Interview Prep

Why validate in a setter, not just __init__?

Find # TODO 5.B. Write explain_property_setters(), explaining why validation belongs in a @property setter.

Topic 6: Bringing It Together — Deeper OOP in Production

1 Property, static, and classmethod together

Find # TODO 6.1. Write a Product class combining a validated price property, a @staticmethod apply_tax, and a @classmethod on_sale.

2 A private auto-incrementing id

Find # TODO 6.2. Write an Employee class combining a private auto-incrementing id, a validated salary property, and a @classmethod hire_intern.

3 Restocking through the setter

Find # TODO 6.3. Write an InventoryItem class combining a validated quantity property, a restock method routed back through the setter, and a @staticmethod reorder_threshold.

4 Withdrawing through the setter

Find # TODO 6.4. Write a BankAccount class combining a validated balance property, a withdraw method routed through the setter, and a @classmethod open_with_bonus.

5 Debug the Code

Find # TODO 6.5. This Subscription class's __init__ bypasses its own setter's validation. Fix it.

💼 Scenario

A small payroll system

Find # TODO 6.A. Write Employee combining a @staticmethod monthly-to-annual conversion with a @classmethod alternative constructor.

💼 Interview Prep

How the three tools work together

Find # TODO 6.B. Write explain_deeper_oop_production_value(), describing how @staticmethod, @classmethod, and @property work together in a well-designed class.

Checking your work Compare your output against solution.py. Your exact wording doesn't need to match — the goal is that your program runs without errors and does what each TODO asks.
← Exercises Challenges →