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

bn. picture bn. · Mar 11, 2013 · Viewed 15.3k times · Source

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

Answer

Pavel Anossov picture Pavel Anossov · Mar 11, 2013

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.