> Decorator is Python’s implementation to visitor pattern...
huh?
huh?
Python decorator does not do that?
@decorator2
@decorator1
def foo(*args):
pass
is just a shorthand for def foo(*args):
pass
foo = decorator2(decorator1(foo))
I completely fail to see the visitor pattern here, even with stretching until breaking.@decorator1 can be a completely different class which manipulate foo.__class__
Certainly, you can use decorators as a tool to implement the visitor pattern by implementing some hairy, magical reflecting class-decorator which adds a method visit to an object, which then calls visit_{CLASSNAME} on the parameter, but this is just a way of implementing this and not the visitor pattern in itself.