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

แชร์
ฝัง
  • เผยแพร่เมื่อ 7 ม.ค. 2025

ความคิดเห็น • 257

  • @KeithGalli
    @KeithGalli  4 ปีที่แล้ว +67

    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 ปีที่แล้ว +2

      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 ปีที่แล้ว +3

    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.

  • @ktejakrishna9620
    @ktejakrishna9620 3 ปีที่แล้ว +9

    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 2 ปีที่แล้ว +1

      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).

  • @moniquemarshall2161
    @moniquemarshall2161 4 ปีที่แล้ว +24

    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 4 ปีที่แล้ว +13

    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!

  • @nicholas-ewing
    @nicholas-ewing 4 ปีที่แล้ว +19

    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 4 ปีที่แล้ว

      Sololearn is a really good free app to learn coding imo also the community is great

  • @samkons280
    @samkons280 3 ปีที่แล้ว +3

    These days when I am looking for a lesson, I always have to include in the search '...by keith' and even if don't TH-cam 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.

  • @osezeestheriyore8612
    @osezeestheriyore8612 4 ปีที่แล้ว +41

    Great video!
    Can you do a 'solving real world data science problem'-video using classes?

  • @larslover6559
    @larslover6559 3 ปีที่แล้ว +32

    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!

  • @adityawadikar9326
    @adityawadikar9326 4 ปีที่แล้ว +12

    This is by far the best video I've seen on TH-cam for this topic .Thanks a lot Keith

    • @KeithGalli
      @KeithGalli  4 ปีที่แล้ว +1

      Glad you enjoyed! :)

  • @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!

  • @MrRadzor
    @MrRadzor 4 ปีที่แล้ว +5

    This channel is underated af . Great work my guy !

  • @jessegreer9
    @jessegreer9 4 ปีที่แล้ว +1

    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!

  • @FreihEitner
    @FreihEitner 3 ปีที่แล้ว +1

    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.

  • @hanqie11
    @hanqie11 4 ปีที่แล้ว +4

    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  4 ปีที่แล้ว +1

      Glad you enjoyed! And lol yeah gotta love subclassing 😁

  • @ikki411
    @ikki411 3 ปีที่แล้ว +2

    I already knew most of this stuff, but you teach so well that it was really nice watching this video!

  • @symnshah
    @symnshah 3 ปีที่แล้ว +1

    This is the best explanation of Python Classes, much much appreciate it.

  • @ferdinandolongoni9246
    @ferdinandolongoni9246 4 ปีที่แล้ว +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 4 ปีที่แล้ว +1

    This was one of the best videos i have seen on python classes. Thanks a lot for sharing

  • @rantan1618
    @rantan1618 4 ปีที่แล้ว +4

    This was a great video, i finally am starting to understand classes in python because of this video, thank you.

    • @portfedh
      @portfedh 3 ปีที่แล้ว

      Same here. Thanks!

  • @JoePescisAngryCousin
    @JoePescisAngryCousin ปีที่แล้ว +1

    Dude, this video is great. I learned so much. You're an excellent teacher.

  • @brijpathak3873
    @brijpathak3873 4 ปีที่แล้ว +3

    You're a great teacher. Thank you so much :) You'll def. know how classes work in Python after watching this!

  • @jameSM__
    @jameSM__ ปีที่แล้ว +1

    This is the best tutorial for classes

  • @frankcasanova2132
    @frankcasanova2132 4 ปีที่แล้ว

    i´ve see a lot of videos about this issue in youtube, and your video it´s the best. greetings from spain!! keep going!

  • @wilmermorales
    @wilmermorales 2 ปีที่แล้ว

    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.

  • @muftizyo8939
    @muftizyo8939 3 ปีที่แล้ว

    My unviersity/college teachers just gave us a debugging assignment with all __init__, subclassing too and I was so confused, this was very helpful

  • @trevorribbans2256
    @trevorribbans2256 4 ปีที่แล้ว +1

    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  4 ปีที่แล้ว +1

      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 :).

  • @lastpercent9767
    @lastpercent9767 2 ปีที่แล้ว

    i dont have words u just saved me big time bro thank you so much for making this video
    print("thank bro")

  • @fentonmsu
    @fentonmsu 4 ปีที่แล้ว

    Explained it better than my professor, yes told him that your videos are better, in my country they don't know how to explain.

  • @camerongoodhue9552
    @camerongoodhue9552 3 ปีที่แล้ว

    Cheers Keith, so good. The polygon example is the best one i've seen as a beginner.

  • @ClockDesignCo
    @ClockDesignCo 4 ปีที่แล้ว

    probably the only vid on classes that have made sense of it to me

  • @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"

  • @jessethomas7009
    @jessethomas7009 3 ปีที่แล้ว

    choosing to do shapes for visual aid was a great idea this was really helpful !

  • @_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.

  • @pythonenthusiast9292
    @pythonenthusiast9292 4 ปีที่แล้ว +1

    thankyou for your videos..
    I have two video series requests for you :
    1) DSA in python
    2) Competitive coding with python (if possible)

  • @jeremycollier4377
    @jeremycollier4377 3 ปีที่แล้ว

    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.

  • @savemyneighborhood
    @savemyneighborhood ปีที่แล้ว

    This analogy is so much easy to understand what class is.

  • @AngeloSomers
    @AngeloSomers 4 ปีที่แล้ว

    More helpful than any other tutorial I’ve seen, Thankyou!

  • @LookNumber9
    @LookNumber9 3 ปีที่แล้ว

    Super useful! Very clear and concise explanations throughout. Thanks.

  • @mikuculus3720
    @mikuculus3720 4 ปีที่แล้ว

    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!

  • @DaveLotito
    @DaveLotito 4 ปีที่แล้ว +4

    21:15 - when you realize you frigged up LOL. Great vid dude!

  • @ceesh5311
    @ceesh5311 3 ปีที่แล้ว +4

    21:15 small moment of clarity

  • @JUDOGAR
    @JUDOGAR 3 ปีที่แล้ว

    Amazing video! Thanks Keith! We wait for more of them!

  • @josephhauser9231
    @josephhauser9231 2 ปีที่แล้ว

    Most helpful video I've seen yet!

  • @paulsmc7041
    @paulsmc7041 3 ปีที่แล้ว +1

    One small correction. At 6:30 (and at at least one other spot) you talk about a "class method" and say this it should have a parameter "self." Although it is defined within a class, this is in fact an "instance method." Instance methods have a parameter "self." Class methods do not.

    • @toadtws
      @toadtws 2 ปีที่แล้ว

      Yeah, this is an unfortunate oversight in an otherwise great video. It wouldn't be so bad if this were another way to say the same thing, like "argument" and "parameter." But a class method (@classmethod) is actually a different thing, so it will actually lead to confusion later on.

  • @jamesferguson5233
    @jamesferguson5233 4 ปีที่แล้ว

    Straightforward and clear...great technique you use to demo these concepts...thanks so much from a budding old geezer hobbyist!

  • @kpaxo123
    @kpaxo123 3 ปีที่แล้ว

    Great video and that lo fi jumper is 🔥

  • @gerardomontanoreyes2704
    @gerardomontanoreyes2704 2 ปีที่แล้ว

    Great video! what happened at 21:16. Some debugging there
    Thank you Keith, so much helpful

  • @python360
    @python360 4 ปีที่แล้ว +5

    Maybe mention "Setter" and "Getter" Methods in a follow up video?
    This video was excellent.

    • @KeithGalli
      @KeithGalli  4 ปีที่แล้ว +1

      Good suggestion! I'll probably include that in the follow up vid

  • @Zippo_1234
    @Zippo_1234 3 ปีที่แล้ว

    Thank you Keith, I learned so much in this video. Subscribed

  • @shubhamdeshpande4111
    @shubhamdeshpande4111 3 ปีที่แล้ว +1

    This is a really good video for beginners in python. Just a small suggestion, instead of using any complicated formula for interior angles of a polygon, just divide 360 by number of sides of the polygon and you will get the interior angle. No need to make the formula complex.

    • @KeithGalli
      @KeithGalli  3 ปีที่แล้ว

      Yeah I needed to review my geometry before making this video. Thanks for the suggestion & glad you enjoyed the video 😊

  • @gabrielstewart9022
    @gabrielstewart9022 3 ปีที่แล้ว

    Best Class Tut Ive found yet! Youre a genius. Subscribeb hahaha

  • @wehibebelachew3527
    @wehibebelachew3527 2 ปีที่แล้ว +1

    Very helpful tutorial and thank you for your time

  • @micharudzki9319
    @micharudzki9319 4 ปีที่แล้ว

    Wow!! I am appreciated for that explanation of Classes in Python. Very detailed information :)

  • @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 ปีที่แล้ว +1

      Thank you! Your feedback means a lot :)

  • @Tracks777
    @Tracks777 4 ปีที่แล้ว +32

    awesome content

  • @darwinpark8228
    @darwinpark8228 4 ปีที่แล้ว

    thank you from Korea! your videos helped me so much

  • @danielholley2495
    @danielholley2495 4 ปีที่แล้ว

    This is an outstanding video. Thanks for taking the time!

  • @fustigate8933
    @fustigate8933 3 ปีที่แล้ว +1

    u were at MIT!!! What a legend. That's my dream and i'll follow what u did

  • @MrRyanroberson1
    @MrRyanroberson1 3 ปีที่แล้ว

    one thing i like to point out when explaining "self", python does the following:
    when you define a class, you define functions that look like Polygon.draw, and you can call those functions as Polygon.draw(square), for example, once you've made a square. As a shorthand, python allows you to move the object to the front in place of the class name, hence square.draw()

    • @MrRyanroberson1
      @MrRyanroberson1 3 ปีที่แล้ว

      the utility of this is usually in cases of multiple inheritance if you want to call a specific parent class's method instead of all super classes' versions of that method at once.

  • @tensianne
    @tensianne 4 ปีที่แล้ว

    Thank you for this video! I really like the way you explain things.

  • @BrianFischer
    @BrianFischer 4 ปีที่แล้ว

    Fantastic video, thank you for putting this up. Looking forward to watching more.

  • @psSubstratum
    @psSubstratum 4 ปีที่แล้ว

    i love your reaction at 17:32

  • @ibluewolf7937
    @ibluewolf7937 2 ปีที่แล้ว

    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.

  • @wesleyvanwestenbrugge8369
    @wesleyvanwestenbrugge8369 4 ปีที่แล้ว

    Excellent video Keith!

  • @WillCastle
    @WillCastle 4 ปีที่แล้ว

    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.

  • @IntuitiveLeap
    @IntuitiveLeap 4 ปีที่แล้ว

    I usually think of the 'self' argument as a reference to an initialized object. We could have 37,493 uniquely named Polygon() objects that have been initialized into memory (ie: we made poly1 = Polygon(params), poly2 = Polygon(params), poly3 ... etc ... 37,493 times. Each one of these objects has a unique value for 'self' that Python is using to be able to find the memory location of the particular instance of Polygon() you are interested in using/manipulating.

    • @KeithGalli
      @KeithGalli  4 ปีที่แล้ว

      Thanks for adding this detail! Very useful way to think about it. Hearting this comment so more people can see it! :)

  • @IronyImpact
    @IronyImpact 4 ปีที่แล้ว

    I couldn't figure out how to contact you directly. But could you please make another video on classes? One that shows them in use in a program? This video was the first time that I started to understand why I should use classes. I need to see them being used in real world examples of code though. Please man, you're a great teacher.

    • @KeithGalli
      @KeithGalli  4 ปีที่แล้ว +1

      Hey i have actually already made a video like that! Check it out here: th-cam.com/video/731LoaZCUjo/w-d-xo.html

  • @CSCrew
    @CSCrew 3 ปีที่แล้ว

    Thanks buddy, it was a great video!

  • @christophrathgeber7567
    @christophrathgeber7567 4 ปีที่แล้ว

    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  4 ปีที่แล้ว

      Glad you enjoy the tutorials! And nice job catching my mistake before I did xD

  • @obscureADSR
    @obscureADSR 2 ปีที่แล้ว

    Great intro to class; thanks!

  • @moritzstrueve5184
    @moritzstrueve5184 4 ปีที่แล้ว

    thumbs up for the beginning :D

  • @iqsa021s
    @iqsa021s 4 ปีที่แล้ว

    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 3 ปีที่แล้ว

    I think you forgot to incorporate a triangle. I think I will try to do it. Thank you for the explanation man..Great.

  • @curdrice70
    @curdrice70 ปีที่แล้ว

    Superb video on classes.

  • @pipertripp
    @pipertripp 4 ปีที่แล้ว +1

    nice vid! I'm surprised you didn't mention the __str__(self) function as it would have made those print statements easier.

  • @sarthak_chauhan1010
    @sarthak_chauhan1010 2 ปีที่แล้ว

    17:32 I'm actually understanding all of this and at this time i was also like oh my god!! why does this happen !!😂

  • @VarunKumar-pz5si
    @VarunKumar-pz5si 4 ปีที่แล้ว +1

    Best I have ever seen

  • @SweetAlexMore
    @SweetAlexMore ปีที่แล้ว

    When would you maybe want to define the arguments inside the draw method within the class? e.g. def draw(self, size, color). In this example, this would've worked, right?

  • @kdkilli
    @kdkilli 2 ปีที่แล้ว +1

    great help buddy thanks a lot

  • @russellstockley117
    @russellstockley117 4 ปีที่แล้ว

    Really enjoying your tutorials,

  • @Amf313
    @Amf313 3 ปีที่แล้ว

    Thank You.... I have learned a lot from your videos

  • @roywilson9580
    @roywilson9580 4 ปีที่แล้ว

    Nice video, thanks for the easy to follow explanations.

  • @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.

  • @willingwinning
    @willingwinning 3 ปีที่แล้ว

    I hit the like button at 0:09 seconds

  • @DrWho2008t101
    @DrWho2008t101 4 ปีที่แล้ว

    Thank you for the video, great work. Appreciate your work.

  • @nicholastanzillo4229
    @nicholastanzillo4229 3 ปีที่แล้ว

    I loved this video, thank you!

  • @darrickrussell776
    @darrickrussell776 3 ปีที่แล้ว

    You can also do:
    angle = 360 / sides
    This will give you the same result.

  • @bastiano2939
    @bastiano2939 3 ปีที่แล้ว

    Amazing video. Helpes a lot

  • @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.

  • @alammaravilla8073
    @alammaravilla8073 3 ปีที่แล้ว

    What a tutorial, really great. THANKS

  • @CountrySideways
    @CountrySideways 3 ปีที่แล้ว

    Thanks!! Well explained! I had to subscribe and like. :)

  • @firminkisamba9914
    @firminkisamba9914 2 ปีที่แล้ว

    Ur intro is cool bro🤣🤣🤣🤣🔥like ur way of teaching

  • @fredrickkenamoah9757
    @fredrickkenamoah9757 4 ปีที่แล้ว

    Great tutorials Keith!! Very clear and concise stuff. Grateful if you would do something on Python from A to Z. Regards

  • @kebincui
    @kebincui 2 ปีที่แล้ว

    Excellent Python teacher 👍👍🌹🌹

  • @briankumanchik2474
    @briankumanchik2474 3 ปีที่แล้ว

    Awesome explanation!

  • @questioner3268
    @questioner3268 4 ปีที่แล้ว +1

    My attempts to import matplotlib.pyplot as plt fail.
    I have installed and imported matplotlib, but cannot install matplotlib.pyplot.
    pip install pyplot
    generates the errors:
    ERROR: Could not find a version that satisfies the requirement pyplot (from versions: none)
    ERROR: No matching distribution found for pyplot
    I'm using PyCharm on Win10, but I get the exact same errors when running from the Cmd line.
    Any suggestions?

  • @leo_kim
    @leo_kim 4 ปีที่แล้ว +2

    Thanks for the tutorials!
    Will you do any content on using Python with Google Docs/Sheets?

  • @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