If I were going to fix this problem (which I may well do soon -- I have to take another pass through this material when writing the book), I would actually define the __getitem__() function instead, as that controls the behavior of the [] operator.
This all comes down to separation of concerns. It shouldn't be the responsibility of __next__() to mutate the internal data to obscure information. It's only job should be to determine whether it exposes that information, and how.
Of course, in all honesty, there's nothing preventing a direct call to agents._roster[1] (Python has no private variables). If we were going to obfuscate or remove classified data, that should really occur on the add_agent() function.
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
If I were going to fix this problem (which I may well do soon -- I have to take another pass through this material when writing the book), I would actually define the
__getitem__()
function instead, as that controls the behavior of the[]
operator.This all comes down to separation of concerns. It shouldn't be the responsibility of
__next__()
to mutate the internal data to obscure information. It's only job should be to determine whether it exposes that information, and how.Of course, in all honesty, there's nothing preventing a direct call to
agents._roster[1]
(Python has no private variables). If we were going to obfuscate or remove classified data, that should really occur on theadd_agent()
function.