[Python-ideas] Deprecating rarely used str methods

Serhiy Storchaka storchaka at gmail.com
Thu Aug 8 21:36:40 CEST 2013


08.08.13 21:58, MRAB написав(ла):
> On 08/08/2013 19:32, Serhiy Storchaka wrote:
>> s.ljust(width) == '{:<{}}'.format(s, width) == '%-*s' % (width, s)
>> s.ljust(width, fillchar) == '{:{}<{}}'.format(s, fillchar, width)
>> s.rjust(width) == '{:>{}}'.format(s, width) == '%*s' % (width, s)
>> s.rjust(width, fillchar) == '{:{}>{}}'.format(s, fillchar, width)
>> s.center(width) == '{:^{}}'.format(s, width)
>> s.center(width, fillchar) == '{:{}^{}}'.format(s, fillchar, width)
>>
> You could apply the same kind of reasoning that's used with regex: why
> use .format when .ljust, etc, is shorter and faster?

Because in common case the result of ljust/rjust is concatenated with 
other strings for output. Instead of

     print('|' + name.ljust(20) + '|' + str(value).rjust(8) + '|')

you can write just

     print('|%-20s|%8s|' % (name, value))

or

     print('|{:<20}|{:>8}|'.format(name, value))

which are shorter and cleaner.

>> str.expandtabs([tabsize]) is rarely used and can be moved to the
>> textwrap module.
>>
> I use it to ensure that indentation is always 4 spaces and never tabs
> in the regex release in case I've missed a setting in an editor
> somewhere, so it _is_ an important method! :-)

Yes, I do not propose totally remove it. But this method is not used in 
most applications in contrary to such popular methods as split() or 
strip(). It looks as one of multiline formatting methods from the 
textwrap module.



More information about the Python-ideas mailing list