Python Class - C11L1 (Dec. 2018)

Profile photo for Scott Leffler
Not Yet Rated
0:00
Elearning
131
0

Description

This is an explainer voiceover that I recorded as part of an ongoing IT class I've been recording for a client.

Vocal Characteristics

Language

English

Voice Age

Middle Aged (35-54)

Accents

North American (General)

Transcript

Note: Transcripts are generated using speech recognition software and may contain errors.
so far are object oriented programming. Efforts have simply defined and used python objects at the class level. We use the name of the class to access the variables and methods on that class. For example, if we defined a boat class with a class variable life vests and a class function called Horn, we would write boat, not life vests or boat dot horn to use that variable or function. When you define things that a class level, you're saying, this is the kind of data and behavior that all boat objects will support. So classes like a template that you can use to create multiple objects with the same properties and methods As you can imagine, a program may want to create more than one copy of a class. After all, a tour company or a cruise line is likely to have more than one boat, so a program written and manage their information would want to use more than one boat object. Each copy of the boat object should have a different name, and other information and instance, is a copy of a class that's created within your program. Each instance or copy can hold its own unique data. You may want one boat object to have a color of red and another blue, and they'll all certainly have different registration numbers to create. An instance of our boat class will write a variable name and set that equals to the class name, followed by opening and closing parentheses. We can create a second instance in the same way, just giving our second boat a different variable name. Now we have two instances of our boat class. An object can hold data in two different kinds of variables. Class variables and instance variables. So far, you've only used class variables, which are always accessed by writing the class name than a dot than the variable name. It's important to understand that the class variables can only hold a single value at a time, no matter how many copies or instances off the class or created. Our boat class has a class variable that holds the number of life vests for the boat. Any time we use this class variable by typing boat dot life vests will get the value for So we're saying that all boats have four life vests. But what if we want to create a new variable, like a color value that's different for each boat. We can do this using instance, variables and instance. Variable is access to using the variable name instead of the class name. In our case, we can set the color for boat to by typing. Boat to dot color equals blue. After the variables initialized, we can read the current value for boat to by typing boat to dot color. Notice that we did not have to define the color variable inside our class instead, instance, variables are automatically created the first time you initialize them in your code so we could add any number of instance variable to our class from within our main program body. We just use the instance name a dot the new variable name and then a value for this variable. These instance variables belonged just to this copy or instance of the class, but one would not have any of this data unless we explicitly added it to boat one. Also, you'll need to remember you cannot use a class or instance variable that's not been initialized in your code. Let's say we decided that our boats will start using a name instance, variable. If we just tried to print boat one dot name in our code before assigning any value, we'd cause an error. This is because we forgot to initialize this value before using it. Before we try to read a variable. We need to set that variable to some value. Once we do that, we can then safely read the variable and do something useful with the information.