# -*- 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完美实现(原创)的更多相关文章

  1. bootstrap菜单完美解决---原创

    由于bootstrap的各方优点,偶的“点金项目细化分包管理平台”决定采用它.但在使用中遇到了一些问题,比如菜单的问题,这个菜单是用的一个JQuery的一个效果,点击后,所点击的链接处的class要加 ...

  2. Decorator:从原理到实践

    前言 原文链接:Nealyang/personalBlog ES6 已经不必在过多介绍,在 ES6 之前,装饰器可能并没有那么重要,因为你只需要加一层 wrapper 就好了,但是现在,由于语法糖 c ...

  3. 6 小时 Python 入门

    6 小时 Python 入门 以下操作均在 Windows 环境下进行操作,先说明一下哈 一.安装 Python 1.官网下载 Python 进入官网(https://www.python.org), ...

  4. RealProxy深入

    Program.cs class Program { static void Main(string[] args) { NoMethodLogging(); Console.WriteLine(&q ...

  5. python基础——装饰器

    python基础——装饰器 由于函数也是一个对象,而且函数对象可以被赋值给变量,所以,通过变量也能调用该函数. >>> def now(): ... print('2015-3-25 ...

  6. Python3 函数式编程

    函数式编程就是一种抽象程度很高的编程范式,纯粹的函数式编程语言编写的函数没有变量,因此,任意一个函数,只要输入是确定的,输出就是确定的,这种纯函数我们称之为没有副作用.而允许使用变量的程序设计语言,由 ...

  7. [置顶] IOS7状态栏StatusBar官方标准适配方法

    IOS7状态栏StatusBar官方标准适配方法 hello,大家好,ios7正式版已经发布,相信大家都在以各种方式来适配ios7. 如果你已经下载了xcode5,正准备使用,你会发现各种布局的改变. ...

  8. Python 3 学习笔记2

    教程链接:http://www.liaoxuefeng.com/wiki/0014316089557264a6b348958f449949df42a6d3a2e542c000 高级特性 切片 取一个l ...

  9. python多版本的pip共存问题解决办法

    python pip 多版本 问题情景 最开始学python的时候用的是py2,且一直用pip来安装库函数.后来py3出来了,所以就装上了,但是一装上出问题了,主要有两个主要的问题.下面将详细说明. ...

随机推荐

  1. VSCode------搭建.net core 2.0,并配置到IIS服务器

    前奏 安装VSCode最新版: https://code.visualstudio.com/ 安装window server hosting,发布和部署到IIS使用: https://www.micr ...

  2. 奔五的人学ios:swift竟然没有字符串包括,找个简单的解决方法

    swift关于字符串的推断中 有前导.有后缀 两个方法.竟然没有包括推断. 经过学习找了个简单的解决方法: extension String { func has(v:String)->Bool ...

  3. Go之go与channel组合使用

    1,等待一个事件 <-ch 将一直阻塞,直到ch被关闭 或者 ch中可以取出值 为止 所以到第17行之后会去执行go后面的func()匿名函数,在里面给ch赋值后(或者close(ch))后,才 ...

  4. jquery 复制文本到剪切板插件(非 flash)

    原创插件,转载请声明出处!!! jquery.copy.js 内容如下: /*! * jQuery Copy Plugin * version: 1.0.0-2018.01.23 * Requires ...

  5. 最短路径——Dijkstra算法和Floyd算法

    Dijkstra算法概述 Dijkstra算法是由荷兰计算机科学家狄克斯特拉(Dijkstra)于1959 年提出的,因此又叫狄克斯特拉算法.是从一个顶点到其余各顶点的最短路径算法,解决的是有向图(无 ...

  6. 在taro中跳转页面的时候执行两遍componentDidMount周期的原因和解决方法

    在做taro跳转的时候,发现在跳转后的页面会走两遍componentDidMount周期,查看了github上的issues,发现是跳转路由带参为中文引起的,只要把中文参数进行urlencode解决 ...

  7. 关于控制台程序下使用mfc库中的函数时断言

    例如: TCHAR path[8192]; int len = getmodulefilename(afxgetinstancehandle(),path,8192);//会出现断言 如果没有选择支持 ...

  8. WebService连接sql serever并使用Android端访问数据

    一.下载sql serever(真真难下) 建立数据库 二.创建WebService VS2015中新建项目,进行连接调试 1. 服务资源管理文件->数据连接->新建连接 2. 继续-&g ...

  9. 【API设计】RESTful API 设计指南

    RESTful API URL定位资源,用HTTP动词(GET,POST,DELETE,DETC)描述操作. 例如 . REST描述的是在网络中client和server的一种交互形式:REST本身不 ...

  10. Android开发进阶从小工到专家之性能优化