• linkedu视频
  • 平面设计
  • 电脑入门
  • 操作系统
  • 办公应用
  • 电脑硬件
  • 动画设计
  • 3D设计
  • 网页设计
  • CAD设计
  • 影音处理
  • 数据库
  • 程序设计
  • 认证考试
  • 信息管理
  • 信息安全
菜单
linkedu.com
  • 网页制作
  • 数据库
  • 程序设计
  • 操作系统
  • CMS教程
  • 游戏攻略
  • 脚本语言
  • 平面设计
  • 软件教程
  • 网络安全
  • 电脑知识
  • 服务器
  • 视频教程
  • vbs
  • DOS/BAT
  • hta/htc
  • python
  • perl
  • VBA
  • ColdFusion
  • ruby
  • PowerShell
  • Lua
  • Golang
  • linux shell
您的位置:首页 > 脚本语言 >python > Python装饰器使用示例及实际应用例子

Python装饰器使用示例及实际应用例子

作者:junjie 字体:[增加 减小] 来源:互联网

junjie 通过本文主要向大家介绍了python示例程序,python简单代码示例,python代码示例,python示例,python爬虫示例等相关知识,希望对您有所帮助,也希望大家支持linkedu.com www.linkedu.com

测试1

deco运行,但myfunc并没有运行

def deco(func):
    print 'before func'
    return func

def myfunc():
    print 'myfunc() called'
 
myfunc = deco(myfunc)
</div>

测试2

需要的deco中调用myfunc,这样才可以执行
def deco(func):
    print 'before func'
    func()
    print 'after func'
    return func

def myfunc():
    print 'myfunc() called'
 
myfunc = deco(myfunc)
</div>

测试3

@函数名 但是它执行了两次

def deco(func):
    print 'before func'
    func()
    print 'after func'
    return func

@deco
def myfunc():
    print 'myfunc() called'

myfunc()
</div>

测试4

这样装饰才行

def deco(func):
    def _deco():
        print 'before func'
        func()
        print 'after func'
    return _deco

@deco
def myfunc():
    print 'myfunc() called'
 
myfunc()
</div>

测试5

@带参数,使用嵌套的方法
def deco(arg):
    def _deco(func):
        print arg
        def __deco():
            print 'before func'
            func()
            print 'after func'
        return __deco
    return _deco

@deco('deco')
def myfunc():
    print 'myfunc() called'
 
myfunc()
</div>

测试6

函数参数传递
def deco(arg):
    def _deco(func):
        print arg
        def __deco(str):
            print 'before func'
            func(str)
            print 'after func'
        return __deco
    return _deco

@deco('deco')
def myfunc(str):
    print 'myfunc() called ', str
 
myfunc('hello')
</div>

测试7

未知参数个数
def deco(arg):
    def _deco(func):
        print arg
        def __deco(*args, **kwargs):
            print 'before func'
            func(*args, **kwargs)
            print 'after func'
        return __deco
    return _deco

@deco('deco1')
def myfunc1(str):
    print 'myfunc1() called ', str

@deco('deco2')
def myfunc2(str1,str2):
    print 'myfunc2() called ', str1, str2
 
myfunc1('hello')
 
myfunc2('hello', 'world')
</div>

测试8

class作为修饰器
class myDecorator(object):
 
    def __init__(self, fn):
        print "inside myDecorator.__init__()"
        self.fn = fn
 
    def __call__(self):
        self.fn()
        print "inside myDecorator.__call__()"
 
@myDecorator
def aFunction():
    print "inside aFunction()"
 
print "Finished decorating aFunction()"
 
aFunction()
</div>

测试9
class myDecorator(object):
 
    def __init__(self, str):
        print "inside myDecorator.__init__()"
        self.str = str
        print self.str
 
    def __call__(self, fn):
        def wrapped(*args, **kwargs):

您可能想查找下面的文章:

  • Python编程生成随机用户名及密码的方法示例
  • Python判断变量是否为Json格式的字符串示例
  • python实现逻辑回归的方法示例
  • python分割列表(list)的方法示例
  • Python编程生成随机用户名及密码的方法示例
  • Python判断变量是否为Json格式的字符串示例
  • python实现逻辑回归的方法示例
  • Python实现对字符串的加密解密方法示例
  • Python列表切片用法示例
  • python利用拉链法实现字典方法示例

相关文章

  • Python中常见的数据类型小结
  • Python线程指南详细介绍
  • Python压缩和解压缩zip文件
  • 讲解Python中if语句的嵌套用法
  • python学习数据结构实例代码
  • 详解Python中的相对导入和绝对导入
  • python简单猜数游戏实例
  • Windows和Linux下Python输出彩色文字的方法教程
  • Python功能键的读取方法
  • 浅谈python字典多键值及重复键值的使用

文章分类

  • vbs
  • DOS/BAT
  • hta/htc
  • python
  • perl
  • VBA
  • ColdFusion
  • ruby
  • PowerShell
  • Lua
  • Golang
  • linux shell

最近更新的内容

    • python生成随机mac地址的方法
    • bpython 功能强大的Python shell
    • centos 下面安装python2.7 +pip +mysqld
    • 详解Python的Django框架中的Cookie相关处理
    • Python中设置变量访问权限的方法
    • python使用正则表达式匹配字符串开头并打印示例
    • 在Python中操作时间之mktime()方法的使用教程
    • python将图片文件转换成base64编码的方法
    • 在Python中使用cookielib和urllib2配合PyQuery抓取网页信息
    • python中使用urllib2伪造HTTP报头的2个方法

关于我们 - 联系我们 - 免责声明 - 网站地图

©2020-2025 All Rights Reserved. linkedu.com 版权所有