Chain of Responsibility design I'm trying to design a chain of responsibility pattern.
It slightly differ from traditional way when handler does only one action on a request, f.e. handles http response considering status code.
But my handlers have to do two actions. Below is an (dummy) example where handler asks a question first and then writes the user response to some store:
# some object where we keep surname and name 
class Request:
    pass


# some outer storage
store = {}


# chain of responsibility implementation

class PersonIdentifier:

    def __init__(self) -&gt; None:
        self.handlers = []

    def add_handler(self, handler):
        if self.handlers:
            self.handlers[-1].set_next_handler(handler)
        self.handlers.append(handler)

    def handle(self, request):
        return self.handlers[0].handle_request(request)


# handlers for chain of responsibility

class BaseHandler:
    handler_name = None

    def __init__(self) -&gt; None:
        self.next_handler = None

    def set_next_handler(self, handler):
        self.next_handler = handler


class SurnameHandler(BaseHandler):
    handler_name = 'surname'
    handler_name_answer = 'surname-answer'
    question = 'Type your surname: '

    def handle_request(self, request):
        if request.handler_name == self.handler_name:
            return self.question
        elif request.handler_name == self.handler_name_answer:
            global store
            store['surname'] = request.surname
            del request.surname
            request.handler_name = 'name'
            return self.next_handler.handle_request(request)
        else:
            return self.next_handler.handle_request(request)


class NameHandler(BaseHandler):
    handler_name = 'name'
    handler_name_answer = 'name-answer'
    question = 'Type your name: '

    def handle_request(self, request):
        if request.handler_name == self.handler_name:
            return self.question
        elif request.handler_name == self.handler_name_answer:
            global store
            store['name'] = request.name


user_request = Request()

chain = PersonIdentifier()
chain.add_handler(SurnameHandler())
chain.add_handler(NameHandler())

# first ask for surname
user_request.handler_name = 'surname'
surname = input(chain.handle(user_request))
user_request.handler_name = 'surname-answer'
user_request.surname = surname

# then ask for name
name = input(chain.handle(user_request))
user_request.name = name
user_request.handler_name = 'name-answer'
chain.handle(user_request)

print(store)

It looks ugly for me. What do I dislike more is using handler_name and handler_name_answer.
May be you could offer nicer way to solve my task?
Chain of Responsibility pattern (hereby &quot;CORP&quot;) includes two things: iteration and &quot;handling&quot;.
IMO, the iteration part, at least in modern programming languages such as Python and Java, can be easily &quot;extracted&quot; to an external iteration. So eventually the code could look like:
result = None
for handler in handlers:
  temp_result = handler.handle(request)
  if temp_result is not None:
    result = temp_result
    break
if result is not None:
  # handled!

I think this approach is simpler than maintaining the inner chain, and it has the same impact.
In CORP, once a handler was found, and it can handle the input, no additional handlers should be executed. It's quite similar to filtering.
However, I think that the code we have in your question tries to execute as many handlers as it can (as it tries to enrich a wider context). So maybe CORP is not really needed here.
Perhaps you can have enriching classes, that have a method enrich that accepts a request and a context, so that every enriching class could either change the state of the context object, or leave it as is:
class BaseEnricher:
  def enrich(request, store):
    pass