Chain of responsibility: OOP Pattern - AndrewMZ6/python_cheat_sheet GitHub Wiki

The chain processes input number depending on it's value

class abstract:
	def __init__(self, nx = None):
		self.nx = nx

	def process(self, inp):
		pass

	def passtonext(self, inp):
		if self.nx != None:
			self.nx.process(inp)

class Concrete1(abstract):
	def process(self, inp):
		if 0 < inp < 10:
			print(f"the input {inp} is processed by {__class__.__name__}")
		else:
			print(f"the input {inp} cannot be processed by {__class__.__name__}. It's going to be passed to {self.nx.__class__.__name__}")

			self.passtonext(inp)

class Concrete2(abstract):
	def process(self, inp):
		if 10 < inp < 20:
			print(f"the input {inp} is processed by {__class__.__name__}")
		else:
			print(f"the input {inp} cannot be processed by {__class__.__name__}. It's going to be passed to {self.nx.__class__.__name__}")
			self.passtonext(inp)

class Default:
	@classmethod
	def process(cls, inp):
		print(f"the input {inp} cannot be processed by any class")
		return

num = 45
c2 = Concrete2(Default)
c1 = Concrete1(c2)
c1.process(num)

output:

the input 45 cannot be processed by Concrete1. It's going to be passed to Concrete2
the input 45 cannot be processed by Concrete2. It's going to be passed to type
the input 45 cannot be processed by any class