Python Abstraction Explained: Mastering OOP Concepts | Code with Josh

Estimated read time: 1:20

    Learn to use AI like a Pro

    Get the latest AI workflows to boost your productivity and business performance, delivered weekly by expert consultants. Enjoy step-by-step guides, weekly Q&A sessions, and full access to our AI workflow archive.

    Canva Logo
    Claude AI Logo
    Google Gemini Logo
    HeyGen Logo
    Hugging Face Logo
    Microsoft Logo
    OpenAI Logo
    Zapier Logo
    Canva Logo
    Claude AI Logo
    Google Gemini Logo
    HeyGen Logo
    Hugging Face Logo
    Microsoft Logo
    OpenAI Logo
    Zapier Logo

    Summary

    Join Josh from Code with Josh as he dives into the core concept of abstraction in object-oriented programming. In this video, Josh explains how abstraction can simplify code and improve modularity by hiding complex details and only showing what is necessary. Through engaging examples and discussions, he illustrates the use of abstract base classes and methods in Python, demonstrating how to enforce that certain methods are implemented in subclasses. Whether you're building complex systems or designing intuitive APIs, this episode offers valuable insights into mastering abstraction to optimize and maintain scalable code.

      Highlights

      • Josh compares abstraction to 'hiding the complex details' in his fun, engaging style. ๐Ÿคน
      • He revisits the four pillars of OOP, with abstraction being the final piece. ๐ŸŽ“
      • Interactive coding examples make understanding abstract classes in Python easier. ๐Ÿ‘จโ€๐Ÿ’ป
      • Free Python courses are available, providing over 20 hours of instructional content. ๐Ÿ“š
      • Code with Josh is not just about learning Python, but mastering it with engaging lessons. ๐ŸŒŸ

      Key Takeaways

      • Abstraction is key to simplifying code and enhancing modularity by hiding complex details. โžก๏ธ
      • Abstract Base Classes in Python serve as templates to ensure subclasses implement necessary methods. ๐Ÿ“‹
      • Learning abstraction helps create clearer, more intuitive APIs and maintain scalable code. ๐Ÿ’ก
      • Josh provides free Python courses for beginner and advanced learners to jumpstart their coding journey. ๐Ÿš€
      • The episode is packed with code examples, reinforcing the theory with practical application. ๐Ÿ’ป

      Overview

      Josh from Code with Josh is back with a tantalizing breakdown of abstraction in object-oriented programming, unraveling one of the most underappreciated yet essential pillars in coding. He kickstarts the video by clearing up misconceptions around abstraction, moving from abstract art to the tech realm by highlighting its pivotal role in hiding unnecessary complexities and enhancing code readability.

        Through a series of engaging explanations and examples, Josh introduces viewers to the benefits of using abstract base classes and methods in Python. He illustrates how abstraction encourages better organization of code and ensures essential functionalities are implemented by subclasses, making the code more modular and easier to manage. His hands-on coding examples bolster the theoretical insights, providing a full-circle understanding of the concept.

          Besides delving into the nitty-gritty of abstraction, Josh generously offers access to free Python courses on his platform, aiming to elevate both beginners and seasoned coders. By the end of the episode, viewers have not only absorbed the core fundamentals of abstraction but are also inspired to explore further with Josh's free resources and join a vibrant learning community.

            Chapters

            • 00:00 - 01:30: Introduction The chapter introduces the concept of abstraction in object-oriented programming, distinguishing it from artistic abstraction like that of Andy Warhol. It promises to teach the creation of abstract classes and methods through interactive lessons and coding exercises. By the end, readers will understand and be able to apply these concepts themselves.
            • 01:30 - 02:30: The Four Pillars of OOP In this episode, the host, Josh, delves into the concept of abstraction, which is the fourth pillar of Object-Oriented Programming (OOP). In previous episodes, he has covered encapsulation, inheritance, and polymorphism. Josh expresses enthusiasm about discussing abstraction in detail, signifying it as the concluding topic in his series on OOP.
            • 02:30 - 03:30: Free Python Courses Announcement In the chapter titled 'Free Python Courses Announcement', the speaker informs the audience about the release of two free Python courses. With over 20 hours of content, these courses aim to take learners from a beginner level to more advanced knowledge. The speaker encourages viewers to like and subscribe to the channel to help it grow and reach more students globally.
            • 03:30 - 05:00: Recap of OOP Concepts The chapter discusses the concept of Object-Oriented Programming (OOP) in Python. It highlights the importance of understanding Python concepts and provides resources to aid learning. The chapter emphasizes abstraction, explaining its purpose and application in programming. Free resources for those starting their Python journey are also mentioned.
            • 05:00 - 08:00: What is Abstraction? The chapter titled 'What is Abstraction?' explores the concept of abstraction in programming, particularly within object-oriented programming (OOP). It starts by recapping the four pillars of OOP, with a focus on encapsulation. Encapsulation is defined as the bundling of data, essentially holding methods and properties together. The chapter implies that understanding these pillars is crucial for grasping the full scope of OOP and that each element builds upon the previous, enhancing comprehension of abstraction. The discussion suggests that abstraction is key for optimizing code, although the transcript mainly highlights encapsulation as a foundational element.
            • 08:00 - 12:00: Code Example: Vehicle Class The chapter begins by discussing the concept of a cookie jar metaphor, which is used to explain the concept of holding and organizing properties and methods within a class. The focus then shifts to inheritance, which is described as the ability for a subclass (child class) to inherit all properties and methods of an existing class (parent class). The analogy of parent and child is used to explain this relationship.
            • 12:00 - 18:30: Creating a Course Management System In the chapter titled 'Creating a Course Management System,' the focus is on developing a system that manages courses through the use of functions and classes in programming. The discussion involves creating functions that link objects to methods within classes. The challenge addressed is how to manage classes sharing the same method names by employing functions to pass these objects appropriately. Abstraction is highlighted as a key concept, though humorously contrasted with non-related elements like 'chicken noodle soup' and 'Jackson Pollock splatter.'
            • 18:30 - 23:00: Implementing the Math Course This chapter discusses the concept of abstraction in programming. Abstraction is defined as the process of hiding complex details while exposing only the necessary functionalities and interfaces needed at a particular moment. The chapter emphasizes the importance of abstraction for simplifying code and enhancing its usability.
            • 23:00 - 28:30: Implementing the Student Class Discusses the benefits of modular code and encapsulation in programming.
            • 28:30 - 30:00: Using the Code and Conclusion This chapter emphasizes the importance of creating clear and intuitive APIs for promoting maintainable and scalable code, which is a key pillar of understanding object-oriented programming (OOP). It introduces a code breakdown that demonstrates the use of two classes, a single object, and calling a single method to illustrate these concepts.

            Python Abstraction Explained: Mastering OOP Concepts | Code with Josh Transcription

            • 00:00 - 00:30 in today's episode you're going to learn all about abstraction and I'm not talking about that chicken noodle soup guy Andy Warhol no this is a different kind of abstraction this is the fourth pillar of objectoriented programming and by the end of this episode you're going to know exactly how you can create your own abstract based classes as well as abstract methods and I'm going to break this all down for you guys through interactive lessons as well as an interactive Cod exercise if you're ready
            • 00:30 - 01:00 I'm more than ready let's dive into abstraction welcome back guys to another episode of Code with Josh for super obvious reasons I'm Josh I'm stoked to have you guys back and guys this is the fourth pillar of oop I've covered it all right we've gone through encapsulation to inheritance to polymorphism now on to abstraction and I'm excited to break it all down in today's last episode of the
            • 01:00 - 01:30 fourth pillar of O guys before I dive into the episode if you're ready to learn do me a favor hit that like button and subscribe as that really does help me a growing Channel reach more students around the world and I'm not sure if you missed it guys but I've released a free python course well actually no I've released two free python courses of over 20 hours of content taking you from zero to knowing from beginning dinner all the
            • 01:30 - 02:00 way up to proficient to having a good understanding of python Concepts I'm going to link these here all right it's also going to be the first link in the description I'm hosting it completely free on my own platform where I'm giving you guys a bunch of these free resources to help you guys get started to help you gain momentum in your python Journey head on down to the link in the description and check that out after today's video all right let's jump into what is abstraction and why can we use
            • 02:00 - 02:30 this to better optimize our code let's do it now that we're ready for abstraction let's really recap our four pillars okay so these were the four pillars we've talked about all right and as we go through I'm going to link these videos in case you've missed them all right these each add on to each other adding on to your understanding of oop now the first one we had was encapsulation this was really when we bundle data together it's really just holding methods and properties this the
            • 02:30 - 03:00 first pillar okay so think of a cookie jar holding a bunch of cookies then we moved on to inheritance and this is where we can inherit all the properties and methods of an existing class and pass them into a subass like a child class right if we think of the class we inherit as the parent and the child as the subass last episode we addressed polymorphism right and polymorphism enables objects of different class passes to be treated uniformly right so
            • 03:00 - 03:30 this was really like we create a function that takes an object and then links the object to a method a part of a certain class but because maybe many classes share the same method name we can use a function to pass those objects in and then today well it's abstraction right but really what is abstraction we know it's not chicken noodle soup we know it's not Jackson poock splatter
            • 03:30 - 04:00 paintings so what is it well that's what we need to talk about so long story short okay abstraction is hiding complex details while only showing us what we need to see in that moment okay so it only exposes those functionalities and the interfaces that we need when interacting with a certain object now why do we use abstraction well there's a lot of reasons okay um it not only simplifies our code and enhan es the
            • 04:00 - 04:30 readability aspect okay but it allows us to create a more modular code concept as well as acting on the idea of encapsulation it allows us to hold all that complexity so if we really think about this okay how I view it is an abstract Base Class is like a reminder like hey if you use me remember to do this okay now I'll come back to that okay when do we use this anytime you're dealing with complex systems that may
            • 04:30 - 05:00 have interrelated components anytime you could be needing to create clearer more intuitive apis then we could be promoting maintainable and scalable code which we should be doing all the time that's why this is an important pillar in our understanding of object-oriented programming if I introduce you guys here to a code breakdown okay um I'll break it down a bit more here now you can see that we have two classes I have one two I have a single object and the then we're calling a single method okay but
            • 05:00 - 05:30 let's talk about our ABC our abstract Base Class and we can thank Little Mikey for his ABCs on this one now this is our abstract base class because we are inheriting ABC which we import this is a module okay um it's held elsewhere we import ABC which is our abstract based class as well as the abstract method decorator which We'll add in later so I created a class called
            • 05:30 - 06:00 vehicle that's really my abstract base class every vehicle has these three properties I don't care what you're driving if you're driving a truck a Lamborghini a Porsche doesn't matter what you're driving it has a make it has a model it has a year okay and then inside this vehicle I'm able to get the basic info but you can see that I've only defined the method and then I'm skipping the method I haven't put anything in there but I did link something to the method me we linked our
            • 06:00 - 06:30 abstract method decorator so this tells python that whatever comes after this so really this method display info is going to be an abstract method this is just a reminder okay whatever class takes our vehicle abstract Base Class must complete this abstract method then you can see down here I have a class called car car is inheriting vehicle okay we initialize everything we passed down as
            • 06:30 - 07:00 well as I create a new property the number of doors in the vehicle then you can see that I complete my display info method right so display info is now completed right by importing our ABC and Abstract method we almost enforce that this display info method is completed okay so because we inherit vehicle which is an ABC we must complete any info for this abstract method okay long story
            • 07:00 - 07:30 short all right we create an abstract based class as a reminder so any class that inherits this like hey you can use this class I don't care what you do but if you decide to use me you must complete the functions that I have to offer that's what's happened all right that was my quick example let's jump into vs code and let's put this to work so here I already have a template done I haven't done any ABC work yet don't worry I wait for you okay what this
            • 07:30 - 08:00 really is is like a class structure for managing my courses my student enrollments and in this example I'm focusing on math you could add as many courses as you want here I have a class of course in this course every course you take I don't care if you take German math science Chinese okay I can enroll students into a course I can withdraw students and then every student gets a grade doesn't matter the course that's why they're all in this class class then this could be considered my
            • 08:00 - 08:30 abstract base class because now I'm going to create another class math and history right so imagine I was like a school administrator or something and I was responsible for maintaining many different classes I would create an ABC to remind any new course that I create that it must have these methods in it right so in our math course we have the same things but this time we need to actually complete these to-do something
            • 08:30 - 09:00 then finally I'm going to have a lot of students okay so I like to create a class for this to store my data there are a few ways to do this we'll look at that class named tuppo a data class all right if you don't know these we'll talk about them but the student class is holding my student details as well as a method to submit assignments okay so I can add a student's grade to a course today it'll be a math course all right so let's begin up top here how can I create an abstract based class out of my course class what do you remember what
            • 09:00 - 09:30 do we need to do guys real quick do me a favor if you're getting Val you do me a favor click the like button and smash that subscribe Button as that really does help me as a growing Channel all right back to the video Let's import I'll come up here and I'm going to say from ABC I would like to import our ABC class as well as our abstract method function decorator really okay coming down here then I'm going to say class is now going to inherit our ABC that's all
            • 09:30 - 10:00 we have to do then inside in it okay I am going to create these so what do we need to know from each student right I going to give properties here this could be like a course code like the name of the course in the system and then the name of the course so let's create those up here inside in it I can say like course code as well as my course name I can then take away pass for this one and we can create those so self. course code and self. course name awesome we have our two properties now these three
            • 10:00 - 10:30 methods we're not going to complete these here because they're an abstract method all we need to do is link your decorator I'm going to say abstract NE that's it each of them okay that's it your abstract Base Class is done it was that easy now we need to fulfill these requirements so coming down here I'm actually going to jump into well let's start with our math course I can do that so our math course is a type of course so it's going to inherit course which is our ABC then our math what do we need to do within it well anytime we're
            • 10:30 - 11:00 inheriting and we're creating new properties I need to inherit all the existing properties from our super class as well as activate those before creating our new properties we learn this in the concept of inheritance so really inside here I'm going to go up I'm just going to copy a course code and course name and I'm going to paste those right there okay the first thing I'm going to do is say super it's like my super function I would like to initialize what do we want to initialize well well those two that we got from our
            • 11:00 - 11:30 super class we don't need self though and then really inside this math course it's going to hold our well I'm going to create a dictionary real quick let's say students is equal to an empty dictionary and this is really going to hold all of our students and the key is going to be like the students ID and then the value linked to the ID is going to be my student as well as a list of their grades so that's really done okay en rule student what do I need know well I
            • 11:30 - 12:00 need to know a student who am I enrolling all we need to do here is I'm going to tap into my dictionary students remember that that's a dictionary I'm going to give my student and then I'm going to give my student ID which don't worry we haven't created that yet I need to complete the student class which is like uh our data class holding that information so our student ID what is the value well it's going to be like a nested dictionary so it's going to be the name of our student right what is the name of our student tap into student. name name that will be a property that we will fulfill the other
            • 12:00 - 12:30 one key pair we can put here is going to be our grades which is an empty list great we're done with that how do we withdraw a student right well pretty much the same thing how do we delete something from a dictionary let's use the Dell python operator that's it right so make sure you give who do we want to delete I'm going to give this some student then what do we want to do well Dell okay so what's our dictionary self. students insert a key what key we want to delete here well student. student ID
            • 12:30 - 13:00 right whatever ID that we got from the enrolled student can me delete it boom boom we're done awesome the last thing I want to do is calculate our grade this one straightforward how do you calculate a grade right you add them all together you divide it by the length of the list right we're just calculating the average here so if I dive in here the first thing I'm going to do is let's check to make sure we have that student to begin with so if
            • 13:00 - 13:30 student. student ID so if we have the ID in our dictionary oops you can see that I'm actually using my class that's because well it didn't act smartly who do we want to calculate the grade four I'm going to give that a specific object there we go okay so if that student is in our dictionary what do we want to do I'm going to say grade is equal to the sum okay the sum of what well self.
            • 13:30 - 14:00 students let's access our Dictionary what is the key and then what's the child Key Well I want to get the list which is grades so dictionary key key that unlocks our list of grades I can take the sum of that and we can divide it by the length the length of what well the same thing actually because this represents a list if there's five items in the list I want to divide it by five so I'm just going to paste this in Len that is really it if if that's the case let's return something I'm going to drop
            • 14:00 - 14:30 in an F string of the student's name and grade let's just be nice here and let's say else return something else cool oh tab that over cool okay there we go our class is done we fulfilled those abstract methods from our abstract Base Class that's really the concept of abstraction let's fulfill our student class and get these things going so if I go down here I'm going to scroll down so student info holds our student ID in the name of our student what I've been using
            • 14:30 - 15:00 up here we just haven't created it yet so I'm going to come down here inside init let's say our student ID and the name of that student let's remove pass let's create those properties student ID is equal to student ID name is equal to name for our submit assignments all right I'm going to add in here right so what course are we submitting the assignments for and then what is the grade that the student got now how can we do this well remember that we're adding these grades to a list
            • 15:00 - 15:30 in a dictionary okay this is held in our class Math course so the question becomes now inside here I need to access my dictionary how do we do that because it's in that other class well if I come down here and we're going to say course let's pretend that the course is going to be from our students self. student ID so students was our dictionary there we go so I'm giving it my ID to access the dictionary what are we trying to access the list grades now that I have a list
            • 15:30 - 16:00 we can use append we know that what do I want to append I want to append the grade that the student got um I'm just going to print off here and so I know it works grade added this is for me all right so really the oop objectoriented part is done let's use our code now so if I go down to the bottom here since I'm done I'm just going to put in uh use the code so my objects let's create a course called math this can be our math course uh let's say like the name of the
            • 16:00 - 16:30 course is like algebra 101 okay that's the name of the course then I need the name of the course and I'll say intro to algebra awesome so I've created a math object that's my math course now I could create like student one and we have student two so let's say student okay what is this person's student ID let's say 10 what's their name I'm going to say Josh that's me student two also an object okay let's say this person uh
            • 16:30 - 17:00 student ID let's say 55 let's say this person is who we think in here Ezekiel Ezekiel awesome so I have my two students me and Ezekiel let's get us both signed up for this algebra class so how do I sign myself in Ezekiel up for math class well we need to use our enroll students right I have a method here I want to use this method to get me enrolled in this math class really all I'm going to do is take my math course I'm going to I'm going to say enroll student who do I want to enroll student
            • 17:00 - 17:30 one copy and paste student two ezekiel's added all right so we're both in the math class now okay now we could do like for example let's take Ezekiel and let's say submit assignment right so what course is this for this is for the math course that's our object I'm giving it and then this object math right that's our object here so math. students that's how I'm able to actually access our
            • 17:30 - 18:00 dictionary within that other class this is really cool guys remember to slow this down if you need to go back I'm going to link the videos right to the four pillars of oop I actually wrote an article on this broke this down really detailed over on my blog the nerd notok that's in the description all right if you're more of a reader okay I have code examples go read that it's for you now that we have submitted what was the score let's say like a 98 I'm going to copy this two more times uh okay so
            • 18:00 - 18:30 Ezekiel can have two scores let's say a 45 and then I'd like to give me a score two so let's say how did I do let's say an 88 not bad Josh really the last thing you need to do here is I'm just going to come down here and I'm going to print off and I'm going to take math okay what do I want to access well let's just like calculate our grade for example who do I want to calculate I can calculate my grade copy and paste for Ezekiel run your code run my code boom look at that my grade out of that was for me but my
            • 18:30 - 19:00 average grade for me in Ezekiel and you guys really got to see how I got to use an abstract based class while using right there are abstract methods 1 2 3 voila the fourth pillar of objectoriented programming abstraction broken down for you guys I hope you guys got value from today's video if you did do me a favor guys click the like button comment your thoughts how did this help you how are you going to use this to
            • 19:00 - 19:30 better your projects if you're not already subscribed smash that subscribe button guys I was really excited to take you guys through an touch on these advanced concepts right as this helps me I try and take this into the classroom for my students and guys if you've missed it I dropped my 13-hour python course for absolute beginners as well as a 7-hour objectoriented programming course to help further your object-oriented skills check those out all right the first link in the
            • 19:30 - 20:00 description is my free platform with all my free resources for you guys head on down there check it out interact join our community because we're growing quick well uh was that it I think that was it guys thanks for tuning in for today I enjoyed this episode and I can't wait to see you guys in next week's episode of you guessed it code with Josh until then