GEOG 485:
GIS Programming and Software Development

1.5.2 Objects and object-oriented programming


The number and string variables that we worked with above represent data types that are built into Python. Variables can also represent other things, such as GIS datasets, tables, rows, and the geoprocessor that we saw earlier that can run tools. All of these things are objects that you use when you work with ArcGIS in Python.

In Python, everything is an object. All objects have:

  • a unique ID, or location in the computer’s memory;
  • a set of properties that describe the object;
  • a set of methods, or things, that the object can do.

One way to understand objects is to compare performing an operation in a procedural language (like FORTRAN) to performing the same operation in an object-oriented language. We'll pretend that we are writing a program to make a peanut butter and jelly sandwich. If we were to write the program in a procedural language, it would flow something like this:

  1. Go to the refrigerator and get the jelly and bread.
  2. Go to the cupboard and get the peanut butter.
  3. Take out two slices of bread.
  4. Open the jars.
  5. Get a knife.
  6. Put some peanut butter on the knife.
  7. etc.
  8. etc.

If we were to write the program in an object-oriented language, it might look like this:

  1. mySandwich = Sandwich.Make
  2. mySandwich.Bread = Wheat
  3. mySandwich.Add(PeanutButter)
  4. mySandwich.Add(Jelly)

In the object-oriented example, the bulk of the steps have been eliminated. The sandwich object "knows how" to build itself, given just a few pieces of information. This is an important feature of object-oriented languages known as encapsulation.

Notice that you can define the properties of the sandwich (like the bread type) and perform methods (remember that these are actions) on the sandwich, such as adding the peanut butter and jelly.