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

均安建网站济南网站优化公司哪家好

均安建网站,济南网站优化公司哪家好,做结构图用什么网站,seo的优化策略有哪些Python装饰器#xff0c;分两部分#xff0c;一是装饰器本身的定义#xff0c;一是被装饰器对象的定义。 一、函数式装饰器#xff1a;装饰器本身是一个函数。 1.装饰函数#xff1a;被装饰对象是一个函数 [1]装饰器无参数#xff1a; a.被装饰对象无参数#xff1a; 分两部分一是装饰器本身的定义一是被装饰器对象的定义。 一、函数式装饰器装饰器本身是一个函数。 1.装饰函数被装饰对象是一个函数 [1]装饰器无参数 a.被装饰对象无参数def test(func): def _test(): print Call the function %s().%func.func_name return func() return _testtest def say():return hello worldsay() Call the function say(). hello worldb.被装饰对象有参数def test(func): def _test(*args,**kw): print Call the function %s().%func.func_name return func(*args,**kw) return _testtest def left(Str,Len): #The parameters of _test can be (Str,Len) in this case. return Str[:Len]left(hello world,5) Call the function left(). hello[2]装饰器有参数 a.被装饰对象无参数def test(printResultFalse): def _test(func): def __test(): print Call the function %s().%func.func_name if printResult: print func() else: return func() return __test return _testtest(True) def say():return hello worldsay() Call the function say(). hello worldtest(False) def say():return hello worldsay() Call the function say(). hello worldtest() def say():return hello worldsay() Call the function say(). hello worldtest def say():return hello worldsay() Traceback (most recent call last): File , line 1, in say() TypeError: _test() takes exactly 1 argument (0 given)由上面这段代码中的最后两个例子可知当装饰器有参数时即使你启用装饰器的默认参数不另外传递新值进去也必须有一对括号否则编译器会直接将func传递给test()而不是传递给_test() b.被装饰对象有参数def test(printResultFalse): def _test(func): def __test(*args,**kw): print Call the function %s().%func.func_name if printResult: print func(*args,**kw) else: return func(*args,**kw) return __test return _testtest() def left(Str,Len): #The parameters of __test can be (Str,Len) in this case. return Str[:Len]left(hello world,5) Call the function left(). hellotest(True) def left(Str,Len): #The parameters of __test can be (Str,Len) in this case. return Str[:Len]left(hello world,5) Call the function left(). hello2.装饰类被装饰的对象是一个类 [1]装饰器无参数 a.被装饰对象无参数def test(cls): def _test(): clsNamere.findall((\w),repr(cls))[-1] print Call %s.__init().%clsName return cls() return _testtest class sy(object): value32ssy() Call sy.__init().s __main__.sy object at 0x0000000002C3E390s.value 32b.被装饰对象有参数def test(cls): def _test(*args,**kw): clsNamere.findall((\w),repr(cls))[-1] print Call %s.__init().%clsName return cls(*args,**kw) return _testtest class sy(object): def __init__(self,value): #The parameters of _test can be (value) in this case. self.valuevaluessy(hello world) Call sy.__init().s __main__.sy object at 0x0000000003AF7748s.value hello world[2]装饰器有参数 a.被装饰对象无参数def test(printValueTrue): def _test(cls): def __test(): clsNamere.findall((\w),repr(cls))[-1] print Call %s.__init().%clsName objcls() if printValue: print value %r%obj.value return obj return __test return _testtest() class sy(object): def __init__(self): self.value32ssy() Call sy.__init(). value 32test(False) class sy(object): def __init__(self): self.value32ssy() Call sy.__init().b.被装饰对象有参数def test(printValueTrue): def _test(cls): def __test(*args,**kw): clsNamere.findall((\w),repr(cls))[-1] print Call %s.__init().%clsName objcls(*args,**kw) if printValue: print value %r%obj.value return obj return __test return _testtest() class sy(object): def __init__(self,value): self.valuevaluessy(hello world) Call sy.__init(). value hello worldtest(False) class sy(object): def __init__(self,value): self.valuevaluessy(hello world) Call sy.__init().二、类式装饰器装饰器本身是一个类借用__init__()和__call__()来实现职能 1.装饰函数被装饰对象是一个函数 [1]装饰器无参数 a.被装饰对象无参数class test(object): def __init__(self,func): self._funcfunc def __call__(self): return self._func()test def say(): return hello worldsay() hello worldb.被装饰对象有参数class test(object): def __init__(self,func): self._funcfunc def __call__(self,*args,**kw): return self._func(*args,**kw)test def left(Str,Len): #The parameters of __call__ can be (self,Str,Len) in this case. return Str[:Len]left(hello world,5) hello[2]装饰器有参数 a.被装饰对象无参数class test(object): def __init__(self,beforeinfoCall function): self.beforeInfobeforeinfo def __call__(self,func): def _call(): print self.beforeInfo return func() return _calltest() def say(): return hello worldsay() Call function hello world或者class test(object): def __init__(self,beforeinfoCall function): self.beforeInfobeforeinfo def __call__(self,func): self._funcfunc return self._call def _call(self): print self.beforeInfo return self._func()test() def say(): return hello worldsay() Call function hello worldb.被装饰对象有参数class test(object): def __init__(self,beforeinfoCall function): self.beforeInfobeforeinfo def __call__(self,func): def _call(*args,**kw): print self.beforeInfo return func(*args,**kw) return _calltest() def left(Str,Len): #The parameters of _call can be (Str,Len) in this case. return Str[:Len]left(hello world,5) Call function hello或者class test(object): def __init__(self,beforeinfoCall function): self.beforeInfobeforeinfo def __call__(self,func): self._funcfunc return self._call def _call(self,*args,**kw): print self.beforeInfo return self._func(*args,**kw)test() def left(Str,Len): #The parameters of _call can be (self,Str,Len) in this case. return Str[:Len]left(hello world,5) Call function hello2.装饰类被装饰对象是一个类 [1]装饰器无参数 a.被装饰对象无参数class test(object): def __init__(self,cls): self._clscls def __call__(self): return self._cls()test class sy(object): def __init__(self): self.value32ssy()s __main__.sy object at 0x0000000003AAFA20s.value 32b.被装饰对象有参数class test(object): def __init__(self,cls): self._clscls def __call__(self,*args,**kw): return self._cls(*args,**kw)test class sy(object): def __init__(self,value): #The parameters of __call__ can be (self,value) in this case. self.valuevaluessy(hello world)s __main__.sy object at 0x0000000003AAFA20s.value hello world[2]装饰器有参数 a.被装饰对象无参数class test(object): def __init__(self,printValueFalse): self._printValueprintValue def __call__(self,cls): def _call(): objcls() if self._printValue: print value %r%obj.value return obj return _calltest(True) class sy(object): def __init__(self): self.value32ssy() value 32s __main__.sy object at 0x0000000003AB50B8s.value 32b.被装饰对象有参数class test(object): def __init__(self,printValueFalse): self._printValueprintValue def __call__(self,cls): def _call(*args,**kw): objcls(*args,**kw) if self._printValue: print value %r%obj.value return obj return _calltest(True) class sy(object): def __init__(self,value): #The parameters of _call can be (value) in this case. self.valuevaluessy(hello world) value hello worlds __main__.sy object at 0x0000000003AB5588s.value hello world总结【1】decorator后面不带括号时也即装饰器无参数时效果就相当于先定义func或cls而后执行赋值操作funcdecorator(func)或clsdecorator(cls) 【2】decorator后面带括号时也即装饰器有参数时效果就相当于先定义func或cls而后执行赋值操作 funcdecorator(decoratorArgs)(func)或clsdecorator(decoratorArgs)(cls) 【3】如上将func或cls重新赋值后此时的func或cls也不再是原来定义时的func或cls而是一个可执行体你只需要传入参数就可调用func(args)返回值或者输出cls(args)object of cls 【4】最后通过赋值返回的执行体是多样的可以是闭包也可以是外部函数当被装饰的是一个类时还可以是类内部方法函数 【5】另外要想真正了解装饰器一定要了解func.func_code.co_varnames,func.func_defaults通过它们你可以以func的定义之外还原func的参数列表另外关键字参数是因为调用而出现的而不是因为func的定义func的定义中的用等号连接的只是有默认值的参数它们并不一定会成为关键字参数因为你仍然可以按照位置来传递它们。
http://wiki.neutronadmin.com/news/12352/

