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

前端 未结 4 527
庸人自扰
庸人自扰 2020-12-23 16:03

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

4条回答
  •  轮回少年
    2020-12-23 16:44

    I use both of them in a similar way: to turn on/off debugging or testing options.

    For example (Python decorators):

    def measure_time(func):
        def _measure_time(*args, **kwargs):
            t0 = time.time()
            ret = func(*args, **kwargs)
            print "time=%lf" % (time.time()-t0)
            ...
            return ret
        return _measure_time
    
    
    @measure_time
    def train_model(self):
        ...
    

    For Java annotations, use getAnnotation, etc. can do the similar jobs or more complicated ones.

提交回复
热议问题