0

I was wondering if there is a reason why python evaluates the slicing/indexing after the definition of a list/tuple ?

(This question concerns Python Code Golf so I know it's not readable or "good practice", it's just about the accepted syntax and the fundamental behavior of the language.)

We can use index to mimic the ternary operator's behavior:

a if x > 9 else b
(b,a)[x>9]  # way shorter

But this has an issue: the content of the tuple is evaluated before the condition in the index.

I created an example to illustrate the point: a function f that reduces the size of a string by one recursively till the string is empty, then returns 0

f = lambda s: f(s) if len(s:=s[:-1]) else 0
print(f("abc"))  # works fine

f = lambda s: (0, f(s))[len(s:=s[:-1])==0]
print(f("abc"))  # max recursion depth error

The recursion depth error occurs because the tuple definition is evaluated before the index. It means that what is in the slice/index doesn't matter, the function will be called again and again.

I don't really understand why python doesn't evaluate the slice/index before because even an obvious case like the following fails:

f = lambda: (0, f())[0]
f()  # max recursion depth error

On top of that, it could benefit in terms of memory usage and runtime if we just evaluate the single element (or the slice) we want from the array and not every single element:

x = 2
print([long_computation(), other_long_computation(), 0][x])

Is there any reason not to evaluate the slice/index before the tuple definition ?

9
  • 1
    You can't slice something that doesn't exist yet. (Also, that's indexing, not slicing.) Jan 22, 2022 at 10:29
  • @user2357112supportsMonica Yes it's more about indexing :) The tuple could exist but without evaluating its elements, kinda like that f = lambda s: (lambda: 0, lambda: f(s))[len(s:=s[:-1])==0]() which works
    – gruvw
    Jan 22, 2022 at 14:06
  • 1
    So you really want a tuple with lazy evaluation of its members? There may be a way to do that in Python, but I don't know how and it certainly wouldn't be worth the trouble. Jan 22, 2022 at 14:21
  • @MarkRansom yes I would like lazy tuple evaluation when [...] index/slice directly follows as I don't see why the rest should be evaluated because it will be left over by the slicing, the evaluation would happen for kept elements directly after slicing
    – gruvw
    Jan 22, 2022 at 14:23
  • But that would only be useful for code golf, a use case that the Python dev team has no desire to encourage. For anyone not doing code golf, it just adds complexity and weird edge cases to the language. Jan 23, 2022 at 0:51

1 Answer 1

-2

Humans read left-to-right, the parser reads left-to-right, the compiler just converts whatever is parsed into bytecode. It would make sense- and it's easier- to just parse it left-to-right rather than adding special cases for stuff that can be already done properly in the left-to-right fashion. Why do you need to do this anyways? You don't need to do it. You can already do it properly in the current parser and compiler. The complexity of this special case and how rare it's ever used, both factors are enough as reasons to not evaluate the slice before the tuple definition.

5
  • The ternary operator is a counter example, not evaluated from left to right. And I don't need to specify why I want something, I just want answers...
    – gruvw
    Jan 22, 2022 at 14:00
  • @Gruvw I gave you another answer: how rare it is ever used is a reason not to evaluate the slice before the tuple definition. You can also suggest this in the python ideas mailing list, but the reason I mentioned in this comment may likely be given to you.
    – uknonot
    Jan 23, 2022 at 20:41
  • @Gruvw also, it would break some code that relies on evaluating the tuple first.
    – uknonot
    Jan 23, 2022 at 22:55
  • I disagree with you about how "rare" it would occur as I often use tuples and indexing like that (not often using recursion, but I also gave a simple example with long_computation()). However, backward compatibility is the biggest issue to this idea. That being said, my question was more about why python do it that way than about we need to change this.
    – gruvw
    Jan 24, 2022 at 7:36
  • You have already got your answer in the comments anyway, so I won't argue anymore.
    – uknonot
    Jan 25, 2022 at 0:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.