Why list comprehensions create a function internally?

The main logic of creating a function is to isolate the comprehension’s iteration variablepeps.python.org. By creating a function: Comprehension iteration variables remain isolated and don’t overwrite a variable of the same name in the outer scope, nor are they visible after the comprehension However, this is inefficient at runtime. Due to this reason, python-3.12 implemented … Read more

Why is the simpler loop slower?

I checked the source code of the bytecode (python 3.11.6) and found that in the decompiled bytecode, it seems that only JUMP_BACKWARD will execute a warmup function, which will trigger specialization in python 3.11 when executed enough times: PyObject* _Py_HOT_FUNCTION _PyEval_EvalFrameDefault(PyThreadState *tstate, _PyInterpreterFrame *frame, int throwflag) { /* … */ TARGET(JUMP_BACKWARD) { _PyCode_Warmup(frame->f_code); JUMP_TO_INSTRUCTION(JUMP_BACKWARD_QUICK); } … Read more

can you recover from reassigning __builtins__ in python?

You can usually get access to anything you need, even when __builtins__ has been removed. It’s just a matter of digging far enough. For example: Python 2.7.3 (default, Apr 10 2012, 23:31:26) [MSC v.1500 32 bit (Intel)] on win32 Type “help”, “copyright”, “credits” or “license” for more information. >>> __builtins__ = 0 >>> open Traceback … Read more

Python or IronPython

There are a number of important differences: Interoperability with other .NET languages. You can use other .NET libraries from an IronPython application, or use IronPython from a C# application, for example. This interoperability is increasing, with a movement toward greater support for dynamic types in .NET 4.0. For a lot of detail on this, see … Read more

What is co_names?

As other’s have already said, this seems to be a documentation error. The documentation for code objects clearly contradicts the documentation for inspect: co_varnames is a tuple containing the names of the local variables (starting with the argument names); […] co_names is a tuple containing the names used by the bytecode; Also, accessing the attributes … Read more

What is a “code object” mentioned in this TypeError message?

One way to create a code object is to use compile built-in function: >>> compile(‘sum([1, 2, 3])’, ”, ‘single’) <code object <module> at 0x19ad730, file “”, line 1> >>> exec compile(‘sum([1, 2, 3])’, ”, ‘single’) 6 >>> compile(‘print “Hello world”‘, ”, ‘exec’) <code object <module> at 0x19add30, file “”, line 1> >>> exec compile(‘print “Hello … Read more

Why is list(x for x in a) faster for a=[0] than for a=[]?

What you observe, is that pymalloc (Python memory manager) is faster than the memory manager provided by your C-runtime. It is easy to see in the profiler, that the main difference between both versions is that list_resize and _PyObjectRealloc need more time for the a=[]-case. But why? When a new list is created from an … Read more