The Law of Demeter is an object-oriented design principle that says you should never call methods on objects you receive from other calls. Or, put another way: Only talk to your immediate friends.
Figure 8.2: The Law of Demeter
We'll now modify our Flight class to accept an aircraft object when it is constructed, and we’ll follow the Law of Demeter by adding a method to report the aircraft model. This method will delegate to Aircraft on behalf of the client rather than allowing the client to "reach through" the Flight and interrogate the Aircraft object directly:
class Flight: """A flight with a particular passenger aircraft.""" def __init__(self, number, aircraft): if not number[:2].isalpha(): raise ValueError("No airline code in '{}'".format(number)) if not number[:2].isupper(): raise ValueError("Invalid airline code'{}'".format(number)) if not (number[2:].isdigit() and int(number[2:]) <= 9999): ...