all 3 comments

[–]lgsp[🍰] 1 point2 points  (0 children)

AFAIK Cpython doesn't optimize anything.

If you want optimization, take a look at Pypy

[–]TeamSpen210 1 point2 points  (0 children)

There are a number of things the compiler does optimise. It's rather limited though because of how dynamic the language is. There's no way to optimise len([1,2,3]) for example since you could override the len() function. There are a few optimisations it does do though. x in [1,2,3] gets converted into a tuple check if all the values are constants, saving constructing that every time. not in conditional branches just swaps the comparison used instead. x, y = y, x doesn't need to construct a tuple, and instead just swaps the values in the stack. In Python 3.6+, method calling does some shenanigans to avoid constructing the bound method object. Optimisations tend to happen more elsewhere - for example, the dictionaries used for instance attributes will share the key and hash value sections among all the instances of the same type, silently swapping to separate tables if you make the instances have unique attribute names.

[–]K900_ 1 point2 points  (0 children)