471,310 Members | 1,425 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,310 software developers and data experts.

a question about decorator

Hi:

please look at this code.

def A():
print 'warp in A'
def why(self, *arg, **kw):
print 'in A'
print self
print arg
print kw
#self(*arg, **kw)

return why

class T(object):
@A()
def test(g, out):
print 'in test', out

it will out put:

warp in A
in A
<function test at 0x00BF0C70>
()
{}

the function why will be called, why? there is no code to call it.

Kyo.

Oct 22 '07 #1
1 925
On Mon, 22 Oct 2007 14:37:36 +0800, kyo guan wrote:
def A():
print 'warp in A'
def why(self, *arg, **kw):
print 'in A'
print self
print arg
print kw
#self(*arg, **kw)

return why

class T(object):
@A()
def test(g, out):
print 'in test', out

it will out put:

warp in A
in A
<function test at 0x00BF0C70>
()
{}

the function why will be called, why? there is no code to call it.
There is: ``@A()``. Parenthesis means *calling* `A`. Then `A` returns
`why` and that is then used as decorator function, i.e. called with
`T.test` as argument.

Ciao,
Marc 'BlackJack' Rintsch
Oct 22 '07 #2

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

45 posts views Thread by Edward K. Ream | last post: by
11 posts views Thread by Ville Vainio | last post: by
41 posts views Thread by John Marshall | last post: by
8 posts views Thread by Paul Morrow | last post: by
30 posts views Thread by Ron_Adam | last post: by
22 posts views Thread by Ron_Adam | last post: by
3 posts views Thread by Ron_Adam | last post: by
4 posts views Thread by thomas.karolski | last post: by
8 posts views Thread by Chris Forone | last post: by
reply views Thread by rosydwin | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.