首页
学习
活动
专区
圈层
工具
发布
    • 综合排序
    • 最热优先
    • 最新优先
    时间不限
  • 来自专栏软件研发

    Use the @Implementer class deco

    在Python3中,当我们使用旧式的类修饰符(class decorator)时,可能会遇到TypeError: Class advice impossible的错误。这个错误通常发生在尝试使用@classmethod和@staticmethod修饰符来装饰类方法或静态方法时。

    40331编辑于 2023-12-15
  • 来自专栏京东技术

    助力双 11 个性化会场高效交付:Deco 智能代码技术揭秘

    Tech 导读 在这次双11的个性化会场我们大规模使用Deco进行研发,带来了48%左右的效率提升,本文将为大家揭秘Deco提效之秘。 图4 设计稿数据表达 Deco 设计稿智能生成静态 代码主要做了两件事情: 从视觉稿中提取「结构化的数据描述」; 将「结构化的数据描述」表达成代码; 本质上,Deco 智能代码是通过设计工具插件从视觉稿原始信息中提取 9图23 数据定义 (3)异步数据请求 针对最常见的异步数据请求的场景,Deco 提供了一个可视化表单,仅需通过简单的配置,即可快速生成异步数据请求的代码。 图24 异步请求界面 (4)事件绑定 Deco 提供了包括点击事件在内的多种节点事件,以及组件的生命周期等事件的定义,用户可以在事件中编辑逻辑代码。 图25 事件绑定 (5)属性编辑及数据绑定 此外,Deco 已经提供组件映射的能力,在这基础上,开放了组件的属性编辑和数据绑定能力,实现页面与动态数据的对接。

    4.6K20发布于 2021-11-16
  • 来自专栏python3

    Python装饰器原理——偷梁换柱

    的内存地址:',deco) 11 print('最后test的内存地址:',test) 12 return deco 13 14 def test(): 15 time.sleep <locals>.deco at 0x0000000003C84048>,即demo=<function timer.<locals>.deco at 0x0000000003C84048> ? 19 test = timer(test)   调用timer函数,timer函数最后  12 return deco返回deco给test,此时test=demo=<function timer. 将deco函数体放于内存<function timer.<locals>.deco at 0x0000000003C63048>中,即deco=<function timer. <locals>.deco at 0x0000000003C63048>, ? timer函数最后将deco返回,即test=deco=<function timer.

    43120发布于 2020-01-15
  • 来自专栏python3

    python高级编程-Part3 修饰器

    简单的修饰器     一个函数接收函数对象作为参数,并且返回函数对象,这样的函数可以成为一个修饰器,形如下面的定义: def deco(func):     def _deco(*args):          修饰器定义完成后,使用@去修饰函数,如下面所示: @deco                  #实际相当于执行了f = deco(f) def f(x):     print x     经过上述处理后 ,查看f的函数名已经变成了“_deco” print f print deco(f) 运行结果是: <function _deco at 0x00000000022314A8> <function _deco (a, b):              # a,b是修饰器需要的参数     def deco(func):         def _deco(*args):         # *args是被包裹的函数需要的参数     return deco                   # 此处需闭包 @deco_args(1,2)                   # 相当于f = deco_args(1,2)(

    36410发布于 2020-01-07
  • 来自专栏简书专栏

    Python闭包函数和装饰器

    (func): print("deco1 decorated") def wrapper(*args, **kwargs): print("this is deco1") end here") return wrapper def deco2(func): print("deco2 decorated") def wrapper(*args, **kwargs): print("this is deco2") func(*args, **kwargs) print("deco2 end here (1) print("result is %d" % (a + b)) func(3, 4) 上面一段代码的运行结果如下: deco2 decorated deco1 decorated this is deco1 this is deco2 hello, here is func for add: result is 7 deco2 end here time is 1000.9815692901611

    69240发布于 2018-09-10
  • 来自专栏JetpropelledSnake

    Python入门之装饰器九步学习入门

    myfunc = deco(myfunc) myfunc() myfunc() 第三步:使用语法糖@来装饰函数 '''示例3: 使用语法糖@来装饰函数,相当于“myfunc = deco(myfunc ") # 不需要返回func,实际上应返回原函数的返回值 return _deco @deco def myfunc(): print(" myfunc() called 装饰函数名实际上应更有意义些''' def deco(arg): def _deco(func): def __deco(): print("before __name__, arg)) return __deco return _deco @deco("mymodule") def myfunc(): print(" return _deco @deco(locker) def myfunc(): print(" myfunc() called.")

    66580发布于 2018-05-03
  • 来自专栏MyPanda的学习笔记

    让python装饰器不再晦涩难懂

    我们可以定义一个新的函数 deco, 然后执行fn=deco(fn)这样的赋值操作,那么在原来fn 被调用的时候,其实是执行了 deco(fn)的操作. 如果这个方式可行,我们面临如下的问题: deco函数必须接受一个函数作为参数,同时deco函数也必须要返回一个函数. 在第一次deco扩展的基础上,我们需要传递额外的参数gift给deco, 很显然无法直接传递给deco,因为deco所接受的参数必须是fn, 所以我们用wrapper函数进行传递. 传递该参数给deco之后,我们修改了deco, 因为deco 要处理这个传入的参数,那么就必须要修改deco 的代码。至此完成了两次扩展. 个人更乐于: 从deco 函数写起,然后再做二次扩展(也就是为deco函数传递参数),这就是外围的wrapper函数,写完deco和wrapper函数,那么装饰器函数就完成了.

    40710发布于 2020-09-28
  • 来自专栏python3

    python实现redis分布式锁

    cls.rdcon.get(cls.lock_key): print "release lock" cls.rdcon.delete(cls.lock_key) def deco (cls): def _deco(func): def __deco(*args, **kwargs): print "before %s called return func(*args, **kwargs) finally: cls.release(cls) return __deco return _deco @deco(RedisLock("112233")) def myfunc(): print "myfunc() called."

    71910发布于 2020-01-08
  • 来自专栏猪圈子

    深入理解 Python 中的装饰器

    先简单看一下示例: from time import ctime def deco(func): def decorator(*args, **kwargs): print(' 这本质上相当于foo = deco(foo) 的嵌套调用。 这里面,你又遇到了 *args 和 **kwargs,它们可以组合接收任意函数参数。 __name__)) return func(*args, **kwargs) return decorator2 @deco2 @deco1 def foo(): print 在嵌套调用的过程中,foo = deco2(deco1(foo)),所以先返回 deco1(foo) 的函数名字即 decorator1, 后返回 foo 的函数名。 foo() bar() 让我们简单分析下这个装饰器,deco函数接受的是一个str对象tag,当执行deco('Python') 后返回的是decorator函数,此函数需要接受一个函数对象,同时返回

    1K10发布于 2019-11-19
  • 来自专栏运维杂记

    进阶的运维开发(一)- 装饰器

    = timeit(1) deco1 = deco(sleep) deco1(3) 4.0014448165893555 根据不带参数的装饰器的理解,这个函数也是很好理解的。 多个装饰器叠加 首先我们要查看多个迭代器的的执行过程 def deco_1(func): print('enter deco_1') def inner1(*args, **kwargs deco_2') def inner2(*args, **kwargs): print('enter deco_2_inner') print(func. __name__) return func(*args, **kwargs) return inner2 @deco_1 @deco_2 def Print(*args, ** kwargs): return args enter deco_2 enter deco_1 Print(1, 2) enter deco_1_inner inner2 enter deco_

    52870发布于 2019-12-11
  • 来自专栏python3

    python3--装饰器

    #返回deco的内存地址 @timer  # 给test1这个函数,添加一个装饰器,等同于 test1=timer(test1)=deco        # 注意timer的return是deco ,所以test1=timer(test1),就是调用timer函数,并把deco的内存地址赋值给test1        # 所以以后都会不在直接执行这个用def定义的最原始的test1()这个函数        # 程序后面的所调用的test1(),其实执行的是deco(),通过deco来执行test1里面的函数体        # 这里的deco()仅仅是timer()的一个嵌套函数,相当于 timer的局部变量是无法直接在全局引用的        # 但是timer这个高阶函数,返回了deco的内存地址,所以通过test1=timer(test1)之后,test1就等于deco的内存地址了 ,就可以直接使用test1()了            #此时这个test()其实就是deco(),而不再是最原始的定的那个def test() def test1():    time.sleep

    41620发布于 2020-01-03
  • 来自专栏python3

    python 装饰器案例解析

    time is 2.0000431537628174 注意: 执行的时候,不能写deco(test1()),为什么呢? 这样写,是把test1函数执行的结果,传给deco了。 deco函数,不需要执行结果,它需要一个函数即可。 Pycharm编辑器写代码的时候,有自动补全功能,切记这里,要把括号删掉才行。 (test1) test2 = deco(test2) test1() test2() 执行报错 TypeError: 'NoneType' object is not callable 因为deco def timer():     def deco():         pass 能不能把嵌套函数的形式融入到deco函数中呢? 把deco函数的代码直接拷贝进来,最后return deco 一个函数只有一个return,把中间的return修改为func() 将func参数移动到函数最上层 def timer(func):     

    42010发布于 2018-08-02
  • 来自专栏bit哲学院

    理解Python 装饰器

    In[13]: def deco(var):    ...:     print var    ...:     def _deco(func):    ...:         def wrapper (func):   ...:  def wrapper():   ...:      print 'deco1'   ...:      func()   ...:      print 'deco1'   return wrapper   ...:  In[6]:  In[6]: @deco1   ...: @deco2   ...: @deco3   ...: def foo():   ...:  print 'foo'   ...:   In[7]: foo() Out[8]:         deco1         deco2         deco3         foo         deco3         deco2         deco1 # 执行顺序从里到外,先调用最里层的装饰器,依次往外层调用装饰器 deco3→deco2→deco1  PS:一般为了保留被装饰函数的元信息

    43200发布于 2020-12-19
  • 来自专栏计算机视觉理论及其实现

    python类的装饰器

    def deco(obj): obj.x = 1 obj.y = 2 return obj@deco # Foo = deco(Foo)class Foo: passprint def deco(**kwargs): def wrapper(obj): for k, v in kwargs.items(): setattr(obj, k , v) return obj return wrapper@deco(x=1, y=2)class Foo: passprint(Foo. def deco(obj): obj.x = 1 obj.y = 2 return obj@deco # Foo = deco(Foo)class Foo: passprint k, v) return obj return wrapper@deco(x=1, y=2)class Foo: passprint(Foo.

    95720编辑于 2022-09-03
  • 来自专栏python3

    python-装饰器

    @deco def myinit(): print("init") myinit() 原始函数myinit,作用输出init; 装饰器函数deco,用一个函数func1作为参数,内部定义一个函数one ,最后return one,形成闭包; 执行顺序:先执行装饰器函数deco外部的print("111"),再执行deco内部的print("222"),最后执行原始函数myinit; 结果为: 111 def deco2(func2): print("aaa") def two(): print("bbb")         func2() return two @deco @deco2 def one,形成闭包; 装饰器函数deco2,用一个函数func2作为参数,内部定义一个函数two,最后return two,形成闭包; 执行顺序:先执行装饰器函数deco2外部的print("aaa") ,再执行装饰器函数deco外部的print("111"),接着解释器向下执行deco内部的print("222"),然后执行deco2内部的print("bbb"),最后执行原始函数myinit; 结果为

    34510发布于 2020-01-09
  • 来自专栏Jack96

    vim底行指令多行注释#和解注释

    ​1.vim底行命令模式 (1).64至74行首插入"#": 底行指令:64, 74 s/^/#/g 64 #[Running] python -u "\Py\deco_1.py" 65 #enter deco3 (foo) 66 #enter deco2 (wrap_3) 67 #enter deco1 (wrap_2) 68 #enter wrapers_1 (2, 3) 69 #enter wrapper_1 result * 2:24 74 #24 (2).64至74行首删除"#": ··· 底行指令:64,74 s/^#//g 64 [Running] python -u “\Py\deco _1.py” 65 enter deco3 (foo) 66 enter deco2 (wrap_3) 67 enter deco1 (wrap_2) 68 enter wrapers_1 (2

    2K20编辑于 2023-03-07
  • 来自专栏Jack96

    vim指定行注释和解注释

    1.vim底行命令模式 1.64至74行首插入"#": 64, 74 s/^/#/g 64 #[Running] python -u "\Py\deco_1.py" 65 #enter deco3 (foo) 66 #enter deco2 (wrap_3) 67 #enter deco1 (wrap_2) 68 #enter wrapers_1 (2, 3) 69 #enter wrapers #exit wrapper_1 result * 2:24 74 #24 1.64至74行首删除"#": 64,74 s/^#//g 64 [Running] python -u "\Py\deco _1.py" 65 enter deco3 (foo) 66 enter deco2 (wrap_3) 67 enter deco1 (wrap_2) 68 enter wrapers_1 (2

    2.8K20编辑于 2023-03-07
  • 来自专栏python-爬虫

    函数的装饰器,两层装饰器和三层装饰器

    ---------------- nick machachong -------------------------------------------------- ​``` #另外种写法 @f1_deco ----------------- ​``` 2.关于有参函数 #如函数 def sum(x,y): print(x+y) #我们要对齐装饰使其输出打印内容上下都加了'-'线 def sum_deco (sum): def wrapper(x,y): print("-") sum(x,y) return sum sum() = sum_deco(sum ) sum(x,y) #多个值 def sb(x,y,z=2,b=1): print(x,y,z,b) #把他进行封装 def sb_deco(sb): def wrapper(*args (sb) sb(1,23,2,3) 3.对于有返回值的函数式 #有返回值 def sb(x,y,z=2,b=1): return x,y,z,b #对于返回值乘以3 def sb_deco(sb

    1K10发布于 2019-07-24
  • 来自专栏小麦苗的DB宝专栏

    Python 函数(三)

    func() return wrapper wrap1 = log(add) wrap1() 注:__name__可以获得函数名称 例一(带有参数的装饰器): import time def deco msecs = (endTime - startTime)*1000 print("time is %d ms" %msecs) return wrapper @deco (func): def wrapper(*args, **kwargs): print("this is deco01") startTime = time.time end here") return wrapper def deco02(func): def wrapper(*args, **kwargs): print("this is deco02") func(*args, **kwargs) print("deco02 end here") return wrapper @deco01

    37010编辑于 2022-02-23
  • 来自专栏carven

    初探ES7 Decorator

    > deco.es5.js && node deco.es5.js 实现 Decorator 那么,装饰器要怎么实现了,什么场景下需要用到decorator呢。 // deco.js class DBAct{ constructor(options){ this. > deco.es5.js && node deco.es5.js # echo db add no permission to exec delete 实现类的Decorator 我们也可以实现一个 > deco.es5.js && node deco.es5.js # echo no permission to exec add no permission to exec delete decorator > deco.es5.js && node deco.es5.js # echo db add no permission to exec delete 这样,一个通用的decorator就出来了。

    53800发布于 2018-08-08
领券