1. What is Object-Oriented Programming
Welcome to this chapter on object-oriented programming, one of the most interesting and commonly used programming paradigms. We're going to cover the basic elements of object-oriented programming and show how it gets used in Python. Let's jump in!
2. What is object-oriented programming
Object-oriented programming is a style of programming in which the programs are organized by the concept of "objects", which can contain data and code. Objects are the basic unit of an object-oriented program and the mechanism by which they achieve modular code and separation of responsibilities.
3. What are classes?
Objects are further organized into categories called "classes", which contain all of the information and functionality that are shared among objects belonging to that class. It's important to note that unlike some other programming paradigms, these basic units of object-oriented programming (objects and classes) are not themselves processes, but rather they can contain code describing the processes they can perform.
4. Classes vs. objects
To make the distinction between classes and objects a little more concrete, consider the following example. A class is a category of object, such as the category "dog". The category of "dog" contains the possibility of many different dogs of different breeds and with different names. By contrast, an object is a specific example of a particular class, such as a particular dog named "Lacy". Lacy is a dog, and therefore has traits common to all dogs, but also has some specific characteristics that define her, such as the breed "poodle" and the name "Lacy".
5. Object-oriented programming in Python
Let's take a quick look at the syntax for object-oriented programming in Python, broken down piece-by-piece, as it can be a bit overwhelming compared to other programming paradigms.
The first piece of a basic object-oriented program in Python is the class definition. Here, we are defining a class called "Dog" using the "class" keyword in Python. Please note that class names in Python always begin with a capital letter by convention.
Inside the body of the class, we can define information and methods (which are similar to Python functions) that belong to all members of that class. If we define a method called double-underscore init, we can specify the information that must be provided when generating a new object of that class. In this case, we require a name to be provided for every new dog. Please note that this first argument "self" is required for every method we define within a given class, and refers to the object itself, once created. Here we have specified that each dog object will contain a "name" attribute equal to the value provided at its creation.
We can also define other methods for members of the dog class in a similar manner, as long as we provide this required "self" argument. Notice that we don't reference the "self" argument in the bark method at all, but we still must include it. Here we are specifying that any dog will have the ability to bark.
Finally, outside of the class definition, we can see the syntax for creating a new object of this class. Use the name of the class, provide any required arguments (in this case just the name of the dog), and save the output to a variable. Here we have created a dog named Lacy.
To execute some behavior of an object, such as having this dog Lacy bark, we simply call the method of the object using this syntax: lacy dot bark.
6. Let's practice!
That was a lot of information! Let's try some exercises to make sure we've understood the basics of object-oriented programming.