Do pythons like sugar?

Afanasiy abelikov72 at hotmail.com
Thu Jan 9 04:01:27 EST 2003


On Thu, 09 Jan 2003 06:52:47 GMT, Afanasiy wrote:

>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)

So the answer is a definitive no? I want to do things as I have them,
I don't want to change my design just to be able to type 'self' less.

Is there any tricky one-liner that brings object members in scope?

P.S. The specific implementation above should be considered irrelevant.




More information about the Python-list mailing list