Python OOP Tutorial (Object Orientated Programming ) #2 - Creating Classes
Key Takeaways
This video tutorial by Tech With Tim covers the basics of creating classes in Python, including initializing attributes, calling methods, and using inheritance to create subclasses. The tutorial provides a comprehensive introduction to object-oriented programming in Python, covering key concepts such as classes, objects, methods, and attributes.
Full Transcript
hey guys and welcome back to the second video in this object and class tutorial series in this video we're going to be going over creating your own objects and your own classes and what that looks like in Python so we'll just go ahead and delete this code from the last video and start fresh so if you guys remember the last video we talked all about objects to talk to methods and that's all cool we kind of already understood a lot of that stuff like we've used those methods it makes sense how they work but now how do we create our own and this is the whole goal of this tutorial series to create your own classes and your own objects and add functionality to your program so to create a new class and this is going to be a new like datatype something like that we'll talk about that in a second you just simply type class and then whatever you want the name of your class to be so in Python they have into string the list whatever okay we're gonna create a class and I'm just going to call it dog now you probably have seen this example before if you do anything what do you call it with like classes or objects because people like to use animals but it's a really good example so this is now creating a new class of dog now what we have to do whenever we create a class is we have to add some methods into it so remember I talked to a methods or things that you call on the actual class itself so I'm just gonna type define underscore underscore anit underscore underscore and we're gonna talk about what this does in a second and make sure you don't forget this self keyword this object keyword you can leave out if you want I just like to type it because I don't know it just a habit right because we have class dog define an it and I'm gonna make one more method and we're gonna call it speak okay and make sure you don't forget this self key we're going to talk about what all this does in a second okay pass and in here I'm gonna say pass alright so we've now just created our own class it does not do anything but we've created our own class so what we actually do here is we're creating well new costs equal to dog inherits from object don't worry about that and this is what's known as the constructor method now this underscore underscore init underscore underscore needs to be in most of your classes okay so if you guys want anything to happen initially when you first create a class you need to make this a knit class and what happens is when we create a new object and the way we can do this is by doing something like I don't know Tim equals dog like that this method is automatically gonna fire it's automatically gonna go off we don't have to say Tim dot underscore underscore name underscore underscore we don't have to do that whenever we create a new object of type dog this is automatically going to happen so for example if every time I created a new object of dog I wanted to print out nice you made a dog like this then every time that we do this so it's like Fred equals dog as well then it's gonna print this to the screen and I can prove this to you by simply running okay so he says nice you made a dog nice - you made a dog alright we didn't have to call this a net it just automatically happened whenever we created this dog so let's talk more about this in net and what we can do with it so in classes there's things called attributes and methods all right methods are anything that you create using define okay and they look just like functions except you have to call them using an object and like we talked about in the last video then attributes are kind of like variables that belong to a certain object so to create a attributes you need to use this self keyword alright what this self keyword actually stands for is it stands for the instance that you're calling so Tim is known as an instance of type dog worth class dog now fred is another instance of type dog now I'm just gonna do this and then hopefully you guys can understand how this kind of works because it is somewhat difficult to explain okay so now what we're saying is we've created this initialization all right and we have now that it takes a name okay so what that actually means is we have to now type a name into our initialization so whenever we create a dog we have to give it a name so in here I'm gonna give it a name Tim and in here I'm gonna give it a name Fred all right and don't worry if you hasn't confused I'm gonna explain this all in depth in a second okay and I'm just gonna do this so that we can actually see what our name is print hi I am kama self dot name and we'll talk about what self does in a second okay Steve Tim dot speak and Fred does speak as well okay so let's print this and he says hi I'm Tim hi I'm Fred okay so how does this work so in this initialization this fires automatically when we create doc okay so by calling this we're automatically calling a knit so if we put a parameter in an int so name that means that we now have to pass a parameter when we create our dog and just like a function we can create multiple parameters we do like name age color kind we can do as many as we want okay and that works fine now what does this self do so this self actually represents the instance right so if I'm if I call dog equals Tim or dog Tim whatever and I set that equal to Tim well Tim is what's being passed into this self parameter here and notice how we have two parameters but we only pass one thing that's because self always needs to be here except in like really specific cases so that we're going to talk about later so when we call self dot name we're saying that Tim dot name is equal to whatever name we put in here and that's the same thing with Fred right so when we call it on Fred then we say well Fred dot name is equal to Fred all right if we didn't do that and we said something like name equals name if I try to print self-doubt would just crash it wouldn't work because we don't know which instance has what name okay so this self means belong that kind of means like belongs to the instance that you're calling on I'm trying to explain this as good as I can because you you really understand this I'm gonna do another parameter here to Qasim self done age equals page all right and let's just print this out and I am self-taught age years old right okay so let's print this out and see what we get missing one age sir I'm gonna put the page in here fifty-five three okay so now if I present say hi I'm Tim and I'm 55 years old hi I'm I I am Fred and I'm three years old okay so each of these are instances of class dog and they each have a name and an age and you're able to call the speak method on them now what this speak method does is notice how when I call speak it automatically takes self because it has to know what instance I'm calling on and that way it's able to access the name and the H now in here you can actually change the name in the H so I'm gonna create a new method I'm gonna say change underscore age like this okay and notice how it takes self no matter what it automatically types it in for me and here I'm going to do age all right now what I can do is I can say self done age is equal to H and if I call let's see here Tim done change age and I give it something like five now if I speak you can see that we get hi I'm Tim and I'm five years old and we say hi I'm Fred and I am three years old now obviously you guys can create as many methods as you want you can do call them whatever you want just make sure whenever you create them they first of all have self because we have to know what instance we're calling it on so we can access those attributes all right and you can change these attributes however you like within those methods now I will show you what else you can do okay so say you wanted to access Tim's H and you didn't want to print out like hi I am all this you didn't want to speak you just want to know what his ages well what you can actually do is you can simply print Tim dot H and now you can see if I run the program we get five down at the bottom and you can do the same thing with name so Tim dot name and it gives us Tim like that so we're actually able to access the attributes of our object from just simply calling whatever that attribute is and in this case its name so whenever you want something to be like public to the instance so that they can see it through it all the methods you need to simply you need to do self dot and whatever you want to call it so I could say like self dot I don't know let's say Li it's gonna stand for a list you can have a list of like one three four and now if I call Tim Li you can see we get one three four and obviously if I call friend Li well this is gonna work as well we get one three four now the main benefit of classes is that you can create things you can create multiple objects of a class so if I wanted to store like the name the age and a list for 300 dog objects if I wasn't using classes what I would have to do is that have to do like dog one name equals Tim and then I'd have to do dog one age equals five and then I do dog two H equals Fred and you'd have to continually keep doing that with a ton of variables and obviously you don't want that so these classes allow you to create infinite amount of objects of that type class and have all of these properties and attributes apply to them okay so what else should we talk about here maybe creating new attributes okay so attributes are anything to have this excel Fame self that age left at li whatever and methods are with these defined keys okay so if I create a new one and I say define and add what do you call it wait okay say add wait I'm gonna give it a weight and then we can say self dot weight is equal to weight and what this does now is it creates a new instance attribute that is going to be applied to this dog class now you see it's giving us an error so this instance attribute weight to find outside of an it that's because it wants us to define that inside of this initialization but it's fine you can actually do it out here so now if I do Tim dot add weight I give it a weight of like 70 okay and I'll print send out weight like here let's see what we get we get 70 okay now what if I try to print fred' dog weight well let's see what happens we get Fred has no attribute weight our dog object has no attribute weight and that's because we have not yet added this by calling add weight on our instance Fred okay so I think I'm going to cut the video off here hopefully you guys have a basic understanding of how classes worked it is kind of difficult to explain it if you guys have any questions leave them down below cuz I feel like I might have butchered the explanation on some of it other than that in next video we're gonna go over I think inheritance and creating some more advanced methods and talking about how those work okay so if you guys enjoyed please make sure you leave a like and subscribe and I'll see you again in the next one [Music]
Original Description
In this Python Object-Oriented Tutorial, we will be learning how to create and use classes within Python. Classes allow us to group methods and information together in a way that is easy to use.
WEBSITE: https://techwithtim.net/tutorials/python-programming/classes-objects-in-python/creating-classes/
**************************************************************
Support the Channel: https://www.patreon.com/techwithtim
Twitter: https://twitter.com/TechWithTimm
Join my discord server: https://discord.gg/pr2k55t
**************************************************************
Please leave a LIKE and SUBSCRIBE for more content!
Tags:
- Tech With Tim
- Python Tutorials
- Classes in python
- Objects in Python
- Classes and Objects python
- Classes tutorial Python
Watch on YouTube ↗
(saves to browser)
Sign in to unlock AI tutor explanation · ⚡30
Playlist
Uploads from Tech With Tim · Tech With Tim · 0 of 60
← Previous
Next →
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
A* Path Finding Algorithm(Visualization)
Tech With Tim
Python Programming Tutorial #1 - Variables and Data Types
Tech With Tim
Python Programming Tutorial #2 - Basic Operators and Input
Tech With Tim
Python Programming Tutorial #3 - Conditions
Tech With Tim
Python Programming Tutorial #4 - IF/ELIF/ELSE
Tech With Tim
Python Programming Tutorial #5 - Chained Conditionals and Nested Statements
Tech With Tim
Python Programming Tutorial #6 - For Loops
Tech With Tim
Python Programming Tutorial #7 - While Loops
Tech With Tim
Python Programming Tutorial #8 - Lists and Tuples
Tech With Tim
Python Programming Tutorial #9 - Iteration by Item (For Loops Continued...)
Tech With Tim
Python Programming Tutorial #10 - String Methods
Tech With Tim
How to Overclock a NVIDIA GPU
Tech With Tim
Python Programming Tutorial #11 - Slice Operator
Tech With Tim
Python Programming Tutorial #12 - Functions
Tech With Tim
Python Programming Tutorial #13 - How to Read a Text File
Tech With Tim
Python Programming Tutorial #14 - Writing to a Text File
Tech With Tim
Python Programming Tutorial #15 - Using .count() and .find()
Tech With Tim
Python Programming Tutorial #16 - Introduction to Modular Programming
Tech With Tim
Python Programming Tutorial #17 - Optional Parameters
Tech With Tim
Python Programming Tutorial #18 - Try and Except (Python Error Handling)
Tech With Tim
Python Programming Tutorial #19 - Global vs Local Variables
Tech With Tim
Python Programming Tutorial #20 - Classes and Objects
Tech With Tim
Cool VBS Script to Prank Your Friends!
Tech With Tim
How to Overclock an AMD GPU
Tech With Tim
Best GPU'S For Mining Ethereum (2018)
Tech With Tim
Recursion and Memoization Tutorial Python
Tech With Tim
Ethereum Mining Rig - Hardware Guide
Tech With Tim
Pygame Tutorial #1 - Basic Movement and Key Presses
Tech With Tim
How to Install Pygame (Windows 8/10)
Tech With Tim
How to Trade Your Cryptocurrency (Bitcoin, Ethereum etc.) For Cash!
Tech With Tim
How to Mine Ethereum 2018 - WORKING (Super-Easy)
Tech With Tim
Microphone Comparison - $10 Mic vs $150 Mic (Blue Yeti USB)
Tech With Tim
Pygame Tutorial #2 - Jumping and Boundaries
Tech With Tim
Pygame Tutorial #3 - Character Animation & Sprites
Tech With Tim
Pygame Tutorial #4 - Optimization & OOP
Tech With Tim
OBS Studio Tutorial - Best OBS Settings
Tech With Tim
Linear Search Algorithm - Python Example and Code
Tech With Tim
Make Any Mic Sound AMAZING! (WITH OBS)
Tech With Tim
Binary Search Algorithm - Python Example & Code
Tech With Tim
Pygame Tutorial #5 - Projectiles
Tech With Tim
Pygame Game - Mini Golf
Tech With Tim
Pygame Tutorial - Projectile Motion (Part 1)
Tech With Tim
Pygame Tutorial - Projectile Motion (Part 2)
Tech With Tim
Pygame Tutorial #6 - Enemies
Tech With Tim
Pygame Tutorial #7 - Collision and Hit Boxes
Tech With Tim
Pygame Tutorial #8 - Scoring and Health Bars
Tech With Tim
Cloud Mining vs. Hardware Mining - 2018
Tech With Tim
How to Install Pygame on Mac OSX (Fast-Simple)
Tech With Tim
Pygame Tutorial #9 - Sound Effects, Music & More Collision
Tech With Tim
Pygame Tutorial #10 - Finishing Touches & Next Steps
Tech With Tim
How to Fade Your Screen in Pygame [CODE IN DESCRIPTION]
Tech With Tim
How to Create a Button in Pygame [CODE IN DESCRIPTION]
Tech With Tim
Pygame Side-Scroller Tutorial #1 - Scrolling Background/Character Movement
Tech With Tim
Pygame Side-Scroller Tutorial #2 - Random Object Generation
Tech With Tim
Pygame Side-Scroller Tutorial #3 - Collision
Tech With Tim
Pygame Side-Scroller Tutorial #4 - Scoring and End Screen
Tech With Tim
How to Create A Message Box in Python - Tkinter
Tech With Tim
Is Ethereum Mining Still Profitable - Is It Worth It (April 2018)
Tech With Tim
How to Run MAC OSX on a WINDOWS PC (Clover Boot-loader)
Tech With Tim
Programming Problem #1 - Alphabet Soup (Beginner/Novice)
Tech With Tim
More on: LLM Foundations
View skill →
🎓
Tutor Explanation
DeepCamp AI