相关文章:

  • 建设网站第一步宁远做网站
  • 做网站商城需要什么软件博客网站建设设计报告
  • 网站制作属于什么专业门户网站兴化建设局 金
  • 网站开发语言为 php帮朋友免费做网站
  • 湖北皇奥建设工程有限公司网站新浪微博网页版
  • 企业网站建设需要考虑内容爱站网新网址是多少
  • 济南外贸网站制作深圳广告设计公司网站
  • 网站建设落地页北京住建个人证书查询网
  • 营销型网站跟云网站阿里巴巴国际站做2个网站有用
  • 青岛网站建设团队电脑优化软件推荐
  • 广州网站建设比较好的公司在哪可以找到做网站的
  • 网站建设与优化推广方案手机做任务网站
  • 中国建设劳动协会网站定制型网站建设平台
  • php网站开发图文教程小程序登录跳转
  • php做网站最容易网站没被收录怎么办
  • 深圳上市公司全部名单杭州seo网站
  • 哪些网站论坛做推广好河南网站建设yipinpai
  • 网站建设方案书生鲜重庆建设传动科技有限公司
  • 哈尔滨手机网站建设价格邢台seo技术
  • 科技风格设计网站中山做网站公司
  • qingdao城乡住房建设厅网站大连模板网站制作电话
  • 建立个人网站怎么赚钱cms系统复杂权限
  • 网站图片设计怎样才能高大上辽阳网站建设公司
  • 郑州个人网站建设装修全包
  • 想自己做网站需要会什么品牌整合营销传播方案
  • 网站建设方维企业网站源码asp
  • 有网站模板怎么做网站网站美工做确认取消对话框
  • 怎么做好网站狠抓措施落实
  • 怎么把自己做的网站放到网上网站移动端指的是什么
  • 做番号网站犯法吗百度网址大全官网下载