python 2.4以后,增加了@符号修饰函数对函数进行修饰,python3.0/2.6又增加了对类的修饰。

$ 在正则表达式中,匹配一个字符串的末尾。(参考http://www.runoob.com/python/python-reg-expressions.html)
@符号是装饰器的语法糖,在定义函数的时候使用,避免再一次赋值操作(具体请参考https://blog.csdn.net/yjreset/article/details/79329979)

import time

def time(func):
    print(time.ctime())
    return func()

@time  # 从这里可以看出@time 等价于 time(xxx()),但是这种写法你得考虑python代码的执行顺序
def xxx():
    print('Hello world!')

 

运行结果:
Wed Jul 26 23:01:21 2017
Hello world!

相关文章:

  • 2021-05-29
  • 2021-10-13
  • 2022-01-11
  • 2021-12-13
猜你喜欢
  • 2021-12-26
  • 2021-08-24
  • 2022-02-10
  • 2022-03-07
相关资源
相似解决方案