课程: Programming Concepts for Python

Objects

- 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 and 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 object. If I wear the 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, whereas a shirt has a size attribute for small, medium, or large. My shoes have a method to tie them, but it doesn't make sense 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, whereas the one I'm wearing is gray. If I change the attributes of one object like get my gray 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 does not 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. To demonstrate that, I'll open a new terminal within my code spaces environment by clicking the top left menu icon, selecting terminal, and then new terminal. Then within that new terminal, I'll use the Python command to start a new interactive Python shell. Now let's create a few objects starting with a string that simply says 'shirt.' When I press Enter to run that command, Python creates a new string object, and then the shell prints that string representation of the object, which in this case is just the word 'shirt.' To see the type of object that command created, we can use Python's type function on it. This line will create a string object that says 'shirt,' and then the type function will print out the data type of that object, so when I run that command, we 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 will have certain string related attributes that we can use. To see the attributes that belong to this string object, we can use the DIR function on it. This DIR function returns a list of the various attributes that belong to this string object. Some of these attributes are common things that all objects have, like the knit method and other attributes here are things that are specific to string type objects, like these split and swap case methods. We can use these various string methods on string objects. For example, let's use the upper method on a shirt string. The upper method creates and returns a new string object, which has the word 'shirt' in uppercase letters. Now, we can keep track of individual objects by looking at their IDs. Python assigns a unique ID number to each object it creates, which can be viewed with the ID function. If we ask for the ID of a string that says 'shirt,' we get back this sequence of numbers, and if we create a different string object, let's say a pair of pants, we 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 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 as being like nouns, and the objects 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 this 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 about them as verbs. They're actions that 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 this all sounds wild and confusing, fear not, there is a benefit to this 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 we can do with any object without having to worry about what specific type of object it is. For example, we can compare two objects, past objects as arguments to functions, or store objects inside of lists or dictionaries, which are themselves objects. The point is we can do all of these common things to functions, methods, strings, integers, lists, and dictionaries because they're all objects. The type of the 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 and makes life as a code maintainer much easier.

内容