[Python-Dev] Re: decorators and 2.4

Paul Prescod paul at prescod.net
Sat Jun 26 13:57:21 EDT 2004


Bill Janssen wrote:

> It's of course the scanning that I'm talking about.  Having only "def"
> and "class" at the left edge makes it remarkably easy to scan for a
> function or class definition.  Putting oddly-shaped decorators there
> too ruins this.

I have to admit that I see this as unnecessary alarmism and I don't 
think it is supported by fact. Looking at just a few standard library 
modules you will see that it is common to put documentation (i.e. 
metadata) and globals in the left-margin. I'll demonstrate in two code 
examples and we'll see if they are really much different in terms of 
readability. In my opinion, the basic code shape (outdent, indent) is 
the same and that is what matters for readability.

     # Dispatch routine for best timer program (fastest if
     # an integer but float works too that).

     def trace_dispatch_i(self, frame, event, arg):
         timer = self.timer
         t = timer() - self.t - self.bias
         if self.dispatch[event](self, frame,t):
             self.t = timer()
         else:
             self.t = timer() - t  # put back unrecorded delta

     # Dispatch routine for macintosh (timer returns time in
     # 1/60th second)

     def trace_dispatch_mac(self, frame, event, arg):
         timer = self.timer
         t = timer()/60.0 - self.t - self.bias
         if self.dispatch[event](self, frame, t):
             self.t = timer()/60.0
         else:
             self.t = timer()/60.0 - t  # put back

Versus:


     # Dispatch routine for best timer program (fastest if
     # an integer but float works too that).

     @classmethod
     def trace_dispatch_i(self, frame, event, arg):
         timer = self.timer
         t = timer() - self.t - self.bias
         if self.dispatch[event](self, frame,t):
             self.t = timer()
         else:
             self.t = timer() - t  # put back unrecorded delta

     # Dispatch routine for macintosh (timer returns time in
     # 1/60th second)

     @something(something)
     def trace_dispatch_mac(self, frame, event, arg):
         timer = self.timer
         t = timer()/60.0 - self.t - self.bias
         if self.dispatch[event](self, frame, t):
             self.t = timer()/60.0
         else:
             self.t = timer()/60.0 - t  # put back

  Paul Prescod





More information about the Python-Dev mailing list