a question about decorator

Marc 'BlackJack' Rintsch bj_666 at gmx.net
Mon Oct 22 03:02:05 EDT 2007


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



More information about the Python-list mailing list