Defining Classes and Creating Objects in Python
#pyhton #oop #objectorientedprogramming In this lecture, we are going to dive into an exciting topic: defining classes and creating objects in Python. We'll explore how to define a class, understand the role of the __init__ constructor method, and see how we can create objects from a class. We'll also learn how to access attributes and methods of an object. To help bring this to life, we'll use a simple example of a Car class, similar to what we discussed earlier in the intro lecture. Let's begin with defining a class. In Python, a class is like a blueprint for creating objects. Classes group data and functions that belong together. Let's try this out. I'll create a new folder called 'class' inside our 'oop' folder. Then, I'll add a new Python file and name it 'car.py'. Now, inside this file, we define a simple class. The first step in defining a class is to use the 'class' keyword followed by the class name. Here, we've defined an empty class called Car. The pass keyword is just a placeholder, meaning we haven't added anything inside the class yet. This is like creating a blueprint, but without the details. Now, let's talk about the __init__ method. This is a special method in Python known as a constructor. It gets called automatically every time we create a new object from a class. We use it to initialize the object with some default values or attributes. Let's add an __init__ method to the Car class. The self keyword represents the instance of the class, allowing us to set values specific to the object we'll create. The make, model, and year are attributes that we can assign to each car object. By defining these attributes, we're giving each Car object its own identity. This class is now ready to use. However, if we run the script at this point it won't output anything because we haven't created any objects yet. So, next, let's create some objects from the class, which is known as instantiation. To create an object, we use the class name followed by parentheses. Let's instantiate two cars. Here, we've created two objects: car1 and car2. Each of these objects now has its own make, model, and year. Still if we run the script, no output will be shown, but now we have two objects stored in memory. Now, let's see how we can access their attributes. We can do this using dot notation. For example, let's print the make of car1 and car2. Now, at this point, if we run the code, The output will be something like this, showing the make of each car object we created. This shows us how we can access the attributes we defined in the __init__ method. Dot notation allows us to interact with the object's data. Now, let's take this a step further by adding a method inside the class that gives us a nice and formatted description of the car. Here's how we can do that: This line is where we define our method. The keyword def is used to define a function or method, and here, we're defining a method named describe. Every method in a class takes at least one parameter, which is typically named 'self'. This method returns a string that includes details about the car, like the year, make, and model, all combined in a single sentence. The self parameter allows the method to access the attributes of the specific object that called the method. It represents the instance of the class on which the method is being executed. In this case, self gives us access to the make, model, and year attributes for each car object. Now, let's go ahead and use this method on the car objects we created earlier. We can call the describe() method on each object like this: Note that the 'describe' method doesn't actually print anything by itself. Instead, it simply returns a string containing the description of the car. If we want to display this description on the screen, we need to explicitly print it using the print() function. Without calling print(), the returned string will just be stored in memory and won't be shown in the output. When we run the script, We'll see the description reported by each car. The describe method lets us print a nice description of each car using the data stored in the object. Each object now has both data (or attributes) and behavior (or methods). The power of adding methods like describe to a class is that it allows us to easily reuse logic that involves an object's data. Instead of manually writing out strings for each car object, we define a method once, and then every car object can use it. This keeps our code organized and clean, and avoids duplication. If we ever need to change how the description is formatted, we only have to update the describe method, and the changes will apply to all car objects automatically. To summarize, in this lecture, we learned how to define a class and the importance of the __init__ method for initializing objects. We created objects from the class and saw how to access their attributes using dot notation. Finally, we added methods to the class to perform actions. With these skills...
Download
1 formatsVideo Formats
Right-click 'Download' and select 'Save Link As' if the file opens in a new tab.