[Python-ideas] Deprecating rarely used str methods

Steven D'Aprano steve at pearwood.info
Fri Aug 9 03:45:38 CEST 2013


On 09/08/13 04:32, Serhiy Storchaka wrote:
> The str class have some very rarely used methods. When was the last time you used swapcase() (not counting the time of apprenticeship)? These methods take place in a source code, documentation and a memory of developers. Due to the large number of methods, some of the really necessary methods can be neglected. I propose to deprecate rarely used methods (especially that for most of them there is another, more obvious way) and remove them in 4.0.


-1

It's true that, if swapcase were proposed today, it probably wouldn't be accepted. And maybe it should have been removed in Python 3. But it didn't. Deprecating working code just because (in the opinion of some people) it's not useful enough just causes code churn for no real benefit.

Somebody out there is using methods ljust, rjust, center, zfill (I use those last two), yes, even swapcase -- somebody is using it, somewhere. You will break their code for very little benefit.

Deprecation is only useful when there is a concrete plan to remove the feature. Python 4000 is so far in the future that it's more of a dream than an actual concrete planned release (this is why I don't refer to it as Python 4). There may not even be a Python 4000. Who knows?

For the next ten, or fifteen, or fifty, years, there will be features in CPython flagged as deprecated, but still working, which means that as other implementations become Python 3 compatible they will be tempted to remove these features early. That's a bad thing, since then different implementations of Python running the same version will not be able to run the same code. The chaos and pain this will cause outweighs by far any benefit you might get from eventually removing features.

When there are concrete plans for a Python 4000, that is the time to think about backwards incompatible changes. Or, following the ten-year transition period for Python 2 -> 3, maybe the BDFL will decide that Python 4000 will *not* break backwards compatibility. I was reading somebody's blog the other day talking about how he ran FORTRAN 66 code unchanged in a modern Fortran compiler and it worked perfectly.

The cost of keeping swapcase etc. is tiny. They're stable code, unlikely to need much in the way of maintenance, and the pain from removing them is likely to outweigh what benefit there is.


> s.ljust(width) == '{:<{}}'.format(s, width) == '%-*s' % (width, s)


Especially given that your alternatives are far less readable than a method call, and difficult to override in a subclass.



-- 
Steven


More information about the Python-ideas mailing list