Duck-Typed Notification Fan-out

Medium ⏱ 12 min 45% acceptance ★★★★☆ 4.2
Write broadcast(channels, msg) that calls .send(msg) on every object in channels and collects the results — plus two example channels, Email returning f'email: {msg}' and Sms returning f'sms: {msg}'. No shared base class, no isinstance: if it has send, it is a channel. That is duck typing.

Examples

Example 1
Input
broadcast([Email(), Sms()], 'hi')
Output
['email: hi', 'sms: hi']
Explanation

Each object answered the same message its own way.

Constraints

  • No common superclass.
  • broadcast knows only about .send.

Topics

OOPduck typing

Companies

TwilioSlackZoho

Hints

Hint 1

A list comprehension over channels.

Hint 2

The interface is implicit — just the method name.

Loading the Python runtime… Run executes your code and shows printed output; Submit checks your function against this problem's examples.