Python 函数修饰器
# 一、用函数修饰函数
#!/usr/bin/python3 def decorate_func(func):
def call(*args, **kwargs):
print('you have called %s()' % (func.__name__))
func(*args, **kwargs)
return call @decorate_func
def func(name):
print('I am not a party member.my name is %s.' % (name))
func(name='Tom')
# 二、用函数修饰类
#!/usr/bin/python3 def decorate_class(aClass):
def call(*args, **kwargs):
print('you have create a %s class, its name is %s.' % (aClass.__name__, args[0]))
return aClass(*args, **kwargs)
return call @decorate_class
class People():
def __init__(self, name):
self.name = name
def print(self):
print('My name is %s.' % (self.name)) a = People('Tom')
b = People('Jerry')
# 三、用函数修饰类方法
#!/usr/bin/python3 def decorate_method(func):
def call(*args, **kwargs):
print('you have called %s().' % (func.__name__))
func(*args, **kwargs)
return call class aClass():
def __init__(self, name):
self.name = name
@decorate_method
def print(self):
print('My name is %s.' % (self.name))
a = aClass('Jerry')
a.print()
# 四、用类修饰函数
#!/usr/bin/python3
class decorate_func():
def __init__(self, func):
self.func = func def __call__(self, *args, **kwargs):
print('youhave called %s().' % (self.func.__name__))
self.func(*args, **kwargs) @decorate_func
def func(name):
print('My name is %s.' % (name)) func('Bob')
# 五、用类修饰类
#!/usr/bin/python3 class decorate_class():
def __init__(self, aClass):
self.aClass = aClass
def __call__(self, *args, **kwargs):
print('You have created a %s class.' % (self.aClass.__name__))
return self.aClass(*args, **kwargs) @decorate_class
class People():
def __init__(self, name):
self.name = name
def print(self):
print('My name is %s .' % (self.name)) a = People('Tom')
# 六、匿名函数作为函数修饰器
#~/usr/bin/python3 def attrsetter(attr, value):
""" Return a function that sets ``attr`` on its argument and returns it. """
return lambda method: setattr(method, attr, value) or method def depends(*args):
if args and callable(args[0]):
args = args[0]
elif any('id' in arg.split('.') for arg in args):
raise NotImplementedError("Compute method cannot depend on field 'id'.")
return attrsetter('_depends', args) class People():
def __init__(self, name):
self.name = name
# 这里用的是函数修饰器,但不是depends函数,而是depends函数执行完之后返回的匿名函数作为函数装饰器
@depends('test')
def print(self):
print('My name is %s .' % (self.name, )) a = People('Tom')
a.print()
print(a.print.__dict__)
Python 函数修饰器的更多相关文章
- python函数修饰器(decorator)
python语言本身具有丰富的功能和表达语法,其中修饰器是一个非常有用的功能.在设计模式中,decorator能够在无需直接使用子类的方式来动态地修正一个函数,类或者类的方法的功能.当你希望在不修改函 ...
- Python 函数修饰符(装饰器)的使用
Python 函数修饰符(装饰器)的使用 1. 修饰符的来源修饰符是一个很著名的设计模式,经常被用于有切面需求的场景,较为经典的有插入日志.性能测试.事务处理等. 修饰符是解决这类问题的绝佳设计, ...
- Python函数装饰器原理与用法详解《摘》
本文实例讲述了Python函数装饰器原理与用法.分享给大家供大家参考,具体如下: 装饰器本质上是一个函数,该函数用来处理其他函数,它可以让其他函数在不需要修改代码的前提下增加额外的功能,装饰器的返回值 ...
- python 类修饰器
1. 修改类函数. 场景: 如果要给一个类的所有方法加上计时,并打印出来.demo如下: # -*- coding:utf-8 -*- import time def time_it(fn): &qu ...
- python函数修饰符@的使用
python函数修饰符@的作用是为现有函数增加额外的功能,常用于插入日志.性能测试.事务处理等等. 创建函数修饰符的规则:(1)修饰符是一个函数(2)修饰符取被修饰函数为参数(3)修饰符返回一个新函数 ...
- python函数-装饰器
python函数-装饰器 1.装饰器的原则--开放封闭原则 开放:对于添加新功能是开放的 封闭:对于修改原功能是封闭的 2.装饰器的作用 在不更改原函数调用方式的前提下对原函数添加新功能 3.装饰器的 ...
- Python函数装饰器高级用法
在了解了Python函数装饰器基础知识和闭包之后,开始正式学习函数装饰器. 典型的函数装饰器 以下示例定义了一个装饰器,输出函数的运行时间: 函数装饰器和闭包紧密结合,入参func代表被装饰函数,通过 ...
- Python @函数装饰器及用法
1.函数装饰器的工作原理 函数装饰器的工作原理是怎样的呢?假设用 funA() 函数装饰器去装饰 funB() 函数,如下所示: #funA 作为装饰器函数 def funA(fn): #... fn ...
- Python @函数装饰器及用法(超级详细)
函数装饰器的工作原理是怎样的呢?假设用 funA() 函数装饰器去装饰 funB() 函数,如下所示: #funA 作为装饰器函数 def funA(fn): #... fn() # 执行传入的fn参 ...
随机推荐
- k8s client-go源码分析 informer源码分析(3)-Reflector源码分析
k8s client-go源码分析 informer源码分析(3)-Reflector源码分析 1.Reflector概述 Reflector从kube-apiserver中list&watc ...
- mysql Bad handshake
由于 Java 程序访问 MySQL 时,MySQL 抛出 Bad handshake 错误,导致接口抛错,然后在 MySQL 配置文件新增 skip_ssl 配置(忽略 SSL 密钥和证书文件),重 ...
- 免费yum源镜像地址
收集的镜像,yum源等网站地址 阿里巴巴开源镜像站 https://opsx.alibaba.com/mirror http://mirrors.aliyun.com/centos/ 网易开源镜像站 ...
- NetCore框架WTM的分表分库实现
介绍 本期主角: ShardingCore 一款ef-core下高性能.轻量级针对分表分库读写分离的解决方案,具有零依赖.零学习成本.零业务代码入侵 WTM WalkingTec.Mvvm框架(简称W ...
- wsl2环境搭建
序言 我电脑配置不高,开虚拟机跑linux总觉得太卡.最近才了解到windows早就上了wsl2--一款较为轻量的虚拟机软件.所以本篇博客偏笔记向,存粹记录以便多次使用. 环境 宿主机windows1 ...
- Linux Cgroup v1(中文翻译)(3):CPU Accounting Controller
英文原文: https://www.kernel.org/doc/html/latest/admin-guide/cgroup-v1/cpuacct.html CPU Accounting Contr ...
- python求最大公约数和最小公倍数
1 def gcd(x,y): 2 while(y): 3 t=x%y 4 x=y 5 y=t 6 #print("最小公倍数是:",x*y/x)#最小公倍数是两数之积除以最大公约 ...
- ForEach遍历集合、 集合容器
ForEach遍历集合 foreach循环是一种更加简洁的for循环,也称增强for循环,能用于遍历数组或集合中的元素. 格式: for(容器元素类型 临时变量:容器变量){ 执行语句} 从上面格式可 ...
- SAP OOALV- 合计
TYPES: BEGIN OF ty_mara, srno LIKE adrc-name1, " Storing the total text matnr LIKE mara-matnr, ...
- MySQL case when then 用法
下面演示一下MYSQL中的CASE WHEN THEN的用法. 一. SELECT MENU_NAME, YXBZ, case YXBZ when 'Y' then '开放' when 'N' the ...