Do pythons like sugar?
Martijn Faassen
m.faassen at vet.uu.nl
Tue Jan 14 07:51:38 EST 2003
Afanasiy <abelikov72 at hotmail.com> wrote:
> 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)
If I see too many 'self.' I rewrite method to something like:
def format(self, width=80):
lines = [''] # could write lines = self.lines = [''] instead
textwidth = self.textwidth
i = 0
for word in self.text.split():
if textwidth(lines[i]) + textwidth(word) <= width:
if textwidth(lines[i]) > 0:
lines[i] += ' '
lines[i] += word
else:
i += 1
lines.append(word)
self.lines = lines # could skip this if wrote lines = self.lines = ['']
This may actually be more efficient too, as there are less attribute
lookups. If a method is really needing to get a whole host of attributes
as local variables I'd doubt the method or class is well written; usually
you only have to pull in one or two.
Regards,
Martijn
--
History of the 20th Century: WW1, WW2, WW3?
No, WWW -- Could we be going in the right direction?
More information about the Python-list
mailing list