Python Decorator and Java Annotation have the same syntax, when I see Python Decorator the first time, I assume it's behave the same as Java Annotation, but indeed this is wrong

python_and_java

Python Decorator is just syntactic sugar for passing a function to another function and replacing the first function with the result, Java Annotation is just a meta data store, it could store additional information about the java artifacts(like class, method), we could have logic based on the java annotations

Pytnon Decorator

functions are first class object in python language, so we could pass funciton as object as parameter, return function as result, this is the foundation of python decorator. so the code like below

def py_decorator(func):
    def wrapper():
        print("before the function is called.")
        func()
        print(after the function is called.")
    return wrapper
def greet():
    print("hello!")

greet = py_decorator(greet)    

is the same as

@py_decorator
def greet():
    print("hello!")

it's just an syntactic sugar, there are more useful tips about python decorators in real python

Java Annotation

Java Annotation is just a way to add comments to the java class, or method or attribute, it could avaialbe in java compile time or jvm runtime, to declare a java annotation, we could do like below

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.FIELD)
public @interface ReadOnly {

}

this annotation will define an annotation named ReadOnly, this annotation could be applied on java attribute, the annotation will be avaialble in jvm runtime. after we define the annotation, we could apply this in java code like below

Class DBFactory {

@ReadOnly
private DBProvider dbProvider;
}

this is like add a comment on attribute dbProvider in jvm that this field is readonly nothing else, if we want any behaviour about this annotation, we have to add logic at runtime to control the behavior , thsi is the foundation of Google Guice, Google Guice heavily rely on java annotation to config the java runtime behavoiur and do dependency injection for us.