Search code examples
javapythonpython-decoratorsjava-annotations

Is a Python Decorator the same as Java annotation, or Java with Aspects?


Are Python Decorators the same or similar, or fundamentally different to Java annotations or something like Spring AOP, or Aspect J?


Solution

  • Python decorators are just syntactic sugar for passing a function to another function and replacing the first function with the result:

    @decorator
    def function():
        pass
    

    is syntactic sugar for

    def function():
        pass
    function = decorator(function)
    

    Java annotations by themselves just store metadata, you must have something that inspects them to add behaviour.

     

    Java AOP systems are huge things built on top of Java, decorators are just language syntax with little to no semantics attached, you can't really compare them.