# -*- 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. numpy, pandas, matplotlib等常用库的学习手册

    pandas介绍: 待续 参考资料: 中文:https://www.cnblogs.com/skying555/p/5914391.html 英文:http://www.datadependence. ...

  2. D盾 v2.0.6.42 测试记录

    0x01 前言 之前发了一篇博客<Bypass D盾_IIS防火墙SQL注入防御(多姿势)>,D哥第一时间联系我,对问题进行修复.这段时间与D哥聊了挺多关于D盾这款产品的话题,实在是很佩服 ...

  3. 【RF库Collections测试】Keep In Dictionary

    Name:Keep In DictionarySource:Collections <test library>Arguments:[ dictionary | *keys ]Keeps ...

  4. SpringBoot 常见问题记录

    问题一 Error starting ApplicationContext. To display the auto-configuration report re-run your applicat ...

  5. Linux 集群架构

    集群介绍 Keepalived 配置高可用集群

  6. Linux eject 命令

    eject命令允许在软件控制下弹出可移动媒体(通常是CD-ROM .软盘 .USB等) [root@localhost ~]# eject cdrom //弹出名字为cdrom的设备或者挂载点 [ro ...

  7. Zabbix的自定义键值和自动发现功能监控Oracle数据库的表空间

    前面介绍了利用Orabbix监控了,参考zabbix通过Orabbix监控oracle数据库,这里我们原先的模板中进行了修改,使用自动发现功能实现监控tablespace的使用情况. 1. 在被监控的 ...

  8. 顶点纹理shader

    Shader "Custom/VertDisplace" { Properties { _MainTex ("Base (RGB)", 2D) = " ...

  9. IOS 通过脚本自动打包工具 webfrogs/xcode_shell

    博文转载至 http://www.2cto.com/kf/201506/408346.html ios 开发通过xcode 打包其实效率不是太高,所以就有人,用shell 写了一个,自动打包,发邮件, ...

  10. Swift - 3.0 去掉 C 风格循环

    Swift 3.0 版本去掉了沿用已久的 C 风格循环语法, 又是向现代开发语言的一次迈进, 咱们就来看看没了 C 风格循环我们还有什么选择 C 风格循环 关于 C 风格循环, 不我们过多介绍了, 就 ...