@log的decorator完美实现(原创)
# -*- coding: utf-8 -*- from functools import wraps
from inspect import isfunction def beforecalled(*args, **kwargs):
print('before called! {}'.format(len(args))) for arg in args:
print(arg) def aftercalled(*args, **kwargs):
print('after called! {}'.format(len(args)))
for k,v in kwargs.iteritems():
print('{0}={1}'.format(k,v)) jclist=['-' for _ in xrange(20)]
print(''.join(jclist)) def logdeco(*decoargs, **decokwargs):
def decotator(func):
@wraps(func)
def wrapper(*funcargs, **funckwargs):
beforecalled(*decoargs, **decokwargs)
result = func(*funcargs, **funckwargs)
aftercalled(*decoargs, **decokwargs)
return result
return wrapper
return decotator def log(*decoargs, **decokwargs):
if len(decoargs)==1 and len(decokwargs)==0:
if isfunction(decoargs[0]) or hasattr(decoargs[0],'__call__'):
return logdeco()(decoargs[0]) def wrappered(func):
return logdeco(*decoargs, **decokwargs)(func) return wrappered @log('lisa',50,email='lisa@xxx.com')
def testmoreparas(x,y):
print('x*y={}'.format(x*y)) @log()
def testemptypara(x,y):
print('x*y={}'.format(x*y)) @log
def testjustlog(x,y):
print('x*y={}'.format(x*y)) if __name__=='__main__':
testmoreparas(5,6)
testemptypara(4,5)
testjustlog(3,4)
Python 2.7.15 (v2.7.15:ca079a3ea3, Apr 30 2018, 16:22:17) [MSC v.1500 32 bit (Intel)] on win32
Type "copyright", "credits" or "license()" for more information.
>>>
============ RESTART: D:\pytest\logdecorator.py ============
before called! 2
lisa
50
x*y=30
after called! 2
email=lisa@xxx.com
--------------------
before called! 0
x*y=20
after called! 0
--------------------
before called! 0
x*y=12
after called! 0
--------------------
>>>
test result
@log的decorator完美实现(原创)的更多相关文章
- bootstrap菜单完美解决---原创
由于bootstrap的各方优点,偶的“点金项目细化分包管理平台”决定采用它.但在使用中遇到了一些问题,比如菜单的问题,这个菜单是用的一个JQuery的一个效果,点击后,所点击的链接处的class要加 ...
- Decorator:从原理到实践
前言 原文链接:Nealyang/personalBlog ES6 已经不必在过多介绍,在 ES6 之前,装饰器可能并没有那么重要,因为你只需要加一层 wrapper 就好了,但是现在,由于语法糖 c ...
- 6 小时 Python 入门
6 小时 Python 入门 以下操作均在 Windows 环境下进行操作,先说明一下哈 一.安装 Python 1.官网下载 Python 进入官网(https://www.python.org), ...
- RealProxy深入
Program.cs class Program { static void Main(string[] args) { NoMethodLogging(); Console.WriteLine(&q ...
- python基础——装饰器
python基础——装饰器 由于函数也是一个对象,而且函数对象可以被赋值给变量,所以,通过变量也能调用该函数. >>> def now(): ... print('2015-3-25 ...
- Python3 函数式编程
函数式编程就是一种抽象程度很高的编程范式,纯粹的函数式编程语言编写的函数没有变量,因此,任意一个函数,只要输入是确定的,输出就是确定的,这种纯函数我们称之为没有副作用.而允许使用变量的程序设计语言,由 ...
- [置顶] IOS7状态栏StatusBar官方标准适配方法
IOS7状态栏StatusBar官方标准适配方法 hello,大家好,ios7正式版已经发布,相信大家都在以各种方式来适配ios7. 如果你已经下载了xcode5,正准备使用,你会发现各种布局的改变. ...
- Python 3 学习笔记2
教程链接:http://www.liaoxuefeng.com/wiki/0014316089557264a6b348958f449949df42a6d3a2e542c000 高级特性 切片 取一个l ...
- python多版本的pip共存问题解决办法
python pip 多版本 问题情景 最开始学python的时候用的是py2,且一直用pip来安装库函数.后来py3出来了,所以就装上了,但是一装上出问题了,主要有两个主要的问题.下面将详细说明. ...
随机推荐
- SpringMVC由浅入深day02_7上传图片
7 上传图片 7.1 需求 在修改商品页面,添加上传商品图片功能. 7.2 springmvc中对多部件类型解析 在页面form中提交enctype="multipart/form-data ...
- Apache安全配置基线指导
搜索关键词:Apache安全配置基线指导 参考链接: windows服务器下Apache 的降权 https://www.landui.com/help/show-1749.html
- AngularJs HTML DOM、AngularJS 事件以及模块的学习(5)
今天的基础就到了操作DOM,事件和模块的学习,其实我个人感觉学习起来AngularJS并没有想象中的那么的艰难,可能是因为这个太基础化吧,但是我们从初学开始就应该更加的自信一些,后来我可能会写一个小的 ...
- STL——空间配置器(SGI-STL)
一. 空间配置器标准接口 参见<STL源码剖析>第二章-2.1.<memory>文件. 二.具备次配置力的SGI空间配置器 1. SGI STL的配置器与众不同,也与标准规范不 ...
- codeforces水题100道 第二十三题 Codeforces Beta Round #77 (Div. 2 Only) A. Football (strings)
题目链接:http://www.codeforces.com/problemset/problem/96/A题意:判断一个0-1字符串中出现的最长的0字串或者1字串的长度是否大于等于7.C++代码: ...
- Android逆向笔记之AndroidKiller与Android Studio的使用
https://blog.csdn.net/a_1054280044/article/details/60465267 版权声明:本文为博主原创文章,未经博主允许不得转载. https://blog. ...
- DELPHIER,正转向PYTHON了
用了10多年的DELPHI ,现在准备学PYTHON了, 理由是: 1)现在是云计算时代了,需要学习一门适应该时代的语言,研究了很久,PYTHON是最佳选择,排除了PHP,PERL,RUBY等,至于G ...
- Elasticsearch学习之head插件安装
通过elasticseach自带的plugin命令 elasticsearch/bin/plugin -install mobz/elasticsearch-head 如下图: 2. zip包安装 ...
- oracle索引优化
零.概述 在这之前,一直都是使用mysql来进行开发或者部署.最近及今后很长一段时间都要使用oracle,今天和同事也遇到一个oracle 慢查询问题.查了很多资料,这里记录备忘.持续更新ing... ...
- rc.sysinit 解析
$# :它可抓出 positional parameter 的數量,即脚本后面的参数有几个 $@和$*表示全部参数,但不包含脚本名,即$0,如果在command line上跑 my.sh p1 “p2 ...