<html><body><br />On 8 Mar, 06:02 pm, phd@phd.pp.ru wrote:<br />&gt;On Thu, Mar 08, 2007 at 06:54:30PM +0100, "Martin v. L?wis" wrote:<br />&gt;&gt; &#160; &#160; &#160;back_name = splitext(name[0]) + '.bak'<br />&gt;<br />&gt;back_name = splitext(name)[0] + '.bak'<br /><br />This is really totally secondary to the point I actually care about, but seeing this antipattern bandied about as a reason to make the change is starting to bother me.<br /><br />There's no way to "fix" this idiom. &#160;Forget the corner case; what if you have a foobar.py and a foobar.txt in the same directory? &#160;This is not at all uncommon, and your "backup" function here will clobber those files in any case.<br /><br />Furthermore, although the module documentation is vague, the docstring for splitext specifically says "either part may be empty" and "extension is everything from the last dot to the end". &#160;Again, this is a case of needing a function designed to perform a specific task, and instead relying on half-broken idioms which involve other functions. &#160;make_backup_filename(x) might *use* splitext, but it is not what splitext is *for*. &#160;A correct implementation which did use splitext would look like this:<br /><br />&#160; &#160; def make_backup_filename(filename):<br />&#160; &#160; &#160; &#160; base, extension = splitext(filename)<br />&#160; &#160; &#160; &#160; return base + '.bak' + extension<br /><br />although personally I would probably prefer this:<br /><br />&#160; &#160; def make_backup_filename(filename):<br />&#160; &#160; &#160; &#160; return filename + '.bak'<br /><br />If the behavior of the old code is going to be buggy in any case, it might as well be buggy and consistent. &#160;Consider a program that periodically makes and then cleans up backup files, and uses the "correct" splitext-using function above. &#160;Perhaps .cshrc.bak makes more sense than .bak.cshrc to some people, but that means that on a system where python is upgraded, .bak.cshrc will be left around forever. &#160;Even on a program whose functionality is improved by this change, the incompatibility between versions might create problems.<br /></body></html>