Python Object Programming And Interaction | Heaplevel | Skillshare

Playback Speed


  • 0.5x
  • 1x (Normal)
  • 1.25x
  • 1.5x
  • 2x

Python Object Programming And Interaction

teacher avatar Heaplevel, Programming Coach / Software Engineer

Watch this class and thousands more

Get unlimited access to every class
Taught by industry leaders & working professionals
Topics include illustration, design, photography, and more

Watch this class and thousands more

Get unlimited access to every class
Taught by industry leaders & working professionals
Topics include illustration, design, photography, and more

Lessons in This Class

8 Lessons (37m)
    • 1. Welcome

      1:43
    • 2. Intro - What You Need To Know

      3:41
    • 3. Just Playing Around With Objects

      7:09
    • 4. Our First Look At A Class Definition

      2:27
    • 5. The Constructor And Visualising Our Objects

      6:05
    • 6. What To Think About When Writing A Constructor

      8:38
    • 7. Adding Behaviour To Objects

      6:27
    • 8. Summary And The Next Step

      1:10
  • --
  • Beginner level
  • Intermediate level
  • Advanced level
  • All levels
  • Beg/Int level
  • Int/Adv level

Community Generated

The level is determined by a majority opinion of students who have reviewed this class. The teacher's recommendation is shown until at least 5 student responses are collected.

20

Students

--

Projects

About This Class

Python is really easy to just get started with, but what if you want to take the next step and learn object-oriented programming (OOP) too?

In this class we use the Python programming language to learn about the world around us "by the object way of thinking". 

This class will begin to teach you the basics of objects and classes - from the ground up. You will learn what a class is and does, also how objects relate to a class. 

This course is perfect for you if you have finished your Python introductory course and you're now curious to learn about object-oriented programming.

Meet Your Teacher

Teacher Profile Image

Heaplevel

Programming Coach / Software Engineer

Teacher

Hello, I'm Haider and I am the founder of Heaplevel. I've been working in the software industry for about a decade and gained a lot of different perspective on software development.

 

When I first started programming I was a beginner, like most of us. It was very unfamiliar and abstract to grasp the concepts. During the course of my career I've learned what is important to teach and what made me actually understand how to program. 

 

I hope this can come to use for your programming journey too. 

See full profile

Class Ratings

Expectations Met?
  • Exceeded!
    0%
  • Yes
    0%
  • Somewhat
    0%
  • Not really
    0%
Reviews Archive

In October 2018, we updated our review system to improve the way we collect feedback. Below are the reviews written before that update.

Why Join Skillshare?

Take award-winning Skillshare Original Classes

Each class has short lessons, hands-on projects

Your membership supports Skillshare teachers

Learn From Anywhere

Take classes on the go with the Skillshare app. Stream or download to watch on the plane, the subway, or wherever you learn best.

Transcripts

