博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
python装饰器入门
阅读量:6719 次
发布时间:2019-06-25

本文共 2543 字,大约阅读时间需要 8 分钟。

按别人的教程弄的。

要清楚基于类和基于函数的实现的不同之处。

#!/usr/bin/env python# -*- coding: utf-8 -*-'''class entryExit(object):    def __init__(self, f):        self.f = f    def __call__(self):        print "Enering", self.f.__name__        self.f()        print "Exited", self.f.__name__def entryExit(f):    def new_f():        print "Entering", f.__name__        f()        print "Exited", f.__name__    return new_f@entryExitdef func1():    print "inside func1()"@entryExitdef func2():    print "inside func2()"func1()func2()class decoratorWithoutArguments(object):    def __init__(self, f):        print "Inside __init__()"        self.f = f    def __call__(self, *args):        print "Inside __call__()"        self.f(*args)        print "After self.f(*args)"@decoratorWithoutArgumentsdef sayHello(a1, a2, a3, a4):    print 'sayHello arguments:', a1, a2, a3, a4print "After decoration"print "Preparing to call sayHello()"sayHello("say", "hello", "argument", "list")print "After first sayHello() call"sayHello("a", "different", "set of", "arguments")print "After second sayHello() call"class decoratorWithArguments(object):    def __init__(self, arg1, arg2, arg3):        print "Inside __init__()"        self.arg1 = arg1        self.arg2 = arg2        self.arg3 = arg3    def __call__(self, f):        print "Inside __call__()"        def wrapped_f(*args):            print "Inside wrapped_f()"            print "Decorator arguments:", self.arg1, self.arg2, self.arg3            f(*args)            print "After f(*args)"        return wrapped_f@decoratorWithArguments("hello", "world", 42)def sayHello(a1, a2, a3, a4):    print 'sayHello arguments:', a1, a2, a3, a4print "After decoration"print "Preparing to call sayHello()"sayHello("say", "hello", "argument", "list")print "After first sayHello() call"sayHello("a", "different", "set of", "arguments")print "After second sayHello() call"'''def decoratorFunctionWithArguments(arg1, arg2, arg3):    def wrap(f):        print "Inside wrap()"        def wrapped_f(*args):            print "Inside wrapped_f()"            print "Decorator arguments:", arg1, arg2, arg3            f(*args)            print "After f(*args)"        return wrapped_f    return wrap@decoratorFunctionWithArguments("Hello", "world", 42)def sayHello(a1, a2, a3, a4):    print 'sayHello arguments:', a1, a2, a3, a4print "After decoration"print "Preparing to call sayHello()"sayHello("say", "hello", "argument", "list")print "After first sayHello() call"sayHello("a", "different", "set of", "arguments")print "After second sayHello() call"

转载地址:http://bgjmo.baihongyu.com/

你可能感兴趣的文章
深入浅出node(3) 异步I/O
查看>>
iOS,plist文件、pch文件,工程设置,小知识点
查看>>
CentOS7配置IP和网络问题排查
查看>>
java Multiple Contexts have a path of ""
查看>>
MVC2.0中的HtmlHelper大全
查看>>
《面向模式的软件体系结构3-资源管理模式》读书笔记(1)--- Lookup模式
查看>>
You must configure either the server or JDBC driver (via the serverTimezone configuration property
查看>>
扩展方法判断序列(或集合)是否包含元素
查看>>
Sql Server Profiler跟踪死锁
查看>>
使用反射操作私有(Private)方法和属性
查看>>
第二阶段团队冲刺站立会议10
查看>>
php 的rabbitmq 扩展模块amqp安装
查看>>
APK签名校验绕过
查看>>
[LeetCode] 4Sum
查看>>
让最新官方编译的 ffmpeg 在 XP 上 跑起来
查看>>
庆祝博客开通
查看>>
地址栏中传递中文参数
查看>>
设计模式之结构型模式
查看>>
冒泡,快排
查看>>
git: fatal: Could not read from remote repository
查看>>