Join Barron Stone for an in-depth discussion in this video Clothing as objects, part of Programming Foundations: Real-World Examples.
- Many modern languages, including Java, C# and Python, implement a programming model called object-oriented programming, which is intended to give programmers a way to think about the various elements in their program, like objects in the real world. Rather than thinking about a program as a sequence of logic in actions, in an object-oriented programming language, we create virtual objects as the pieces of our program and then interact with and use those objects to accomplish a task. Object-oriented languages give us a way to structure and organize our code, that makes it easier to design and maintain large-scale applications.
I'll discuss many of the benefits of object-oriented programming throughout this chapter but first, let's start by asking the basic question, what is an object? Well, to understand what an object means, in terms of programming, start by thinking about what makes something an object in the real world. Now, imagine for a minute that this closet space represents my program and all of the various items of clothing are objects that I've created to use within my program. Object-oriented programmers typically think of objects as nouns, which are people, places or things.
If you can refer to something with words like, the or this, it's probably a noun and can be thought of as an object in your program. For example, this shirt is an object. All objects have attributes, which can be used to describe the object in its current condition. This red cotton shirt is clean. There were three attributes in that sentence, red, cotton and clean, which are adjectives that describe this shirt. Some of those attributes will remain constant for the life of the object. This shirt will always be cotton but there are other attributes that describe its condition that might change.
I'll probably get it dirty at some point. So, it'll no longer be clean. Now, in addition to having attributes, I can also do things with this shirt. The term method describes actions that an object can perform. These are verbs that could be applied to the shirt. I could wear it, stretch it, the shirt could fade, it might rip. Some of those methods might change certain attributes about an objection. If I wear this shirt long enough, then that might change it from clean to dirty. Other methods might enable this shirt to interact with other objects.
Different types of objects have different attributes and methods, depending on what they are. A pair of pants probably has attributes for waist size and circumference. Where as a shirt has a size attribute for small, medium or large. My shoes have a method to tie them but it doesn't make since for shirt or pants to have that same tie method. Objects can also contain other objects. For example, the outfit I'm wearing is an object which contains individual shirt, pants, and shoe objects and, just like in real life, we can have multiple, individual, instances of the same type of object.
The shirt I'm wearing and the red one from earlier, are two separate instances of a shirt, each with different attributes. This one's red, where as the one I'm wearing, is grey. If I change the attributes of one object, like get my grey shirt dirty, that's not going to affect the attributes of the red shirt. It's also possible to have multiple objects of the exact same type, with the exact same values. It's easy to see that even though these are identical red shirts, they are still two distinct objects. This way of thinking about real life objects as individual things with attributes that describe them and actions that they can perform, is how we should also think about objects in the programming sense.
Python doesn't have built-in types of objects, to represent clothing but it does have a variety of built-in objects for common data types like, numbers and strings. I have the Python interactive shell open through idle, where I'll create a few objects. I'll make a simple string that says shirt, by typing the word between to singe quote symbols and when I hit enter, Python creates a new string object and since I'm using the interactive shell, it'll display the string representation of that object, which, in this case, is just the word shirt.
I can see that the type of object that was just created is a string, by using the type function on it. This line will create a string object that says shirt and then the type function will print out the type of object that it is. So, when I hit enter you can see that this object belongs to the class str which is short for string. The fact that this object has the type string is important because that means it has certain string related attributes that we can use. To see the attributes that belong to this string object, I can use the dir function on it.
This function returns the list of the various attributes that belong to this string object. Some of these attributes are common things that all objects have like, the underscore, underscore, init method. Other objects here are things that are specific to string type objects like, the split and swap case methods. I can use the various methods on the string objects I create. For example, I can use the upper method on the shirt string, by typing .upper(). This upper method creates and returns a new string object, which has the word shirt in upper cased letters.
I can keep track of individual objects by looking at their ID's. Python assigns a unique ID number to each object it creates, which can be viewed with the ID function. When I ask for the ID of the string that says shirt, I get back this sequence of numbers and if I create a different string object, lets say a pair of pants, you can see that the pants string has a different ID than the shirt string. They are two separate objects. These concepts hold true for other data types too like, integers. In Python, even the number one, is an object with its own ID and its own set of attributes.
Just like with the string, some of these attributes are common to all types of objects and others are specific to integers. Near the beginning of this video, I made the statement that you can think about programming objects being like nouns and the objects that we've seen so far, are easy to think about that way because numbers and strings can be thought of as things but many object-oriented languages don't just limit objects to being things that represent data like, strings and numbers. A common phrase that you might hear people use when talking about Python, is that everything is an object in Python.
What that means, is that even functions and methods in Python are objects, with their own unique ID and set of attributes. For example, the ID function is an object with its own ID and the dir function is another object with its own set of attributes. It may feel weird to think of functions and methods as being objects too because we, typically, think of about them as verbs. Their actions our program can do but in an object-oriented language like, Python, those functions and methods are objects, in just the same way that strings and numbers are objects.
Now, if all of this is sounding crazy and confusing, fear not. There is a benefit to this crazy scheme of making everything an object. Since there are certain attributes that are common to all types of objects, that means there are certain things I can do with any object, without having to worry about what specific type of object it is. I can compare two objects, pass object as arguments to functions, or store objects inside of lists or dictionaries, which are themselves, objects. The point is, I can do all of these common things the functions, methods, strings, integers, lists and dictionaries because they're all objects.
The type of object doesn't matter. This flexibility to treat all objects in similar ways, lets us write robust programs that can be more resilient to changes, which makes life, as a code maintainer, much easier.
- Reusing functions
- Local vs. global variables
- Creating and naming custom objects
- Class inheritance
- Modules and packages
- Multidimensional lists and tuples
- Queues and stacks
- Creating and combining sets
- Storing data in dictionaries
- If/else and switch statements
- For vs. while loops
- Error handling
- Polling and event-driven programming