Python学习_09 装饰器

Python学习_09 装饰器
2018-04-17
一、课前复习:
1、字符串复习:
s.format()          格式化
s.isalpha()         是否字母
s.isspace()         是否空格
s.isdigit()         是否数字
s.isnumeric()       是否数字字符
s.istitle()         单词是否首字母大写
s.isalnum()         是否字母和数字
s.islower()         是否小写字母组成
s.strip()           移除字符串首位制定的字符,默认空格
s.index()           下标
s.capitalize()      首字母大写
s.count()           统计
s.endswith()        是否以**结尾
s.find()            检测字符串是否以**开头和**结尾
s.join()            连接
s.lower()           全部转换成小写
s.split()           通过指定分隔符对字符串切片
2、列表复习
s.remove()      移除指定的对象
s.copy()        复制
s.append()      列表末尾添加
s.index()       下表
s.count()       统计
s.sort()        排序
s.reverse()     反向排序
s.insert()      插入指定位置
s.clear()       清空列表
s.extend()      一般用于两个列表相加
3、元组复习
s.clear()       删除字典内所有所有元素
s.copy()        复制
s.pop()         删除字典给定键 key 所对应的值,返回值为被删除的值
s.fromkeys()    用于创建一个新字典,以传入参数作为新的value
s.get()         返回指定key的value
s.items()       返回key、value
s.update()      相当于2个字典相加形成新的字典
二、装饰器
1、定义
         在不改变函数源代码和方法的前提下,给函数前面和后面添加一些功能;装饰器通过@符号进行使用,相当于把函数作为参数传给他;
@startEND 装饰器相当于hello = startEND(hello());当你调用hello()的时候,就相当于调用了startEnd(hello()) 
2、简单装饰器

#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time    : 2018\4\17 0017 22:10
# @Author  : xiexiaolong
# @File    : demon2.py

def startEnd(fun):
    def a():
        print("#####################")
        fun()
        print("@@@@@@@@@@@@@@")
    return a
@startEnd
def hello():
    print("hello world")
h = hello
h()
结果:
D:\python\venv\Scripts\python.exe D:/python/第八节课装饰器/demon2.py
#####################
hello world
@@@@@@@@@@@@@@

Process finished with exit code 0
分析:当调用函数的时候,相当于把函数当作参数传给装饰器,然后执行装饰器的时候再执行函数
3、带一个参数的装饰器
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time    : 2018\4\17 0017 22:24
# @Author  : xiexiaolong
# @File    : demon3.py

def startEnd(fun):
    def a (name):
        print("this is {0}".format(name))
        print("###############")
        fun()
        print("@@@@@@@@@@@@@@@@")
    return a
@startEnd
def hello():
    print("hello word")

h = hello
h("xiexiaolong")
结果:
D:\python\venv\Scripts\python.exe D:/python/第八节课装饰器/demon3.py
this is xiexiaolong
###############
hello word
@@@@@@@@@@@@@@@@

Process finished with exit code 0
分析:函数加了参数
4、带2个参数的装饰器
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time    : 2018\4\17 0017 22:55
# @Author  : xiexiaolong
# @File    : demon4.py

def startEnd(au):
    def a (fun):
        def b(name):
            print("this is {0}".format(au))
            print("############")
            fun(name)
            print("@@@@@@@@@@@")
        return b
    return a

@startEnd("xxl")
def hello(name):
    print("hell {0}".format(name))
h = hello
h("xiexiaolong")
结果:
D:\python\venv\Scripts\python.exe D:/python/第八节课装饰器/demon4.py
this is xxl
############
hell xiexiaolong
@@@@@@@@@@@

Process finished with exit code 0
分析:函数传入一个name的参数,装饰器带一个默认参数au=xxl


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值