当前位置: 首页 > news >正文

如何查看网站是否开启gzip怎么制作图片二维码

如何查看网站是否开启gzip,怎么制作图片二维码,班服定制网站,wordpress建立个人网站Python Decorators II: Decorator ArgumentsOctober 19, 2008(本文是(Python3之模式和用法)一书的章节节选第二部分#xff0c;点击阅读第一部分)回顾#xff1a;不含参数的decorators在前文中#xff0c;我介绍了如何使用不含参数的decorators#xff0c;并使用类来实现。…Python Decorators II: Decorator ArgumentsOctober 19, 2008(本文是(Python3之模式和用法)一书的章节节选第二部分点击阅读第一部分)回顾不含参数的decorators在前文中我介绍了如何使用不含参数的decorators并使用类来实现。因为我发现这样做更容易接受。如果创建了一个无参decorator被decorated的函数被传至构造器每次调用decorated函数时就会调用__call__()方法class decoratorWithoutArguments(object):def __init__(self, f):If there are no decorator arguments, the functionto be decorated is passed to the constructor.print Inside __init__()self.f fdef __call__(self, *args):The __call__ method is not called until thedecorated function is called.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 decorationprint Preparing to call sayHello()sayHello(say, hello, argument, list)print After first sayHello() callsayHello(a, different, set of, arguments)print After second sayHello() calldecorated函数的所有参数会被传至__call__()。输出结果是Inside __init__()After decorationPreparing to call sayHello()Inside __call__()sayHello arguments: say hello argument listAfter self.f(*args)After first sayHello() callInside __call__()sayHello arguments: a different set of argumentsAfter self.f(*args)After second sayHello() call注意__init__()是唯一一个被调用执行decoration的方法每次调用decorated的sayHello()时就会调用__call__()。含有参数的decorators现在让我们来修改上面的代码看看向decorator加入参数后结果是什么。class decoratorWithArguments(object):def __init__(self, arg1, arg2, arg3):If there are decorator arguments, the functionto be decorated is not passed to the constructor!print Inside __init__()self.arg1 arg1self.arg2 arg2self.arg3 arg3def __call__(self, f):If there are decorator arguments, __call__() is only calledonce, as part of the decoration process! You can only giveit a single argument, which is the function object.print Inside __call__()def wrapped_f(*args):print Inside wrapped_f()print Decorator arguments:, self.arg1, self.arg2, self.arg3f(*args)print After f(*args)return wrapped_fdecoratorWithArguments(hello, world, 42)def sayHello(a1, a2, a3, a4):print sayHello arguments:, a1, a2, a3, a4print After decorationprint Preparing to call sayHello()sayHello(say, hello, argument, list)print after first sayHello() callsayHello(a, different, set of, arguments)print after second sayHello() call从输出结果可以看到加入参数使程序执行发生了很大变化。Inside __init__()Inside __call__()After decorationPreparing to call sayHello()Inside wrapped_f()Decorator arguments: hello world 42sayHello arguments: say hello argument listAfter f(*args)after first sayHello() callInside wrapped_f()Decorator arguments: hello world 42sayHello arguments: a different set of argumentsAfter f(*args)after second sayHello() call现在decoration方法调用构造器然后就马上调用__call__()后者只能包含一个参数(函数对象)且返回替代原有函数的decorated函数对象。注意当前decoration期间__call__()仅被调用一次此后从__call__()返回的decorated函数就可以在实际调用中使用了。虽然这种机制有一定合理性—构造器在这里可获取decorator参数但__call__()对象不能再作为decorated函数使用了。因此你必须使用__call__()执行decoration—可能第一次遇到这种与无参情况截然不同的方式你会比较吃惊何况还必须编写和无参decorator完成不同的代码。含decorator参数的decorator函数最后让我们看一个更复杂一点的decorator函数实现它需要你处理所有细节def decoratorFunctionWithArguments(arg1, arg2, arg3):def wrap(f):print Inside wrap()def wrapped_f(*args):print Inside wrapped_f()print Decorator arguments:, arg1, arg2, arg3f(*args)print After f(*args)return wrapped_freturn wrapdecoratorFunctionWithArguments(hello, world, 42)def sayHello(a1, a2, a3, a4):print sayHello arguments:, a1, a2, a3, a4print After decorationprint Preparing to call sayHello()sayHello(say, hello, argument, list)print after first sayHello() callsayHello(a, different, set of, arguments)print after second sayHello() call输出结果为Inside wrap()After decorationPreparing to call sayHello()Inside wrapped_f()Decorator arguments: hello world 42sayHello arguments: say hello argument listAfter f(*args)after first sayHello() callInside wrapped_f()Decorator arguments: hello world 42sayHello arguments: a different set of argumentsAfter f(*args)after second sayHello() calldecorator函数的返回值必须是一个封装待decorated函数的函数。也就是说Python会保存返回函数然后在decoration期间调用并传递待decorated函数。这也是为何有三层函数的原因里面那个函数才是被替换的。由于闭包wrapped_f()有权访问decorator参数arg1, arg2和arg3而无需像在class版本中那样显式存储它们。然而我也是在这里发现了“显胜于隐(explicit is better than implicit)”。即使该函数版本看起来要更加简洁紧凑但我发现还是类版本容易理解当然也就容易修改和维护。下一节内容在下一节中我会给出decorators的一些实例—基于Python开发的build system—然后在最后一节讨论类decorators。
http://www.yutouwan.com/news/65986/

相关文章:

  • linux网站如何做ip解析深圳网络推广网站推广
  • 民治网站设计圳网站建设公司精准客源app
  • 建站模板招募设计师广州网站建设公司哪家服务好
  • 合肥网站制作公司开鲁网站seo免费版
  • 不知此网站枉做男人的网站合肥公共资源交易中心
  • 台州网站建设公司威海外贸建站
  • c 转网站开发wordpress login 图标
  • 关于集团网站建设请示360地图下载最新版
  • 网站做镜像是什么泰安钢管网站建设
  • 网站开发毕业设计说明花钱也可以哪些网站可以做推广广告
  • 深圳做网站收费品牌网站建设荐选蝌蚪
  • 网站开发工具最适合网站建设制作介绍河南
  • 自助建站系统网站建设系统网站建设网站建设wordpress主题列表封面
  • 百度推广智能网站wordpress码农主题
  • 手机网站设计与规划郑州seo优化外包顾问
  • 网站建设多少钱印刷企业网站模板
  • 合肥网站建设 乐云seo国外网站需要备案吗
  • 模特公司网站模板珠海建网站设计
  • 内蒙古高等级公路建设开发有限责任公司网站wordpress默认账号密码忘记
  • 济南外贸网站建设公司排名wordpress 网页制作
  • 网站空间购买注意事项代理网址域名
  • 沧州网站建设设计定制自助网站建设平台
  • 客户网站建设问题销售型网站建设
  • 设计接单兼职网站搜狗推广停止了吗
  • 网站地区分站系统网络媒体广告代理
  • 邯郸市网站建设多少钱wordpress.备份
  • 优化图片传网站常州网站建设流程
  • 免费企业网站建设流程wordpress淘宝发货插件
  • 网站开发客户需求域名网站购买
  • 做网站反应快的笔记本有哪些国内最新军事新闻