Do pythons like sugar?

Bengt Richter bokr at
Thu Jan 9 23:06:23 CET 2003

On Thu, 09 Jan 2003 15:31:11 GMT, Afanasiy <abelikov72 at> wrote:

>On Thu, 09 Jan 2003 06:52:47 GMT, Afanasiy <abelikov72 at>
>>I've written this method for a text formatting class...
>>But it's ugly as sin. With all those self's I consider
>>it much less readable than it could be...
>>A few languages provide syntax sugar for dealing with
>>this by allowing you to localize the class scope.
>>Does Python? eg. `with self do:`
>>  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)
>It should not be common practice to slander someone who asks this.
>I'm going to attempt to ignore this thread now. Have a nice life.

I am sorry to read that you felt slandered. I may have missed a post or two.
There is certainly no license to slander in this newsgroup, and I think archives
fortunately will show that most disagreements do not end in huffy standoffs.

That said, I understand the feeling of being mistaken for something I'm not
and getting a response that is both socially and technically off mark.
Join the club ;-) It's bound to happen from time to time.

A point relating to your question is optimization. The 'self' name is not
treated specially, so it is looked up just like any other local name in the
method function. With an alternate indication of default self attribute
references, presumably one step could be removed from the code stream,
using a suitable new byte code instruction.

A possibility is keeping the '.' and leaving out the 'self' when the current
object is being referred to. That would make your code above look like:
(with 4-space indentation ;-)

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

How does that look to you?

Bengt Richter

More information about the Python-list mailing list