Join Barron Stone for an in-depth discussion in this video Naming objects, part of Programming Fundamentals in the Real World.
- As we create and use objects in our programs, we need a way to refer to them. And this can be accomplished by binding names to those objects. Take this shirt for example. It's a solid color so I'm going to use the name red shirt to refer to it. If I'm wearing the red shirt, that means I'm wearing this specific object. Now red shirt itself is not an object, red shirt is just a name that I use to refer to this specific object. And just like in real life, it's possible to have multiple names for the same object. - Ah, your crimson shirt, I like it! - As you can see, Olivia has a different name for my shirt.
- It's a crimson shirt. - I'm not sure what crimson is, so I call this my red shirt. We have two different names that we use to refer to the same object but there is only this one shirt. That means if I spill juice on the red shirt... - The crimson shirt is gross and sticky. And if I decide to toss the crimson shirt into the washing machine... - Then the red shirt will get cleaned. Regardless of which name we use to reference this shirt object, we're both using and modifying the exact same thing. When we use one of those names to change the object, then that change will be reflected in any other names that are bound to it.
Ah, I like your style. - I got my own crimson shirt. - And I still have my red shirt. Now we're wearing identical shirts. These two shirts are equal but they're not the same. And this is an important distinction because we can have two shirt objects which have the same attributes but they're two separate and unique objects which are referred to by different names. So, if I spill juice on my red shirt... Oh! - And I toss my crimson shirt into the washing machine... - The red shirt is still gross and sticky.
Since we had two separate objects which we referred to by different names, when Olivia washed the crimson shirt, it had no effect on the red shirt. This example script called start_02_03_naming_shirts from the exercise files contains the class definition for a basic shirt object. Whenever I create a new shirt, the init method will initialize the clean attribute to True because a brand new shirt should be clean when I get it. There are methods to virtually spill juice on a shirt and to wash it, make_dirty, and make_clean which each change the value of the clean attribute accordingly.
I'll run the script to load the class into the Python shell so I can use it to create a new shirt object. To start, I'll create a new shirt and give it the name red for short by using the shirt constructor method. The shirt constructor method created and returned a new shirt object which was then bound to the name red. The name red does not contain the shirt. It's just the name that points to the object I just created. The equal sign operator takes any object on the right side which can be an existing object or the output from a function and it binds that object to the name on the left side.
Next, I'll give the shirt named red a second name, crimson. And I can do that by typing crimson = red. Now, if I pass red and crimson to the id function, I can see that they are the exact same object because they have the exact same id number. The names red and crimson are both bound to the same shirt object. If I type red.clean to check the status of the red shirt, I can see that it's clean and the same thing goes for the crimson shirt.
It's also clean. Now, I'll use the make_dirty method to spill some juice on the red shirt because I'm clumsy. And then if I check the status of the clean attribute, it's False. And as you might expect, the crimson shirt is also dirty now since it's the exact same object. I can check to see whether or not two different names refer to the same object by using the is operator which will return True or False depending on whether or not the two objects have the same id. So in this case, if I type red is crimson, I get back True because they refer to the same object.
Now, I'll create a new shirt object for Olivia by using the shirt constructor method again and bind it to the name crimson. Now if I check the id's of the red shirt and the crimson shirt again, each name refers to a different object. Since I created a new shirt object to bind to the name crimson, it'll start out clean but the red shirt from earlier will still be dirty. I don't like having gross, dirty shirts so I'll use the make_clean method on the red shirt to clean it up. Since the shirts are two separate objects, the make_clean method only worked on the red shirt but the crimson shirt was already clean so now I have two clean shirts.
However, even though the shirts have the same value for the clean attribute, if I compare them with the is operator, it returns False because now those two names refer to different objects.
- 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