Differences between revisions 1 and 2
Revision 1 as of 2008-04-17 08:24:11
Size: 1106
Editor: ZoomQuiet
Comment:
Revision 2 as of 2008-04-19 08:45:38
Size: 2625
Editor: ZoomQuiet
Comment:
Deletions are marked like this. Additions are marked like this.
Line 43: Line 43:
== 研究原因 ==
{{{yuting cui <[email protected]>
reply-to [email protected],
to [email protected],
date Sat, Apr 19, 2008 at 2:30 AM
subject [CPyUG:48006] Re: [CPyUG:/] Re: decorator的另一种写法
}}}
简单看了一下,对于一个函数来说,它的默认的__get__方法是把实例和这个函数绑定
比如说:{{{
py>>> class A(object):
   def __init__(self):
       pass

py>>> A.__dict__['__init__']
<function __init__ at 0x014A77F0>

py>>> a=A()
py>>> a.__dict__['__init__']

Traceback (most recent call last):
 File "<pyshell#47>", line 1, in <module>
   a.__dict__['__init__']
KeyError: '__init__'

py>>> a.__init__
<bound method A.__init__ of <__main__.A object at 0x014AA670>>

}}}

这个地方按照Python2.5参考手册3.4.2.3所说{{{
相当于调用type(a).__dict__['__init__'].__get__(a,type(a))
为了达到这个目的,function的默认__get__
就是把给定参数和该函数绑定并返回一个实例方法(instancemethod)
这样在你调用a.__init__这个instancemethod的时候,
就相当于把a作为第一个参数并在后面按顺序添加其他的参数之后传递给
__init__这个function
}}}

于是,前面那个特殊写法的decorator就很好理解了{{{
@A.__get__
def f(args):pass
}}}
这样在调用f(args)的时候,就相当于
{{{
调用A.__get__(f)(args)
而A.__get__(f)按上述说法相当于
一个把f绑定在第一个参数的A
这样A.__get__(f)(args)就等于A(f,args)
}}}
`完毕`

TableOfContents

Include(ZPyUGnav)

优雅型修饰

decorator的另一种写法 {{{from leopay <[email protected]> reply-to [email protected], to [email protected], date Wed, Apr 16, 2008 at 11:43 PM subject [CPyUG:47631] decorator的另一种写法 }}}

一般decorator都是写一个嵌套函数,

   1 def A(func):
   2     def new_func(*args, **argkw):
   3         #做一些额外的工作
   4         return func(*args, **argkw) #调用原函数继续进行处理
   5     return new_func
   6 @A
   7 def f(args):pass

其实也有另外一种"优雅"点的写法:

   1 def A(func, *args, **argkw):
   2     #做一些额外的工作
   3     return func(*args, **argkw) #调用原函数继续进行处理
   4 
   5 @A.__get__
   6 def f(args):pass    

研究原因

{{{yuting cui <[email protected]> reply-to [email protected], to [email protected], date Sat, Apr 19, 2008 at 2:30 AM subject [CPyUG:48006] Re: [CPyUG:/] Re: decorator的另一种写法 }}} 简单看了一下,对于一个函数来说,它的默认的get方法是把实例和这个函数绑定 比如说:

py>>> class A(object):
   def __init__(self):
       pass

py>>> A.__dict__['__init__']
<function __init__ at 0x014A77F0>

py>>> a=A()
py>>> a.__dict__['__init__']

Traceback (most recent call last):
 File "<pyshell#47>", line 1, in <module>
   a.__dict__['__init__']
KeyError: '__init__'

py>>> a.__init__
<bound method A.__init__ of <__main__.A object at 0x014AA670>>

这个地方按照Python2.5参考手册3.4.2.3所说

相当于调用type(a).__dict__['__init__'].__get__(a,type(a))
为了达到这个目的,function的默认__get__
就是把给定参数和该函数绑定并返回一个实例方法(instancemethod)
这样在你调用a.__init__这个instancemethod的时候,
就相当于把a作为第一个参数并在后面按顺序添加其他的参数之后传递给
__init__这个function

于是,前面那个特殊写法的decorator就很好理解了

@A.__get__
def f(args):pass

这样在调用f(args)的时候,就相当于

调用A.__get__(f)(args)
而A.__get__(f)按上述说法相当于
一个把f绑定在第一个参数的A
这样A.__get__(f)(args)就等于A(f,args)

完毕


反馈

创建 by -- ZoomQuiet [DateTime(2008-04-17T08:24:11Z)]

PageComment2

[:/PageCommentData:PageCommentData]

MiscItems/2008-04-17 (last edited 2009-12-25 07:14:50 by localhost)