Is mutating the iterable of a list comprehension during comprehension intended?

Sorry in advance if this post is confusing or this is the wrong subreddit to post to

I was playing around with list comprehension and this seems to be valid for Python 3.13.5

(lambda it: [(x, it.append(x+1))[0] for x in it if x <= 10])([0])

it = [0] print([(x, it.append(x+1))[0] for x in it if x <= 10])

The line above will print a list containing 0 to 10. The part Im confused about is why mutating it is allowed during list comprehension that depends on it itself, rather than throwing an exception?

submitted by /u/CCThermal to r/Python
[link] [comments]


Commentaires

Laisser un commentaire

Votre adresse e-mail ne sera pas publiée. Les champs obligatoires sont indiqués avec *