Menu
CoddyTech

Composition Shares Objects

Lesson 38 of 48 in Coddy's Python for Lua Developers course.

Lua tables can hold references to other tables; Python objects can hold references to other objects. In both examples, the bill stores the same rate object the caller retains. Later mutation of that rate affects the next bill calculation. Composition delegates behavior to the contained dependency without requiring inheritance. If the design needs a snapshot instead, copy the relevant immutable data or explicitly construct an independent dependency.

Lua example

local rate = {cents = 4}
function rate:cost(units) return self.cents * units end
local bill = {rate = rate}
function bill:total(units) return self.rate:cost(units) end
rate.cents = 7
print(bill:total(2))

Python

class Rate:
    def __init__(self, cents):
        self.cents = cents
    def cost(self, units):
        return self.cents * units
class Bill:
    def __init__(self, rate):
        self.rate = rate
    def total(self, units):
        return self.rate.cost(units)
rate = Rate(4)
bill = Bill(rate)
rate.cents = 7
print(bill.total(2))

Porting trap

A constructor assignment does not make the supplied object exclusive or frozen in either language.

Implement the specified API in the editable Python files. The locked driver.py runs the program and supplies input/output. Keep its imports and names unchanged.

challenge icon

Challenge

Beginner

Implement Rate(cents) with cost(units) returning cents times units, and UsageBill(rate) that stores its collaborator as rate. Its total(units) must delegate to the collaborator's cost(units). Input is four nonnegative integers on separate lines: rate, units, replacement rate, and bonus. The locked driver prints the first total, replaces the collaborator with a ready-made object whose cost is replacement_rate * units + bonus, and prints the new total. A bill must use its current collaborator, not cache a formula or initial result.

The locked driver handles input and output. Submit only the required class and function definitions with the exact API names. Do not read input or print from your classes.

Your code goes in the editable files rate.py and usage_bill.py. driver.py holds the supplied input/output code and API checks and cannot be edited. Keep the supplied imports; do not read input or print from the editable modules.

Try it yourself

from rate import Rate
from usage_bill import UsageBill

class BonusRate:
    def __init__(self, cents, bonus):
        self.cents = cents
        self.bonus = bonus

    def cost(self, units):
        return self.cents * units + self.bonus

cents, units, replacement, bonus = [int(input()) for _ in range(4)]
bill = UsageBill(Rate(cents))
print(bill.total(units))
bill.rate = BonusRate(replacement, bonus)
print(bill.total(units))
quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

All lessons in Python for Lua Developers

Practice on your own: Online Python compiler