Do pythons like sugar?

Simon Wittber (Maptek) Simon.Wittber at perth.maptek.com.au
Thu Jan 9 02:06:19 EST 2003


>Does Python? eg. `with self do:`

Nope. However, you could perhaps rewrite this function to be slighty
more readable and faster. (See below)

>  def format( self, width=80 ):    
>    self.lines = ['']
>    i = 0
>    for word in self.text.split():  
>      if self.textwidth(self.lines[i]) + self.textwidth(word) <= width:
>        if self.textwidth(self.lines[i]) > 0:
>          self.lines[i] += ' '
>        self.lines[i] += word
>      else:
>        i += 1
>        self.lines.append(word)


    def format(self, width=80):    
        lines = ['']
        i = 0
        for word in self.text.split():  
            if self.textwidth(lines[i]) + self.textwidth(word) <= width:
                if self.textwidth(lines[i]) > 0:
                    lines[i] += ' '
                lines[i] += word
            else:
                i += 1
                lines.append(word)

        self.lines = lines

This avoids a few references to self, which looks better, and is slighty
faster too!

Simon.





More information about the Python-list mailing list