Introduction to Object-Oriented Programming in Python
This guide explains the fundamental concepts of Object-Oriented Programming (OOP) in Python, including classes, objects, attributes, behaviors, inheritance, and composition. It covers how to define classes, create instances, and manage data visibility.
In the world of programming, we often need to represent complex, real-world entities like cities, lakes, or geographic features. Traditional data structures like lists or dictionaries can hold data, but they struggle to capture the full essence of an entity—both what it is and what it does. This is where Object-Oriented Programming (OOP) comes in. OOP allows us to create digital models that mimic real-world entities by grouping their characteristics and actions together into a single, organized unit.
Object-Oriented Programming and Class Abstraction
Object-Oriented Programming (OOP) is a paradigm centered around "objects." Instead of focusing solely on the logic required to manipulate data, OOP focuses on the objects themselves. To do this effectively, we use a concept called class abstraction.
A Class is an abstraction of a type of real-world entity. Think of a class as a blueprint or a template. For example, if you are working with geospatial data, you might have a class called Lake. The class Lake isn't a specific lake you can visit; rather, it is a definition that says, "All lakes have an area, a volume, and a pH level, and they all have the ability to grow or shrink".
This diagram illustrates the concept of classes and objects in programming, using lakes as an example. A class, like 'Lake', acts as a blueprint defining attributes such as area and volume. Objects, such as Lake A, Lake B, and Lake C, are instances created from this blueprint, each with its own specific values for these attributes.
Objects and Instances
While a class is the blueprint, an Object (also called an Instance) is the actual thing built from that blueprint.
If Lake is the class, then Lake Mendota is an instance of that class. Every instance is a distinct entity. Even if two lakes have the exact same area and pH, they are still two different objects in your computer's memory.
Class Definition Syntax
In Python, we define a class using the class keyword. By convention, class names should start with a capital letter.
class City:
pass # 'pass' is a placeholder that does nothing
Instance Creation
Defining a class does not create any objects; it only tells Python what the blueprint looks like. To actually create an object, you perform instantiation using the following syntax:
# Creating instances of the City class
madison = City()
losangeles = City()
Exam Tip: Notice the difference in naming conventions. Classes typically use PascalCase (e.g., WaterWell), while instances (objects) use snake_case (e.g., water_well_1).
Attributes and Behaviors
Every object is defined by two main components: Attributes and Behaviors.
Attributes (Data): These are the properties or characteristics of the object. For a Point object, attributes might include latitude and longitude.
Behaviors (Methods): These are the actions the object can perform. In OOP, these functions are called methods. For example, a list object has a .sort() method to organize its contents.
Accessing Data with Dot Notation
In Python, we access an object's attributes and methods using dot notation. If you have an object named pt1, you access its latitude by writing pt1.latitude.
Assigning Data Attributes
You can assign values to an object's attributes directly:
While you can assign attributes manually, it is much more efficient to use a constructor. In Python, this is a special method named __init__. The constructor is automatically called when a new instance is created, allowing you to set up the object's initial state immediately.
class Point:
def __init__(self, lat, lon):
self.lat = float(lat)
self.lon = float(lon)
This diagram illustrates the concept of object-oriented programming, showing how a class definition (blueprint) is used to create an object instance with specific attributes.
Attribute Visibility: Public, Protected, and Private
Not all data within an object should be accessible to the outside world. Python uses naming conventions to signal how an attribute should be treated:
Public: The default. Any part of the program can read or change it.
Protected: Indicated by a single underscore (e.g., _name). This is a convention telling other programmers, "Please don't touch this unless you are a subclass".
Private: Indicated by a double underscore (e.g., __name). This makes the attribute harder to access from outside the class.
Advanced Concepts: Inheritance and Composition
Once you master basic classes, you can use two powerful design patterns to write even more efficient code: Inheritance and Composition.
Inheritance
Inheritance allows a new class (the subclass or child) to derive attributes and methods from an existing class (the base class or parent). This is perfect for GIS applications. For example, you might have a base class Feature that contains common attributes like area and center. You can then create subclasses like State or Parcel that inherit those properties but add their own unique ones, such as a state's capital.
Composition
Composition is the concept of nesting objects within other objects. Instead of saying a "State is a Polygon," you might say a "State has a Capital (which is a City object)". In this design, the State class is the composite, and the City class is the component.
This diagram illustrates the concepts of inheritance and composition in object-oriented programming, using a dog as an example. Inheritance, an "is-a" relationship, shows that a Dog is an Animal and inherits properties. Composition, a "has-a" relationship, shows that a Dog has Legs and a Brain as separate components.
By mastering these building blocks, you can transform complex geospatial datasets into organized, intuitive, and reusable code structures.
Keep reading for free.
Create an account to finish this reading and make it part of your study routine.