While you're at it you might as well `assert 0 <= key < self.num_layers` (yes that syntax works).