def dispatch(self, value): method_name='visit_'+str(value) method =getattr(self, method_name) method() 

(continued from previous page) 

It’s suggested that you use a prefix for the method names, such as$_{visit_}$in this example. Without such a prefix, if values are coming from an untrusted source, an attacker would be able to call any method on your object. 

## 3.11 Can’t you emulate threads in the interpreter instead of relying on an OS-specific thread implementation? 

Answer 1: Unfortunately, the interpreter pushes at least one C stack frame for each Python stack frame. Also, extensions can call back into Python at almost random moments. Therefore, a complete threads implementation requires thread support for C. 

Answer 2: Fortunately, there is Stackless Python, which has a completely redesigned interpreter loop that avoids the C stack. 

## 3.12 Why can’t lambda expressions contain statements? 

Python lambda expressions cannot contain statements because Python’s syntactic framework can’t handle statements nested inside expressions. However, in Python, this is not a serious problem. Unlike lambda forms in other languages, where they add functionality, Python lambdas are only a shorthand notation if you’re too lazy to define a function. 

Functions are already first class objects in Python, and can be declared in a local scope. Therefore the only advantage of using a lambda instead of a locally-defined function is that you don’t need to invent a name for the function – but that’s just a local variable to which the function object (which is exactly the same type of object that a lambda expression yields) is assigned! 

## 3.13 Can Python be compiled to machine code, C or some other language? 

Cython compiles a modified version of Python with optional annotations into C extensions. Nuitka is an up-and- coming compiler of Python into C++ code, aiming to support the full Python language. For compiling to Java you can consider VOC. 

## 3.14 How does Python manage memory? 

The details of Python memory management depend on the implementation. The standard implementation of Python, CPython , uses reference counting to detect inaccessible objects, and another mechanism to collect reference cycles, periodically executing a cycle detection algorithm which looks for inaccessible cycles and deletes the objects involved. The$_{gc}$module provides functions to perform a garbage collection, obtain debugging statistics, and tune the collector’s parameters. 

Other implementations (such as Jython or PyPy), however, can rely on a different mechanism such as a full-blown garbage collector. This difference can cause some subtle porting problems if your Python code depends on the behavior of the reference counting implementation. 

In some Python implementations, the following code (which is fine in CPython) will probably run out of file descrip- tors: 

