Python lists/arrays: disable negative indexing wrap-around in slices

后端 未结 4 2059
不知归路
不知归路 2020-12-11 15:43

While I find the negative number wraparound (i.e. A[-2] indexing the second-to-last element) extremely useful in many cases, when it happens inside a slice it i

4条回答
  •  猫巷女王i
    2020-12-11 16:13

    While you could subclass e.g. list as suggested by jdi, Python's slicing behaviour is not something anyone's going to expect you to muck about with.

    Changing it is likely to lead to some serious head-scratching by other people working with your code when it doesn't behave as expected - and it could take a while before they go looking at the special methods of your subclass to see what's actually going on.

    See: Action at a distance

提交回复
热议问题