python reverse string ::-1 slice function equivalent

Viewed 56

I see how "String"[::-1] works to return "gnirtS"

But isn't this supposed to be compatible with the slice function?

"String"[slice(-1, 0, -1)] ?

This returns "gnirt"

1 Answers

slice(-1, 0, -1) is not equivalent to [::-1]. slice(None, None, -1) is.

class SliceNotationTranslator:
    def __getitem__(self, item):
        print(item)

s = SliceNotationTranslator()
s[::-1]

Outputs

slice(None, None, -1)
Related