1. Welcome: Hello? Do you know when this and this have in common? They can both be written as a class in. This is exactly what this course is about, right? Their own class definitions. This is the perfect course for you. If you've already finished. You were Python introductory course. And now you're ready to write your own classes and learn about object oriented programming . I'm a software engineer, and I've worked in the industry for about 10 years. But I also started out where you are now in trying to understand object oriented programming and what it's all about. So that's why I created this course because I know what type off hurdles and abstract concepts we need to on dissect and take it step by step. So in this course, we're gonna cover some ways on directing with objects and understanding class definitions and obviously also how to write your own. I believe in learning. By doing so in the project, I'm not gonna ask you to only write one class but several different classes because that's the only way to get comfortable with this concept. So welcome to the course. And I hope you enjoy it. Thank you. 2. Intro - What You Need To Know: hello and welcome to object interaction with fightin. This is the perfect course for you if you've already finished your introduction to Python, and now you think it's time to learn more about object oriented programming and level up your programming skills. So here's what you need to know in order to be successful in its course. And first of all, we assume some python skill, some knowledge about vita and functions, and we also require that you understand valuables and how to use them in. You would learn the basics about classes and objects and how to use them, but also when to use them. And we would also have time to write some code for class and objects to see how we can build Ah on their own some basic objects. So just a short exercise before we continue. Think about objects that you have in your room or where you're sitting. Anything that comes into mind and hold on to that, and we will return to that in the next lesson. So we feel, for example, think about a car. What type off brand is it and how fast can you go? For example, another property might be the location of the car or think off ice cream. What could you say about a nice game? How would you describe in ice cream by the taste of it? By the color of it? Is it warm? It's a cold or a tree, he said. A tall tree doesn't have a lot of branches leaves. You get the idea to think about a couple of objects and write down their properties, cause we would use this in the next lesson and we will demonstrate and how you can use that for writing your own classes and also objects. So give yourself a couple of minutes just to record some points about obvious and their properties, and it's OK, you can post the video, and when you're done, just hit Play Great won't come back. I hope you by now have a couple off objects and properties written down. And for the next lesson, I'm gonna demonstrate how we can use objects in python and also how they appear so follow along 3. Just Playing Around With Objects: all right. So let's start our journey with Pilon object by introducing our 1st 1 and for this have selected a picked a very simple object that it's intuitive to understand and that is the square. And what I have open here is an pipes and scratch fight that I'm going to demonstrate the usage off square object. So the first thing we're gonna do his create our own valuable and according to my square and I'm doing these very much step by step here. But I've prepared a class called Square and basically what I've done here is called my own class square and assigned it to the left hand side. It is valuable. No. What is a class? What is an object? So square basically defines template and my square is one concrete example off a square. So if you recall that when we talked about trees and ice creams and the cars, when you talk about a particular thing, we talk about origins. When you talk about a category that is the class when we get back to that later, but did see now what we can do with our variable and what is to find in there. So I'm going to do this again. My square Not and as you have two different properties here, size and color. And if I would just type that and try to print Okay, so now I have something called my square that size, which prints out 10 or returns to him size Here is an attribute, just like in the reward. If you would cut out a box or a square of something, you could define that size. And now in the pie Tomorrow we have this same type of property called size, and I do that. I recall it by stopping the variable name. Don't the attributes. Let's see the other one. Okay, none. Well, we're gonna see why that is not in second. But the idea here is we have our own valuable, my square. And on that there is several attributes which we can use and learn about. And this object, right? So before we create the second object, why was color none? Well, the answer to that is for the same reason size has the value town and the secret behind that is something we'll look at in the next lesson. But that is something already defined in this factory how new values will be selected or assigned to any new order. Now, let's create a 2nd 1 I'm gonna quite my second square and the same syntax here. Now, what would happen if I print the same things? Okay, 10 non 10. And now? So these objects seem to print out the same type of values for these properties, and this goes back to what a class really is. It is just a template. It's a factory from which we can create objects and each object that come out from that factor will look the same, however, or did they copies off each other or the clothes, or are they just separate with different values? Like if he would cut out paper and create two different squares with same size, they would still be two different squares. Right? Okay, so let's print out the I d off these squares. This one ends in 16. This one hand since 60. So they are different internally, yet they have the same type of attributes. We're going to see why that is in the next video. Okay, in the next lesson, we're gonna take a look at what goes inside a class and how to customized the creation often object. We look at something called constructors and some of the building blocks off partner filed that contains class. 4. Our First Look At A Class Definition: partner. Fine that could take. The class always starts with the last name which in this case is square and it's followed by the cure class. Second opponent is constructor. It is not manager to have it, but it's a good way to explicitly identify and tell what this class looks like or worth Objects of this class will look like so this in it. Here it's a constructor and it's also called the Magic method because it's a built in in the python. And after the constructor we have another set of optional methods and I'm just demonstrated here as as TR String, which is the built in part in it as well and an optional method that I've done defined myself. No, it doesn't attain anything. But for the illustration purposes, this is what the basic anatomy of a class could look like. Now you see in here between the parentheses, this is key were called self. No. For now, it's just important. No, it has to be there. And then later lessons. We can explain how the mechanisms of self work okay, so python doesn't really put a limited number of classes in your fine, which means I could just write this over again, for example, with a different name. Could have a triangle class and was anything in the constructor and any other option of method. But we're not gonna do that in this lesson. So I'm gonna delete this, but just go to know it doesn't really put a limit. 5. The Constructor And Visualising Our Objects: So what is the constructor and was the purpose of it? We see here non in 10 which is the same values we saw in the previous lesson. And the constructor is used when we create an object in the pilot program. And the constructed just blinded us, whatever it seas. And when I write Ah square like this, I'm calling the class and creating an object. And I can do this several times. It doesn't really matter. I'm creating 12 345 different objects and I could not to run this, but that's you know, And if you remember, all these are just left and disappear after the program. These are called instance variables or attribute instances. And they define the behavior or the state off an object at any point in time. So didn't demonstrate that again. Say, like, have my say I want to create a Red Square on I also want to create a blue square. Now, if I would print this out and two point Luke object, I would see none and none to fix that I could do like this and also like this. Okay. And print again and see that I have the value spread and blue. So this is interesting to note. I've called the score class twice. Assign that to this variable, then have taken the instance attributes and assign it and value a text value. And we saw that printing each object. It's unique, so the restaurant at color would not affect the blues, credit, color or vice of ourselves. Okay, so one of these squares we talk about here we have the Red square with Blue Square and we change the value of the attribute instance. And how do they really relate to each other and through class diagrams? And the class diagram is your visualization off any type of class and usually what you have is rectangular or sometimes canon of box. And within that box you have all the attributes belonging to in that class. Now, let us see how this relates to the objects. What we have is now class diagram and object diagrams so we could see that a square has the number off attributes and objects have the same axe boots, but their values are different. They are unique to every object Instance. So when we're when we create a new square the object with automatic have the those variables or these attributes and the values of these are stored in the object. So what? This means that we will always be sure of that. Every object is independent and if one is deleted, the other is not okay. So the constructor then will take care off the assignment off these values and basically creating the object for us. Now we can customize the constructor, which we will see later. But for now, it's good to see the constructor as the ignition to our objects. So whenever where we call the class, the constructor is called and our object is created and return to us for storage in variable, for example. 6. What To Think About When Writing A Constructor: okay, we'll come back. And now we're gonna talk about writing Constructor. And what's the best way to actually write the constructor against That is not really. It's a silver bullet, but here's a couple of things that might be helpful when writing class. So how will I use this class? And what do I want to represent with a class? And as we know the relationship between the class and constructors? Quite. You know, close as the constructor is what determines how an object will be formed or looked like when class is called. You hear a couple of things to consider when writing your class and also constructor. Okay, so here's the first thing to consider the environment hand how it relates to. So let's say we are focusing on cars and in this video how we can, for example, imagine we have ah type of van or SUV, and it has some characteristics which might be different from a racing car. But if Elmo if I only want to focus on the general concept of a car, then I will probably want to moderate in Python by saying What's the brand of the car and say the Max speed of the car. So let's say I want to write that in Python. How would I actually do that? And what would I need to think about when typing the constructive? So let's say I have the for example hand. We know that we can write the class by typing the word class followed by the name and the constructor is written like this to indicate department. I just don't want to do anything right now. I can type pass. And now I have my first and simple definition of a court last within constructed that is empty. I'm gonna display of this role. The comment. Everything below here is what we might call the outside. Basically, this is where we call our class. And every time we call her class, the result would be a new object. So if I would just her this I'm calling my glass and I want to say that, and I could do that by saying like, car number one now say that I want to tell that every new car should have a name for a brand, and the way I could support that is by a couple different ways. But here I can support in a constructor by adding and new arguments saying brand And then I can assign that to my own valuable. And just to be clear here, that's named this one in brand indicating this one would be provided from the outside here . So I want to use this. And the way I do it is between the princess's I type in the value off the brand. So in this case, I'm indicated that I want a new object, please. And you can start on initiated with the value Mercedes for its print. And now we can try to Prentice see what happens. See, so one today. Here's this, Brent. Well, the intention here is I would just want to model car with the concept of the car in my by then program. And the only thing that sufficient for me now is to keep track of the brand. No. How would I think about if I say have a program that I want to track each card that I have in my program? So, for example, I might have different cars in my system, and I'm tracking them with some location, and in this case, it would make sense to have something for you. Position and the way I do this is to just explain this. And I had a new argument like this. What I'm saying here that in my program where I have these model off the car, I'm just I just want to catch two things. I just want to capture the position of a car and the brand. And this is enough for me, for example, in another program, say I have Ah, maybe I'm writing in game. In that case, I want to probably keep track off many different things like Max speed, the current speed and other different attributes. But for this purpose, we just want to illustrate how we can think about this constructor and how it can help us. So when I've ended two arguments, I've been forced a contract, saying that every time called the class, I need to provide two arguments so this one would fail, and states here are missing one required position argument, and the position is something could just make up saying right now it seemed like longer and try to run that again, and it works. So this is just a simple way off explaining how you can use the constructor to and just hand that you were different object that you create. Likewise, say, like, I have a second Carmen program and it's also a Mercedes, but it's actually down in Bristol and from whatever you else want to do in your program. And that is also enough for this video. I hope you I learned that in order to write a constructor, it's just not one way you can actually decide for yourself how many different arguments you want in a constructor and the way you want to model. You were concept. So the main team points in this video is that look at the object that you have and how do you actually want to use that in your price and program? What will be the boundaries, what will be the context and from their own? You can then start to design your classes and the construct 7. Adding Behaviour To Objects: car. I won't come back This video, we're gonna look changing the behavior often object. And the way we do that is by changing the state often object. So you learn how to basically write your own method that can change the, uh, instances behavior. And we complete our introduction to this journey off objects by adding methods to our class definition. Okay, So, reviewing our car example, we now have the intention off simulating the environment off a car, speeding up or breaking. And we want to do that by using a new method. And the current state Now, for these core one and cartu is the values we have for Brandon position so far is that the Koran? Its status to find by Mercedes and London and court to Mercedes in Bristol. So there you have the same values or in the same attributes. But values are different. No. If you remember, we mentioned that the class definition consists of a class name, option constructor, and then option set of methods. Now, my intention here is that I want to actually increase this state and say that every car should also have a current speed. And this is supportive for me cause I want to actually keep track off and the car so speak . And I'm gonna do that by passing a new argument called current speed and assign it to a new and incense attributes. Now have a new contract here saying that I have three arguments. This also means I need to change class or the call to the class here. And I'm gonna do that by saying this car one should start at speed is zero and this one, we'll start it. It's on video out somewhere. So it's my 80. So this is just for the purpose off, uh, demonstration. We can have the representation in kilometers or MPH. It doesn't really matter for this example, So I'm gonna correct this one as well. In current speed, we will be assigned to the self current speed attribute. So now we have objects that are Chris is or has to be attributes and notice. I didn't have to, like, recreated the objects. So the next step now is to create a new method. And I won't assimilate something called a speed up. So I write the method enter and then I'm going to simulate this policy that every time I called the current speed that well, increase the value by five. Let's see what I've done here of taking the current value increases by fire and assigned it to the current speed attributes. And then we can also to a demonstration of how this method works. So you take the car one object dot we see that we have speed up here, which is available for us. I'm going to simulate this also by printing the current speed before calling speed up. And then I'm going to demonstrate it, but calling it afterwards and see what will be the event use. We see her. We have 1st 0 in the five, which correctly reflects our intentionally that in my game or program. When speed up is called, I'm changing the current speed off the car one. So this is unique for every object. And if I would go ahead and type this for the second object cartu, I've not affected that speed and this also goes back to every object, has its own states. We have the same attributes which are brand position and current speed. However, their values is what makes the objects unique. Okay, so let's do another one. Have a new medical self. Oh, are you sort of have new medical break and that one will do exactly the same thing. But it would just decrease the speed on this could be like some I know One doesn't have to be five. It's up to my intention as a programmer. Cand I only got you to test this out. And I hope you have enjoyed this and learned that in order to create home jigs, we can create there methods and basically rewrite their behavior or change their the state by introducing this behavior. 8. Summary And The Next Step: Okay, so let's summarize what we've learned so far in this scores. We've have looked at classes and discussed what they are, and we also spoke about objects and how they relate to the class and all totally relate to other objects. We saw how we can write a constructor and how it relates to the class and the object as well. And we looked at arguments to the constructor, and every object has its own method. So now we have a small tool set that we can use to be their own classes, and it's up to your imagination. Next step IHS that project, and then I'm gonna ask you to come up with different concepts and model them in your own classes. So good luck.