decorators ?

Josiah Carlson jcarlson at uci.edu
Tue Nov 30 13:46:25 EST 2004


km <km at mrna.tn.nic.in> wrote:
> 
> Hi all,
>  was going thru the new features introduced into python2.4 version.
> i was stuck with 'decorators' -  can someone explain me the need of such a thing called decorators ? 

Decorators are not 'needed'.  They are a convenience.

Among the things that they are meant to make more convenient are the
following...

class foo:
    @staticmethod
    def goo(arg, ...):
        pass

rather than...

class foo:
    def goo(arg, ...):
        pass
    goo = staticmethod(goo)

And other things of its ilk.  Where they will be used the most right off
is in function signatures for PyObjC bindings in (I believe) OSX by Bob
Ippolito.  Using his example:

class FooClass(NSObject):
     def returnsAnInteger(self):
         return 1
     returnsAnInteger = objc.selector(returnsAnInteger, signature='i@:')

     def returnsVoidTakesAnInteger_(self, anInteger):
         pass
     returnsVoidTakesAnInteger_ = \
         objc.selector(returnsVoidTakesAnInteger_, signature='v@:i')

...becomes...

def signature(sig):
     def _signature(fn):
         return objc.selector(fn, signature=sig)
     return _signature

class FooClass(NSObject):
     @signature('i@:')
     def returnsAnInteger(self):
         return 1

     @signature('v@:i')
     def returnsVoidTakesAnInteger_(self, anInteger):
         pass


 - Josiah




More information about the Python-list mailing list