Everything you need to know about Classes in Python! (Object Oriented Programming Tutorial)

  Рет қаралды 187,400

Keith Galli

Keith Galli

Күн бұрын

Follow me / keithgalli for more tech content!
Join the Python Army to get access to perks!
KZbin - / @keithgalli
Patreon - / keithgalli
Download Kite: www.kite.com/
In this video we overview the main things I think are pointing when you are starting to learn about classes & integrate them in your everyday code. In this video we cover:
- why should we use object oriented programming (oop) ?
- the basics of defining a class in python (__init__)
- class methods
- what the heck is "self"?
- Inheritance & Subclassing
- Operator overriding
A timeline is in the comments.
If there are topics you felt like I left out, let me know in the comments! I'll potentially make a follow up video to this one.
For an example of me structuring my code with classes, check out this machine learning video: • Real-World Python Mach...
If you want to practice using classes, try rewriting the game we build in this video to use them: • How to Program a Game!...
^(I'll update the github repo for that video with my solution to this soon)
Resources:
Github code: github.com/KeithGalli/python-...
Operator overloading: www.geeksforgeeks.org/operato...
---------------------
Follow me on social media!
Instagram | / keithgalli
Twitter | / keithgalli
---------------------
Today’s merch!
Creator: @ChilledCow
Website: teespring.com/stores/chilledc...
---------------------
Video timeline!
0:00 - Introduction
1:34 - Why use classes?
2:27 - Defining our first class in Python
6:05 - Class methods
13:53 - Passing in default keyword arguments (size of our polygons)
20:18 - Inheritance & subclassing (using the super() method)
24:43 - Class method overriding
26:06 - Plotting points with classes
28:09 - Operator overloading
---------------------
If you are curious to learn how I make my tutorials, check out this video: • How to Make a High Qua...

Пікірлер: 258
@KeithGalli
@KeithGalli 4 жыл бұрын
Video timeline! 1:34 - Why use classes? 2:27 - Defining our first class in Python 6:05 - Class methods 13:53 - Passing in default keyword arguments (size of our polygons) 20:18 - Inheritance & subclassing (using the super() method) 24:43 - Class method overriding 26:06 - Plotting points with classes 28:09 - Operator overloading Thanks for watching! Please consider liking & subscribing if you enjoyed this video :).
@theultimatereductionist7592
@theultimatereductionist7592 3 жыл бұрын
My experience with all computer programming, 40 years of experience, starting with Fortran, then C++, Java, LaTeX & TeX typesetting, and now Python, is that it is such a colossal waste of effort. To spend 40 years trying to figure out the meaning of one piece of syntax, and then succeeding, only to have never made any money during those 40 years because all one did was try to figure out what that one piece of syntax does or means, is the understatement of understatements of Pyrrhic Victory. I honestly now hate it more and learn and understand less of it now than at any earlier point in my life. At least taking formal community college courses got me to understand and do something with programming. But literally there exists absolutely nothing PRACTICAL that one can do with computer programming in ANY language outside doing formal college course work for a teacher. I mean, sure, just to make the codehead morons & idiots happy whose entire life is wrapped up in fetishizing and worshipping and oohing and aahing at one language using a period and another language using a ? mark to delimit a command or whatever, believe it's the greatest thing since farming & anesthesia when I figure out how to get one line to work, before getting stopped for the next 10 years by the next 200 lines of code. To teach me programming, you would have to personally live with me and work with me every day for 10 years, and pay me $30,000 a year to do this thing I hate. Best to stick to proving mathematical theorems. At least, when one finally triumphs after 30 years trying to solve that, one has added new deep original knowledge to the collective knowledge of humans.
@JK-jt3lr
@JK-jt3lr Жыл бұрын
If you want to add more points at once, use the __radd__ function.
@kevy1967
@kevy1967 Жыл бұрын
I’ve just returned to programming. I grew up in the 1970s/80s programming an old TRS-80 in basic. I’ve just started learning Python and have been struggling with classes, so, thank you. Your video and style of teaching was excellent and I finally think I can understand now.
@moniquemarshall2161
@moniquemarshall2161 4 жыл бұрын
I've always struggled to understand the usefulness of classes, and this video communicated it really well with great examples. Thanks for always producing quality content!
@johnwu7509
@johnwu7509 3 жыл бұрын
I had lots of trouble understanding classes. I watched no less than 5 videos and went through many sites and this video really helped me to understand. Great job!
@samkons280
@samkons280 2 жыл бұрын
These days when I am looking for a lesson, I always have to include in the search '...by keith' and even if don't KZbin brings your videos first because they know my preference!. Such a great teacher, I have followed through many of your lessons decided to shout out on this one. Keep being on fire.
@ikki411
@ikki411 3 жыл бұрын
I already knew most of this stuff, but you teach so well that it was really nice watching this video!
@jeremycollier4377
@jeremycollier4377 2 жыл бұрын
Hey Keith! Amazing video. I found this video because I wanted to truly understand the basics of classes and you did such a great job of explaining it in a simplistic way. Not to mention showing off some cool visual libraries! Looking forward to see what other videos you have.
@FreihEitner
@FreihEitner 2 жыл бұрын
This was really handy! Between a 5 hour "Nana" Python tutorial I watched earlier and this, I have a greater understanding of OOP than I have had for the past 15 years of off-and-on reading of programming books trying to grasp it.
@nicholas-ewing
@nicholas-ewing 4 жыл бұрын
After learning Python via SoloLearn and having to struggle through their horribly designed lesson on OOP for Python, this tutorial was an amazing help at actually understanding OOP.
@nikhilprem7998
@nikhilprem7998 3 жыл бұрын
Sololearn is a really good free app to learn coding imo also the community is great
@jessegreer9
@jessegreer9 3 жыл бұрын
I have built a couple of basic classes but I’m writing one to auto do a bunch of freq calcs that is a lot more complicated. 10min into your vid and I finally got a work in progress. Thank you!
@osezeestheriyore8612
@osezeestheriyore8612 4 жыл бұрын
Great video! Can you do a 'solving real world data science problem'-video using classes?
@ktejakrishna9620
@ktejakrishna9620 2 жыл бұрын
For angles, we can use simple formula, exterior angle for n sided polygon = 360/n, and in draw method, we can use left instead of right as below. turtle.forward(self.size) turtle. left(360/self.sides) We can get the polygon drawn
@n8trh
@n8trh Жыл бұрын
Yes, this! When I took a Python class my first year at my university, they used this method to draw pentagons with the turtle package. I realized in class that this method was not only very intuitive, but also could be used for what became my favorite derivation for the interior angle formula of a regular n-gon (or by extension, their sum formula).
@ferdinandolongoni9246
@ferdinandolongoni9246 4 жыл бұрын
Man, you're saving my days! I want to make a snake game in python and saw some tutorials on the ytb and found nothing that i could totally understand. Then I saw you Python Game Tutorial and learned so much! Besides that, I saw you GUI tutorial and learned so uch about Tkinter too. So, thank you so much and continue doing this awesome job! Regards from Brazil
@VinodKRamachandra
@VinodKRamachandra 3 жыл бұрын
This was one of the best videos i have seen on python classes. Thanks a lot for sharing
@MrRadzor
@MrRadzor 3 жыл бұрын
This channel is underated af . Great work my guy !
@brijpathak3873
@brijpathak3873 3 жыл бұрын
You're a great teacher. Thank you so much :) You'll def. know how classes work in Python after watching this!
@frankcasanova2132
@frankcasanova2132 3 жыл бұрын
i´ve see a lot of videos about this issue in youtube, and your video it´s the best. greetings from spain!! keep going!
@greg_715
@greg_715 3 жыл бұрын
I did not understand classes at all until watching this. You did what my university lecturer couldn't. Thanks a bunch!
@camerongoodhue9552
@camerongoodhue9552 2 жыл бұрын
Cheers Keith, so good. The polygon example is the best one i've seen as a beginner.
@_sofigeek_9452
@_sofigeek_9452 2 жыл бұрын
Very well done! Now it is so beautiful, to see and understand the concept of object programing language. Thank you, Brother.
@AngeloSomers
@AngeloSomers 3 жыл бұрын
More helpful than any other tutorial I’ve seen, Thankyou!
@hanqie11
@hanqie11 3 жыл бұрын
One of the best explanations of oop. Thank you! Love the way you get enthusiastic when talking about sub classing around 24' :D So nerdy
@KeithGalli
@KeithGalli 3 жыл бұрын
Glad you enjoyed! And lol yeah gotta love subclassing 😁
@JoePescisAngryCousin
@JoePescisAngryCousin Жыл бұрын
Dude, this video is great. I learned so much. You're an excellent teacher.
@LookNumber9
@LookNumber9 2 жыл бұрын
Super useful! Very clear and concise explanations throughout. Thanks.
@symnshah
@symnshah 3 жыл бұрын
This is the best explanation of Python Classes, much much appreciate it.
@jessethomas7009
@jessethomas7009 3 жыл бұрын
choosing to do shapes for visual aid was a great idea this was really helpful !
@larslover6559
@larslover6559 3 жыл бұрын
You are "my kind" of teacher. Going forward in a relaxed nice progressive way. You being a graduate from the best university in the world certainly gives you credibility too, haha. Keep up the good work!
@jamesferguson5233
@jamesferguson5233 3 жыл бұрын
Straightforward and clear...great technique you use to demo these concepts...thanks so much from a budding old geezer hobbyist!
@wilmermorales
@wilmermorales Жыл бұрын
oh man, your tutorials are the best I have found!
@tuckerboan2363
@tuckerboan2363 3 жыл бұрын
I usually see examples of people who use the Name, age class. This one is much better because you actually do one that's much harder but you explain it so well. Thank you so much for this video.
@adityawadikar9326
@adityawadikar9326 4 жыл бұрын
This is by far the best video I've seen on KZbin for this topic .Thanks a lot Keith
@KeithGalli
@KeithGalli 4 жыл бұрын
Glad you enjoyed! :)
@mikuculus3720
@mikuculus3720 3 жыл бұрын
My book taught me basically everything but I didn’t know what any of it meant this video is great for understanding what you’re doing!
@kpaxo123
@kpaxo123 3 жыл бұрын
Great video and that lo fi jumper is 🔥
@drpwnage23
@drpwnage23 2 жыл бұрын
Thank you Keith, I learned so much in this video. Subscribed
@monagulapa3022
@monagulapa3022 3 жыл бұрын
You are a very talented person. Because you can explain complex ideas in a simple way new learners can understand. And as always, Thank you for your amazing tutorials! :)
@kkopchynski1
@kkopchynski1 2 жыл бұрын
Great video! To elaborate on some previous comments that suggested 360/n: The reason this works is that you are using the EXTERIOR angles in turtle geometry, and the exterior angles of any regular polygon always add to 360. This is the classic "Turtle Trip Theorem"
@BrianFischer
@BrianFischer 3 жыл бұрын
Fantastic video, thank you for putting this up. Looking forward to watching more.
@JUDOGAR
@JUDOGAR 3 жыл бұрын
Amazing video! Thanks Keith! We wait for more of them!
@ClockDesignCo
@ClockDesignCo 3 жыл бұрын
probably the only vid on classes that have made sense of it to me
@tensianne
@tensianne 3 жыл бұрын
Thank you for this video! I really like the way you explain things.
@muftizyo8939
@muftizyo8939 2 жыл бұрын
My unviersity/college teachers just gave us a debugging assignment with all __init__, subclassing too and I was so confused, this was very helpful
@danielholley2495
@danielholley2495 3 жыл бұрын
This is an outstanding video. Thanks for taking the time!
@josephhauser9231
@josephhauser9231 Жыл бұрын
Most helpful video I've seen yet!
@wehibebelachew3527
@wehibebelachew3527 2 жыл бұрын
Very helpful tutorial and thank you for your time
@rantan1618
@rantan1618 3 жыл бұрын
This was a great video, i finally am starting to understand classes in python because of this video, thank you.
@portfedh
@portfedh 2 жыл бұрын
Same here. Thanks!
@lastpercent9767
@lastpercent9767 Жыл бұрын
i dont have words u just saved me big time bro thank you so much for making this video print("thank bro")
@ashleysullivan7965
@ashleysullivan7965 Жыл бұрын
Great intro to class; thanks!
@trevorribbans2256
@trevorribbans2256 3 жыл бұрын
First thing I have had difficulty with in Python. Was pumped to find out I can have my buddy from high school teaching it to me haha really helpful video man!
@KeithGalli
@KeithGalli 3 жыл бұрын
Hahaha what's good Trevor! I definitely found classes confusing at first too. Happy that you stumbled on this video and found it helpful! Hope all is well :).
@roywilson9580
@roywilson9580 4 жыл бұрын
Nice video, thanks for the easy to follow explanations.
@pythonenthusiast9292
@pythonenthusiast9292 3 жыл бұрын
thankyou for your videos.. I have two video series requests for you : 1) DSA in python 2) Competitive coding with python (if possible)
@micharudzki9319
@micharudzki9319 3 жыл бұрын
Wow!! I am appreciated for that explanation of Classes in Python. Very detailed information :)
@sachin2021
@sachin2021 4 жыл бұрын
Hi Keith Galli, Your tutorials are simply awesome. I have enjoyed every bit of it. Will it be possible for you to make a small video on polymorphism, encapsulation, multiple inheritance and context manager. PS: the introduction of Glasses..oops..classes...classes in Python was the cherry on the cake.
@wesleyvanwestenbrugge8369
@wesleyvanwestenbrugge8369 4 жыл бұрын
Excellent video Keith!
@jameSM__
@jameSM__ Жыл бұрын
This is the best tutorial for classes
@CSCrew
@CSCrew 3 жыл бұрын
Thanks buddy, it was a great video!
@darwinpark8228
@darwinpark8228 3 жыл бұрын
thank you from Korea! your videos helped me so much
@solarenergynow08
@solarenergynow08 2 жыл бұрын
Great and clear video. Thanks!
@curdrice70
@curdrice70 Жыл бұрын
Superb video on classes.
@kychemclass5850
@kychemclass5850 Жыл бұрын
A great tutorial. Thank you so much!
@fredrickkenamoah9757
@fredrickkenamoah9757 3 жыл бұрын
Great tutorials Keith!! Very clear and concise stuff. Grateful if you would do something on Python from A to Z. Regards
@russellstockley117
@russellstockley117 3 жыл бұрын
Really enjoying your tutorials,
@leo_kim
@leo_kim 4 жыл бұрын
Thanks for the tutorials! Will you do any content on using Python with Google Docs/Sheets?
@DrWho2008t101
@DrWho2008t101 3 жыл бұрын
Thank you for the video, great work. Appreciate your work.
@savemyneighborhood
@savemyneighborhood Жыл бұрын
This analogy is so much easy to understand what class is.
@jmstockholm
@jmstockholm 4 жыл бұрын
Well chosen example and leaving some things for later and picking them up at an appropriate point in time like "self". Going into enough details but not to much, i.e. simplified enough (substituting "+") to get the idea, but not to a point where the statement actually isn't correct (e.g. common with metaphors), thus only confusing (smarter) people. Thanks for the effort!
@KeithGalli
@KeithGalli 4 жыл бұрын
Thank you! Your feedback means a lot :)
@gabrielstewart9022
@gabrielstewart9022 2 жыл бұрын
Best Class Tut Ive found yet! Youre a genius. Subscribeb hahaha
@briankumanchik2474
@briankumanchik2474 3 жыл бұрын
Awesome explanation!
@christophrathgeber7567
@christophrathgeber7567 3 жыл бұрын
Such great tutorials! Really great teaching style and great examples! It is really fun watching and learning. Felt so proud when i noticed the missing "self" before you did :D Great accidental attention check :-)
@KeithGalli
@KeithGalli 3 жыл бұрын
Glad you enjoy the tutorials! And nice job catching my mistake before I did xD
@bastianian2939
@bastianian2939 3 жыл бұрын
Amazing video. Helpes a lot
@nicholastanzillo4229
@nicholastanzillo4229 2 жыл бұрын
I loved this video, thank you!
@valenciawalker6498
@valenciawalker6498 Жыл бұрын
Thank you ! For breaking it down .
@robertknopf6207
@robertknopf6207 3 жыл бұрын
Keith, love your stuff. Been watching a lot of them. Quick question with classes, how would you do a variable amount of subclasses with variable attributes? So using your shape class, have the python program choose from the different shapes, and then choose different lengths, colors, line thicknesses and then store them so that you can refer back to them later?
@jannealasaarela233
@jannealasaarela233 3 жыл бұрын
Thanks!! Well explained! I had to subscribe and like. :)
@ceesh5311
@ceesh5311 3 жыл бұрын
21:15 small moment of clarity
@wiz8058
@wiz8058 4 жыл бұрын
😊😊 you nailed it 💯 and best content ever bro. And the glasses at first you got me I thought you gonna teach about blue ray lenses then you came back on track 😂😂😂, but for the video you did great keep it up
@Amf313
@Amf313 2 жыл бұрын
Thank You.... I have learned a lot from your videos
@DaveLotito
@DaveLotito 3 жыл бұрын
21:15 - when you realize you frigged up LOL. Great vid dude!
@fentonmsu
@fentonmsu 3 жыл бұрын
Explained it better than my professor, yes told him that your videos are better, in my country they don't know how to explain.
@krishnadhakad746
@krishnadhakad746 Жыл бұрын
great help buddy thanks a lot
@alammaravilla8073
@alammaravilla8073 3 жыл бұрын
What a tutorial, really great. THANKS
@gerardomontanoreyes2704
@gerardomontanoreyes2704 Жыл бұрын
Great video! what happened at 21:16. Some debugging there Thank you Keith, so much helpful
@hobbz4921
@hobbz4921 4 жыл бұрын
great video keith, can you do a video about dictionarys, can you make a mock program that references a random database of some kind please?
@MalekBenkouider
@MalekBenkouider 2 жыл бұрын
Great video, thank you so much
@WillCastle
@WillCastle 3 жыл бұрын
We are learning classes in my programming 1 class and not even our textbook (by person) goes over how to make our modules useful. Our book has been a nightmare our entire semester. Luckily our professor doesn't teach straight from the textbook and luckily I found your video on youtube.
@tempura_edward4330
@tempura_edward4330 3 жыл бұрын
Thank you for this great video!!
@ibluewolf7937
@ibluewolf7937 Жыл бұрын
Regarding the polygon example instead of: defining Object properties self.interiorAngles = (self.sides - 2)*180 self.angle = self.interiorAngles/self.sides and then using them with turtle.right(180-self.angle), you could just use: turtle.right(360/self.sides) because every polygon has an angle sum of 360º and you want all of your polygon's angles to be equal in each of your sides.
@psSubstratum
@psSubstratum 3 жыл бұрын
i love your reaction at 17:32
@python360
@python360 4 жыл бұрын
Maybe mention "Setter" and "Getter" Methods in a follow up video? This video was excellent.
@KeithGalli
@KeithGalli 4 жыл бұрын
Good suggestion! I'll probably include that in the follow up vid
@iqsa021s
@iqsa021s 3 жыл бұрын
I watched this episode for the first time, so I can understand the object-oriented approach Really thank you very much The only thing I still don’t know much about the usage of __add__, the others are generally understood Student from Taiwan
@happyagain855
@happyagain855 2 жыл бұрын
I think you forgot to incorporate a triangle. I think I will try to do it. Thank you for the explanation man..Great.
@FaraazQ
@FaraazQ 4 жыл бұрын
For the next or some subsequent video maybe you could do productionizing a model, making it so we can save a trained model and use it on new data via a flask app or restful API (I know very little about this so if that didn't make sense, sorry)
@baciukrystyan6479
@baciukrystyan6479 4 жыл бұрын
Very well explained! Thank You!
@KeithGalli
@KeithGalli 4 жыл бұрын
Glad you enjoyed! You're welcome :)
@mubashir22ful
@mubashir22ful 4 жыл бұрын
Thank you so much bro!
@mahmudhasankhan7951
@mahmudhasankhan7951 Жыл бұрын
Thanks Keith.
@mustafaalkan3378
@mustafaalkan3378 4 жыл бұрын
thanks for kite it looks very handy
@psyche2739
@psyche2739 4 жыл бұрын
I like your teaching style bro! I hope you will create a tutorial in your next video about socket. I have already subscribed and like your video. I think Shape is better than Polygon as your class name. Thanks!
@jeancarlobejaran3295
@jeancarlobejaran3295 4 жыл бұрын
Great video!
@moritzstrueve5184
@moritzstrueve5184 3 жыл бұрын
thumbs up for the beginning :D
@szymonkrupa
@szymonkrupa 4 жыл бұрын
Would be nice to see some practical use of classes in data science (data visualization). Or maybe you know where can I find examples? Keep up good work!
@KeithGalli
@KeithGalli 4 жыл бұрын
I agree! Check out my machine learning tutorial, linked to in the description as one example.
@KlinikXOfficial
@KlinikXOfficial 2 жыл бұрын
Real good video. However, everyone makes a class inside the same file, a helpful video is calling a class from another file and from another directory would be awesome. Also, you could of simplified the calculations for the angles by doing turtle.right(360/self.sides) and it would of worked just fine haha
Python Object Oriented Programming (OOP) - For Beginners
53:06
Tech With Tim
Рет қаралды 3,2 МЛН
ШЕЛБИЛАР | bayGUYS
24:45
bayGUYS
Рет қаралды 677 М.
У мамы в машине все найдется
00:38
Даша Боровик
Рет қаралды 3,3 МЛН
когда одна дома // EVA mash
00:51
EVA mash
Рет қаралды 13 МЛН
5 Useful Dunder Methods In Python
16:10
Indently
Рет қаралды 47 М.
Functions vs Classes: When to Use Which and Why?
10:49
ArjanCodes
Рет қаралды 137 М.
You Should Be Using This For Work/Research in Python | OOP Tutorial
34:23
Mastering Python - Everything You Need To Know To Become a Python Master
16:02
Object Oriented Programming (OOP) in Python
46:37
Traversy Media
Рет қаралды 275 М.
OOP Class Inheritance and Private Class Members - Python for Beginners!
16:12
Python 101: Learn the 5 Must-Know Concepts
20:00
Tech With Tim
Рет қаралды 1 МЛН
Python Metaclasses: Everything is an Object
31:23
Real Python
Рет қаралды 6 М.
Learn Python Classes With a Text-Based Battle - OOP Tutorial
15:25
Ork Slayer Gamedev
Рет қаралды 124 М.