visitor pattern self visitor compositevisitor(truetrueself my_home_system accept(self visitorretrieved_state sorted([str( statusfor in self my_home_system iterable]self assertequal(retrieved_statesorted(expected_state)if __name__ ="__main__"unittest main( have just couple of notes on the preceding code firstif you are taking input from userneverever use the eval functionas python blindly executes whatever is in the string passed to eval secondwe are choosing to balance the general nature of eval by using the name of the class to be visitedwhich is not the best way in pythonas now you rely on naming conventions and magicwhich is not explicit stillyou could implement the pattern if you were thinking state machine might help the situationyou are not wrong this brings us to something that would seem obvious once you read itbut might not be so obvious from the get-goand that is the fact that you can use more than one design pattern in single implementation in this caseit should be clear that the state of the house resembles state machine we have four statesperson is not homeperson is not home person is not homeperson is home person is homeperson is not home person is homeperson is home whenever person either leaves or arrives at home the state of the system changes soimplementing state machine for the visitor seems like good idea try implementing the state pattern on the previous code snippet as an exercise when you separate the data structures in the system from the algorithms that operate on themyou gain an added benefit you can adhere to the open-closed principle much more easily this principle sees you write code where your objects are open to extension (using inheritancebut closed to alteration (by setting or changing values in an object or altering the methods on an objectlike the dry and yagni principlesthe open-closed principle serves as guiding light as you plan and develop system violations of these principles also serve as an early-warning system that you are writing unmaintainable code that will come back and hurt you down the line