Why does list ask about __len__?

See the Rationale section from PEP 424 that introduced __length_hint__ and offers insight on the motivation: Being able to pre-allocate lists based on the expected size, as estimated by __length_hint__ , can be a significant optimization. CPython has been observed to run some code faster than PyPy, purely because of this optimization being present. In … Read more

Two integers in Python have same id, but not lists or tuples

Immutable objects don’t have the same id, and as a matter of fact this is not true for any type of objects that you define separately. Generally speaking, every time you define an object in Python, you’ll create a new object with a new identity. However, for the sake of optimization (mostly) there are some … Read more

How does swapping of members in tuples (a,b)=(b,a) work internally?

Python separates the right-hand side expression from the left-hand side assignment. First the right-hand side is evaluated, and the result is stored on the stack, and then the left-hand side names are assigned using opcodes that take values from the stack again. For tuple assignments with 2 or 3 items, Python just uses the stack … Read more

Why are reversed and sorted of different types in Python?

The difference is that reversed is an iterator (it’s also lazy-evaluating) and sorted is a function that works “eagerly”. All built-in iterators (at least in python-3.x) like map, zip, filter, reversed, … are implemented as classes. While the eager-operating built-ins are functions, e.g. min, max, any, all and sorted. >>> a = [1,2,3,4] >>> r … Read more

Why were literal formatted strings (f-strings) so slow in Python 3.6 alpha? (now fixed in 3.6 stable)

Note: This answer was written for the Python 3.6 alpha releases. A new opcode added to 3.6.0b1 improved f-string performance significantly. The f”…” syntax is effectively converted to a str.join() operation on the literal string parts around the {…} expressions, and the results of the expressions themselves passed through the object.__format__() method (passing any :.. … Read more

Python eval: is it still dangerous if I disable builtins and attribute access?

I’m going to mention one of the new features of Python 3.6 – f-strings. They can evaluate expressions, >>> eval(‘f”{().__class__.__base__}”‘, {‘__builtins__’: None}, {}) “<class ‘object’>” but the attribute access won’t be detected by Python’s tokenizer: 0,0-0,0: ENCODING ‘utf-8’ 1,0-1,1: ERRORTOKEN “‘” 1,1-1,27: STRING ‘f”{().__class__.__base__}”‘ 2,0-2,0: ENDMARKER ”

Are list comprehensions syntactic sugar for `list(generator expression)` in Python 3?

Both work differently. The list comprehension version takes advantage of the special bytecode LIST_APPEND which calls PyList_Append directly for us. Hence it avoids an attribute lookup to list.append and a function call at the Python level. >>> def func_lc(): [x**2 for x in y] … >>> dis.dis(func_lc) 2 0 LOAD_CONST 1 (<code object <listcomp> at … Read more