python中$和@基础笔记

 

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!
posted @ 2018-06-02 13:44  点点花飞谢  阅读(7699)  评论(0编辑  收藏  举报