• United States

Python objects and modules

Jun 09, 20034 mins
Enterprise Applications

* Gearhead columnist Mark Gibbs moves from the fundamentals of Python to the larger features of the language

Python objects and modules

Last week, we looked at the fundamentals of Python ( so this week we’ll examine some of the larger features of the language.

The earlier story  provided a close brush with Python statements – you saw assignments, tests and loops. And what is being manipulated isn’t your common or garden variables. What you’ve got are objects. Every data item under Python is an object with a name, an identity, a type and a value.

You might remember we mentioned that Python object types can be dynamic – the type can be defined by the value you tell the object to store or by the operations you perform on the object.

These dynamically typed objects are referred to as mutable, and you also can create immutable objects whose value and types can’t be changed – the tuples we discussed last week are a good example of immutable objects. Tuples, lists and dictionaries also are containers because they contain references to other objects.

Many Python objects also have attributes (properties) and methods (functions supported an object type). For example:

x = 2 + 4j # define a complex number

r = x.real # use method to get the real attribute

a = [1, 2, 3] # define an array

a.append(4) # append method to add an item

Python has a plethora of functions to interrogate objects, including id() to get the identity of an object (which usually returns the memory location of the object, although that depends on the implementation); type() to get its type (note that even types have a type – yep, their type is ‘type’); and isinstance() to test the type.

Python objects are “reference counted.” This means that whenever an object is assigned a new name (“a = 4” creates an object “4” and the name “a” is a pointer to the object), or placed in a container object (added to an array), the object’s count is incremented. Conversely, whenever a reference is deleted (as in “del a”) or no longer assigned to another object (“a = 5” so the object “4” is no longer pointed to) the count is decremented.

This is where an important aspect of Python is revealed: When an object’s reference count reaches 0 it is “garbage collected” – the memory assigned to that object is automatically reclaimed by the interpreter and made available for future use. This prevents memory leakage. Memory leakage occurs when memory allocations are not recovered when the object they represent is de-referenced, resulting in memory getting eaten up.

In Python the automatic garbage collection works very well, but it is possible to defeat it with objects that create a reference to each other creating a “circular dependency” – see the example on page 19 of the book “Python Essential Reference” published by New Riders, which shows how this can happen.

Anyway, objects under Python are a complex world that space prevents us from exploring in any greater detail.

Now, when you write a Python program you actually are creating a “module.” Everything in Python is a module, and one module can import functions from another module. And modules that work with Python programs can be written in languages other than Python, such as C.

In Python you can, like in most other languages, create subprograms or functions that can be called individually from imported modules. For example, the following function could be an element of a module called

def CtoF(c=0):

return (1.8 * c) + 32

The purpose of “c=0” in the first line is to provide a default value in case the function is called with the argument missing, which would otherwise “throw” an error.

The module called “_main_” is the core of the program, and Python comes with a large number of standard support modules. Some are built in to the core of the interpreter, while others are external and have to be loaded. Internal modules include local operating-system service support and functions that benefit from being preloaded for performance reasons, such as exception handling.

External modules can be divided into those that are platform-specific and those that are cross-platform. In the latter category are services such as the main Internet protocols (SMTP, POP2, HTTP – server and client, generic socket server and so on) and services such as file input and output, string handling and SQL database access.

Next week, we’ll wrap up our discussion about Python. Export your functions to