4 ms·
> Decorator is Python’s implementation to visitor pattern... huh?
by utku_karatas2 17y ago
> Decorator is Python’s implementation to visitor pattern...
huh?
- didip 17y agoVisitor patterns add new functionality to existing object structure [http://en.wikipedia.org/wiki/Visitor_pattern http://en.wikipedia.org/wiki/Visitor_pattern]. Python decorator does not do that?
- tetha 17y agoThe decorator syntax @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.
- didip 17y agoPython decorator can do more than just that. @decorator1 can be a completely different class which manipulate foo.__class__
- tetha 17y agoCertainly, it is an arbitrary python function, so the resulting foo might be a dictionary or whatever. But this doesn't push it closer to the visitor pattern. 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.