logo

Annotations vs Decorators

Last Updated: 2024-01-20

Java Annotations and Python Decorators look very similar:

Java Annotations:

@Foo
void bar() {}

Python Decorators:

@foo
def bar():
    pass

But they are quite different:

Java Annotations are just containers for metadata. They can be used as documentations or to instruct the compilers to take actions (e.g. checks, surpass warnings, etc).

Python Decorators provide a way to achieve higher-order functions, the example above is just syntactic sugar for:

def bar():
    pass
# Pass bar() to foo() as an argument
bar = foo(bar)

Read more about Annotations / Attributes and Higher-order Functions in different languages.