Re: decorators ?

From: Josiah Carlson (jcarlson_at_uci.edu)
Date: 11/30/04


Date: Tue, 30 Nov 2004 10:46:25 -0800
To: km <km@mrna.tn.nic.in>, python-list@python.org


km <km@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



Relevant Pages

  • Re: Ruby vs Perl performance
    ... It is fun, though. ... def initialize some, random, args ...
    (comp.lang.ruby)
  • Re: Meta-Meta-Programming
    ... def just_wrap ... *args, not &block. ... but it does sound good: monitor-functions. ...
    (comp.lang.ruby)
  • Re: Meta-Meta-Programming
    ... def just_wrap ... *args, not &block. ... but it does sound good: monitor-functions. ...
    (comp.lang.ruby)
  • Meta-Meta-Programming
    ... def just_wrap ... *args, not &block. ... but it does sound good: monitor-functions. ...
    (comp.lang.ruby)
  • Re: When are attr_reader type methods called?
    ... > def call_this ... This will work as the method bar exists and belongs to the class Foo, ... hoo # this will raise a NoMethodError, ... call them inside your class definitions -- they are methods available ...
    (comp.lang.ruby)