 and welcome to Python for Java coders part 11. In this lesson, we'll look at classes and how to set up a class structure in Python. So first, I'm gonna take a look over here at Java. Again, I'm assuming you already know how Java works. I have created a class called Animal, and as a public class, I have a public int, a protected double, and a private string. I also have a static int, and then I have the constructor here. Again, I'm assuming you know these things. If you don't, this is not the tutorial for you. And I have a setter, a getter, and I have this other method, that a static method is to say, and I have the two string method. And I'm gonna show you how to do the equivalent code over here on Python. So basically, if you take a look down here, this is the output we're expecting, animals, three different ones. I've created just some runner code here, and I have three animals, Lucky, Gizmo, and Bella. They're three of the dogs in my life. And just basically print out some information to show how to interact with that class. So taking a look over here in Python, I'm gonna go ahead and hide this. Now real quick, if you wanna add a file to Reploit, you just click Add File, and it'll pop up, you can rename it, all that sort of thing. So I'm gonna go ahead and leave this open. I'll slide this over to give ourselves a little bit of extra room, so we can kind of see what's going on. See if I can get this over here. And my computer's not cooperating, there we go. Okay, I have to leave this open a little bit because I gotta switch back and forth between main and animal. So I'm gonna go over here to animal, and I'm gonna start making my class. I'm gonna type class animal. Now something interesting, notice how when I named my file, I kept it in lowercase, and I'm pretty sure that's the preferred way to do this. You might wanna check on that one, I'm not sure on that one. I don't do a lot of class stuff, I don't do a lot of separate file stuff in Python, so some of this a little bit, maybe a little iffy, but this'll get you through. And then I'm gonna put some blank parentheses. Note, if you are using Python 2, which you shouldn't be, you'll have to put object in here. But otherwise in Python 3, which you should be on, we don't do that. Just like everything else in Python, we use colons, we don't use curly braces. And I'm gonna make my first variable, number of animals. Again, I'm using snake casing equals zero. Now notice how I skipped these three, okay? And this is one of the differences that you'll see here in Python. Is when I create my constructor, I'm gonna put all of this into the constructor. So my constructor is not named the same as the class. So this is a big difference. So I'm gonna type def, because it's a normal function, just like anything else, a normal method. I'm gonna type not one, but two underscores, i-n-i-t, and two underscores. I'm gonna put self, and I'll explain that in a moment, and name. And again, colon, and we use the indentation. So if you look over here, we've got the class name, but here we don't do that. We just put double underscore in it and double underscore afterwards, okay? This is the constructor, this is how you build a constructor in Python. So you can see here how we've got this.name equals name. So in Python, the equivalent is self, and I also have to put self here, okay? What this tells us is that this is not a static method. It is connected to an actual object, okay? So self.name equals name, okay? So that is the basic structure of our constructor. Now, as I mentioned earlier, we didn't have these things here, but we're gonna put them in here. So I'm gonna put int age. Again, we don't have to declare types in Python, but I do have to use self, self.age equals zero in this case. Self.weight equals, say zero plus zero, we don't know it yet. And self.name, we already done self.name, okay? Now, a couple things. I'm gonna put these in this order just to make it a little bit more clear. Self.age, notice over here it is public. In Python, if we wanna make something public, we don't do anything, it's just public by default. We don't have to worry about it. Protected, to make something protected, we put one underscore before the variable name. Very interesting. Nothing after, just one before. And private is going to be two underscores. That's how we know it is public, protected, or private. Now, Python's not quite as strict as Java. You could still technically access a protected variable from outside the class, but it's not a good idea. And here in private it will shoot up an error, I believe. Now, we have this here. Now, Java already knows that this is a static variable. So we don't have to do anything special here, we just say number of animals plus plus. Python's a little different. I'm gonna have to actually type the name of the class. And remember, there is no plus plus in Python, it's plus equals one. So that is the equivalent constructor and class variable set up we have in Java versus Python. So again, just to kind of review, the instance variables are gonna be attached to each object, go inside the constructor. The constructor does not take the name of the class. Takes two underscores before and two underscores after. And then if we wanna do something with a static variable, we declare it outside of the initializer, outside of the constructor, and then we use the class name. There may be other ways to do this, but this is the way that I know how to do it. So now let's take a look at our methods. So we have public void set weight double new weight. Now, Python, we don't have to add all the public private stuff, we're just gonna do this. We're just gonna do def, it's always define, set weight. Notice I use camel casing, I use underscores, or sorry, I use snake casing and underscores, not camel casing. And new weight colon, and I just say self.weight. Oops, weight is protected, so I have to underscore equals new, oops, I screwed it up today, weight. It's hard going back and forth. Now it would work again, it's just a convention thing. Then I wanna do my getter, get weight, there's nothing inside there. I'm going to return self.self is roughly equivalent to this, so probably if you think about it as this, it'll probably help you. Now in this case, we're working on the class variable, not the instance variable. So I'm gonna, oh, I did make a mistake there, sorry, I forgot about that. I have to put self in here. You must put self. If it is dealing with an instance variable or instance method, something that is attached to an object, we gotta use self. In this case, where it isn't, we're gonna say def and increment number of animals, and we put that blank. And then what I'm gonna say here is, same thing, animal dot number of animals plus equals one. Okay, so I'm just gonna go ahead and run that and see if anything happens, see if anything errors. Okay, so far so good. And then the final thing that I wanna do here is to straight. Again, I don't need public, I'm gonna define the method. And in Python, it's not called two string, this becomes no surprise. It's double underscore, S-T-R, double underscore. And I'm gonna type self. And it works exactly the same after that. I just return what I want it to be. So I'm gonna go ahead and copy this and change it. Okay, so I'm gonna return name and I'm gonna use the curly braces. And I'm going to use format, format. And again, here you have to remember to use self.double underscore name because it's private and self.single underscore because it is protected. I'm gonna run that just to make sure there's no errors, at least compilation time, compile time errors. Okay, that's good. All right, I'm happy with that. So now let's take a look at how we're going to interact with these. Before I do that real quick, notice how this method has a double underscore before and after, as does this. These are special methods that are kind of set into Python. So this tells you this is part of kind of the base Python, I guess, way it operates. I'm not quite sure how to put it. So if you see two before and two after, that is something that is from Python. Don't mess with that. Make sure you know what you're doing. So that is our class structure. And we've got our class variables. We've got our instance variables, public, protected, and private. And then we've got some methods. Any method that deals with the instances, you're gonna need self. You'll see an error if you don't use self. And another thing, I didn't do an example here, but if, for example, you wanted this to be protected, you would just put a single underscore before it. If you want it to be private, you would put a double underscore before it. So the same thing that goes for variables goes for methods, but we'll make those all public for now. Okay, now let's take a look at our runner code and take a look at our equivalent Java runner code. If I can get that back open, files. So we go to main and we start out with print. Animals, three different ones. Okay, now I'm gonna go ahead and I'm going to make my, I'm gonna make my objects. So I'm gonna say lucky equals animal, lucky. I'm gonna say gizmo equals animal, gizmo. And I'm gonna say bella equals animal. Let's go ahead and test this and see what happens. And we're gonna get an error and I'll tell you why in a second. Okay, so you see here it says animal is not defined. You know, in Java, if they're in the same folder, everything's gonna work out just fine. Python, it's not. We need to do, we need to actually import this. So keep in mind, the name of the file is animal.py. The class is animal with a capital letter. Okay, so here we use capitals. This is called Pascal casing and we do it that way. So what we gotta do is, before we do anything, we gotta type the following. From animal, okay, because this is animal.py, import animal. That's the capital letter class name. Okay, so it's the file name minus the dot py and then the class name. Now if I run it, I've got an error because I put a semicolon at the end of this thing. Okay, there we go, and format. Yeah, I think that'll fix it. So let's go over here. Okay, so now it's working, we're not getting any errors, which is what we like to see. So I'm gonna go ahead and copy this. Save a little time for everybody. I can't have weird indentation, it's gotta match. And of course it's set weight, set weight, and set weight. And then my next part is I'm gonna go ahead and print the number of animals, print, quote, number of animals, and I'm gonna do that format. And I'm gonna do animal dot number of animals. And what I'm just gonna do is I'm gonna do the same thing I did over here in Java. I'm just gonna print, lucky. I'm gonna print gizmo, I'm gonna print bella. Let's go ahead and test that. Okay, you can see we have basically the same output. So for the escape character there, so it's working exactly the same. And then the last thing is just to show you, basically this works the same in Python. So if I wanna increment the number of animals manually, I would say animal dot increment number of animals. And then I can just go ahead and copy that and print that out again. Well, that's probably not that useful, but it does illustrate the principle. So that is basically that. Structurally, they're very similar. But as you can see, there are a few key differences in the way the languages operate. In particular, with instance variables versus static variables and static methods and all that sort of thing. How you know something as static as far as methods is in Python at least, is you look for self. If it's self, you know it's probably not what we would consider static, because you need to have some sort of object passed along with that. But what's weird, and this confused me a lot, is you'll notice here lucky dot set weight 20.5. There's only one argument here. If I look over here, there's two. This self, and this is what confuses me even to this day sometimes, comes from here. This is the self, this is the second argument. First argument, second argument. First argument, second argument. Of course, Java actually probably makes a little bit more sense the way Java does it. So don't forget the self, that's one of the things I think people miss quite often when they make that transition. And yes, but I think otherwise everything works basically the same. And again, keep in mind that we don't use public private protected keywords, we use underscores, which is kind of weird. But it works pretty well, as I must say. So that is that, I think we'll stop there. And hopefully in a future lesson, we can continue on, talk a little bit about inheritance and a couple of other bit more advanced type things, okay? Have a great day, thanks for tuning in.