• software2

    Tripped up by Rstrip

    Here’s a simple function which is supposed to strip any ".jpg" extension from a file name. def photo_name(photo_file): "Return the name of the photo" return photo_file.rstrip('.jpg') assert photo_name('cat.jpg') == 'cat' assert photo_name('selfie.jpg') == 'selfie' assert photo_name('emoji.gif') == 'emoji.gif' # A GIF is not a photo As shown, it passes a few simple tests. Unfortunately it turns out to be broken. >>> photo_name('dog.jpg') 'do' >>> photo_name('tile.png') 'tile.pn' There’s no mystery here. A check of the documentation shows that the optional chars parameter to str.rstrip specifies a set of trailing characters to be removed from the source string. So, in the example above, '.jpg' means: strip trailing characters in the set {'.',…

    Comentarios desactivados en Tripped up by Rstrip