# -*- 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. Tomcat------启动时报错:Failed to start component [StandardEngine[Catalina].StandardHost[localhost].

    启动报错信息: Failed to start component [StandardEngine[Catalina].StandardHost[localhost] 因此出现这种错误的原因可能有: ...

  2. SpringMVC由浅入深day01_13springmvc和struts2的区别_14问题

    13 springmvc和struts2的区别 1.springmvc是基于方法开发(一个url对应一个方法),请求参数传递到方法的形参,可以设计为单例或多例(建议单例),struts2是基于类开发, ...

  3. UITextField in a UITableViewCell

    http://stackoverflow.com/questions/409259/having-a-uitextfield-in-a-uitableviewcell http://stackover ...

  4. 利用BurpSuite实现半自动化盲注

    为了方便演示,这里直接使用字符,构造SQL Payload: ?id= and substring(user(),,)='r' 1.使用Burp抓包,发送到Intruder,设置模式和变量 2.设置字 ...

  5. Android--Led_Demo_APK控制LED灯

    下面代码主要实现接口定义,实现从.so库文件接口函数在JAVA里面的声明:package com.friendlyarm.AndroidSDK; import android.util.Log; pu ...

  6. error:please select android sdk

    发现问题所在就是 在model iml文件中: 把<orderEntry type="inheritedJdk" /> 改成 <orderEntry type=& ...

  7. this关键字制定对象的属性

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  8. html/php/mysql乱码

    1. html通过jquery的ajax传递数据给php. encodeURI("my.php?name=博客园") 2. php接受 urldecode($_GET[" ...

  9. Mac 下如何下载、启动和关闭Tomcat 和管理Mac自带的Apache

     Mac 下载.启动和关闭Tomcat 1. 下载Tomcat(地址:tomcat.apache.org),选择适合的版本(这里选择6.0.48),点击“Download”,之后在新页面点击“Core ...

  10. No.4 PyQt学习(页面跳转)

    先定义了两个MainWindow进行跳转,但发现这样的话,从第二个Window无法跳转会第一个.代码如下: # -*- coding: utf-8 -*- import sys from PyQt4. ...