Python Data Types

(This and the following subsections describe Python in quite a lot of detail. If you are more interested in AIL, Amoeba and how they are linked with Python, you can skip to section 3 now.)

Python's syntax may not have big surprises (which is exactly as it should be), but its data types are quite different from what is found in languages like C, Ada or Modula-3. All data types in Python, even integers, are `objects'. All objects participate in a common garbage collection scheme (currently implemented using reference counting). Assignment is cheap, independent of object size and type: only a pointer to the assigned object is stored in the assigned-to variable. No type checking is performed on assignment; only specific operations like addition test for particular operand types.

The basic object types in Python are numbers, strings, tuples, lists and dictionaries. Some other object types are open files, functions, modules, classes, and class instances; even types themselves are represented as objects. Extension modules written in C can define additional object types; examples are objects representing windows and Amoeba capabilities. Finally, the implementation itself makes heavy use of objects, and defines some private object types that aren't normally visible to the user. There is no explicit pointer type in Python.

Numbers, both integers and floating point, are pretty straightforward. The notation for numeric literals is the same as in C, including octal and hexadecimal integers; precision is the same as long or double in C. A third numeric type, `long integer', written with an `L' suffix, can be used for arbitrary precision calculations. All arithmetic, shifting and masking operations from C are supported.

Strings are `primitive' objects just like numbers. String literals are written between single quotes, using similar escape sequences as in C. Operations are built into the language to concatenate and to replicate strings, to extract substrings, etc. There is no limit to the length of the strings created by a program. There is no separate character data type; strings of length one do nicely.

Tuples are a way to `pack' small amounts of heterogeneous data together and carry them around as a unit. Unlike structure members in C, tuple items are nameless. Packing and unpacking assignments allow access to the items, for example:

x = 'Hi', (1, 2), 'World'   # x is a 3-item tuple,
                            # its middle item is (1, 2)
p, q, r = x                 # unpack x into p, q and r
a, b = q                    # unpack q into a and b
A combination of packing and unpacking assignment can be used as parallel assignment, and is idiom for permutations, e.g.:
p, q = q, p                 # swap without temporary
a, b, c = b, c, a           # cyclic permutation
Tuples are also used for function argument lists if there is more than one argument. A tuple object, once created, cannot be modified; but it is easy enough to unpack it and create a new, modified tuple from the unpacked items and assign this to the variable that held the original tuple object (which will then be garbage-collected).

Lists are array-like objects. List items may be arbitrary objects and can be accessed and changed using standard subscription notation. Lists support item insertion and deletion, and can therefore be used as queues, stacks etc.; there is no limit to their size.

Strings, tuples and lists together are sequence types. These share a common notation for generic operations on sequences such as subscription, concatenation, slicing (taking subsequences) and membership tests. As in C, subscripts start at 0.

Dictionaries are `mappings' from one domain to another. The basic operations on dictionaries are item insertion, extraction and deletion, using subscript notation with the key as subscript. (The current implementation allows only strings in the key domain, but a future version of the language may remove this restriction.)