Python internals
Terry Reedy
tjreedy at udel.edu
Tue Jul 15 16:48:16 EDT 2008
Peter Anderson wrote:
> Hi! I am slowly teaching myself Python. I was reading David Beazley's
> excellent book "Python - Essential Reference"; in particular about
> variables. Let me quote:
>
> "Python is a dynamically typed language in which names can represent
> values of different types during the execution of a program. In fact the
> names used in the program are really just labels for various quantities
> and objects. The assignment operator simply creates an association
> between a name and a value. This is different from C, for example, in
> which a name (variable) represents a fixed size and location in memory..."
>
> As an old mainframe programmer, I understand the way C does things with
> variable but this text got me wondering how Python handles this
> "association" between variable name and value at the lower level. Is it
> like a fifo list?
No. Names are associated with (references to) objects in a namespace.
The details are implementation specific. For CPython, the references
are addresses stored in an array. If the set of names is dynamic, names
are converted to indexes by hashing during execution and the array of
references is expanded before it gets full. If the set of names is
static, as is usual for the local namespace of functions, an
optimization converts the names to indexes (in a constant length array)
during compilation. (On function startup, slots corresponding to
non-parameter local vars are, I expect, initialized to invalid reference
values to detect 'use before assignment' bugs.)
Terry Jan Reedy
More information about the Python-list
mailing list