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:
- Go to the refrigerator and get the jelly and bread.
- Go to the cupboard and get the peanut butter.
- Take out two slices of bread.
- Open the jars.
- Get a knife.
- Put some peanut butter on the knife.
If we were to write the program in an object-oriented language, it might look like this:
- mySandwich = Sandwich.Make
- mySandwich.Bread = Wheat
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.