Understand Classes & Objects in 8 Minutes

Tech With Tim · Beginner ·💻 AI-Assisted Coding ·3y ago

Key Takeaways

Explains classes and objects in object-oriented programming

Full Transcript

thank you [Music] so unfortunately University professors consistently failed to explain the difference between a class and an object and what that actually means obviously you need to understand this before you can start working in object-oriented programming languages and actually write object oriented programming code so in the next five minutes I'm going to explain this to you let's get it alright so on my screen here I'm just going to write a few variables so I have x equals 1 y equals string now what do we know about these variables well we know that each of these variables have a type right we know data types exist in Python and one is type int and this string here is type Str we have a bunch of other types as well and depending on the value that we use it has a different type now what you need to understand about the type of an object is that is really the class that was used to create the object now it's a bit hidden here in Python because something like one and something like string are built into the language and when I write one I don't have to explicitly say oh I'm going to use the int class to Define this however there is an INT class and there is a Str or string class that exists in Python and anytime you do something like create a new string or create a new int you are using that class to create the object okay so almost anything you see in Python so something like one something like two something like a list something like a function these are all objects and they all have a type the type is the class that was used to create them and even though you don't necessarily see that that's still their type right so we know this is an INT we know this is a string now the class is really a blueprint that defines the functionality that an object has the reason why I can do something like Z is equal to one plus one is because the int class which is the type of these two objects defines that I can add two integers together if it didn't Define that I couldn't add them together same reason I can use subtraction multiplication same reason I can use any operation you see in Python it's because the class which is the type of the object defines that behavior it says I can add these two objects now let's look at this example what if I try to do one plus Str well this is not an accepted operation I can't do this if you run this code in Python we will get an error now the reason we'll get an error is because this behavior is not implemented on the string or the int Class A String doesn't know how to add an into it and an INT doesn't know how to add a string to it it's not defined Behavior if it was defined we'd be able to do this so the way you think about this a class is the blueprint that defines the behavior that is allowed on an object an object is an instance of a class you can have multiple objects as many as you want of the same class so I can have a bunch of in so I can have a bunch of floats I can have a bunch of strings all of these objects are dependent they're unique and they use the class to to understand what Behavior they have access to now let's look at a custom class and I'll make this even more clear all right so I've got a custom class on the screen let's dissect this a little bit and then I will create an instance and show you how this works so we have dog class first of all this is the name of our class any instance we create of this class will be of type doc because that's the name of our class just like of type ins string Etc that's the name of our class that's the type of the objects we create using the class now the first method you see here now this is a function but we call it a method because it's inside of a class is what's known as The Constructor or the initializer this is what is called every time you create a new instance now this is called on classes like int on float on string you just don't see it because it's built into the language so I'm kind of exposing to what a class looks like whereas the ones built in are kind of hidden you don't really see them anyways we have in here right and what we take is a name age and breed as well as this self keyword now the self keyword refers to the instance that this is being called on that this function or this method is being called it's important to have that distinction because we have multiple instances self is referring to the individual instance that is being called upon or being used in this context you're going to see what I mean in a second but when I say something like self.name equals name I'm saying I want to create an attribute this is data that's stored on the instance not on the class on the instance called name and I'm making it equal to the name value that I passed to this Constructor continuing same with age same with breed we'll look at an example in a minute now we move down here to our other methods now these methods are essentially the behavior of this type right the behavior of instances this class is Blueprinting what all of my instances are able to do so when I put something like bark this means that I can call the bark method so I can do something like dot bar Arc right on any instance of this class because the method is defined same thing with get info I can call this get info method here and it will return information about the instance that I called this on okay so enough of that let's put this over the side let's look at an example of creating an instance so let me just zoom in a bit here let's say I have dog is equal to Doc now first of all this is how you create an instance of a class if it's not built into python you write the name of the class which is dog you put your parentheses and then you pass any required parameters to the Constructor so we need a name age and breed so I'm going to pass Tim let's just pass four for breed uh let's just go with person I know that's not a dog breed but I can't think of any right now so we'll go with something that doesn't really make sense all right so we passed name age person when we do this by default this Constructor here this initializer gets called and it assigns these attributes to our dog instance this is one instance right now if I write something like dog 2 is equal to dog now let's just imagine I have some values in here I'm not going to write them out what I've done is created a separate instance so we have two individual instances that all have their own attributes now since they are the same type they have the same defined Behavior we know that they have a name we know that they have an age we know that we have a breed we don't know what the name age or breed is but we know that they have that because they are type Doc and we know that on both of these instances I can call the bark method and I can call the get info method because that's the defined behavior in the class so if I go here and I write something like dog dot bark if we were in our terminal or if we're actually running this code you would see that bark would print out again the only reason I can do that is the blueprint the type the class has defined this functionality okay very important to understand here as I wrap up this video that each instance stores its own attributes it has its own information right so when I created this first dog and it has Tim for person then I create the second dog me creating this second dog has no effect on this first dog it doesn't change the name it doesn't change the age it doesn't change the breed they all have their own name age and breed because they are instances of the class okay the class is the blueprint defining the behavior for the object the object kind of uses the class to understand what Behavior it has access to to have its attributes assigned all of that obviously I could continue going with this for hours upon hours upon hours the point of this video was to give you a quick understanding of the difference between classes and objects and hopefully conceptualize in your brain what is a class what is an object what do they mean key point class blueprint object instance of the blueprint object uses the class to understand what Behavior it has the class defines the behavior of all of the objects hopefully this was helpful let me know if you like these short explainer videos I'm happy to do more hopefully I'll see you at another YouTube video [Music]

Original Description

Today I will be going over what most professors fail to explain which is the Classes vs Objects and what that actuallly means. These are crutcial to understand before you can work in object oriented programing languages and code. Commet down below if this helped you! 💻 ProgrammingExpert is the best platform to learn how to code and become a software engineer as fast as possible! Check it out here: https://programmingexpert.io/tim and use code "tim" for a discount! ⭐️ Timestamps ⭐️ 00:00 | Intro 00:29 | Classes Vs Objects 05:23 | Example ◼️◼️◼️◼️◼️◼️◼️◼️◼️◼️◼️◼️◼️◼️ 👕 Merchandise: https://teespring.com/stores/tech-with-tim-merch-shop 📸 Instagram: https://www.instagram.com/tech_with_tim 📱 Twitter: https://twitter.com/TechWithTimm ⭐ Discord: https://discord.gg/twt 📝 LinkedIn: https://www.linkedin.com/in/tim-ruscica-82631b179/ 🌎 Website: https://techwithtim.net 📂 GitHub: https://github.com/techwithtim 🔊 Podcast: https://anchor.fm/tech-with-tim 🎬 My YouTube Gear: https://www.techwithtim.net/gear/ 💵 One-Time Donations: https://www.paypal.com/donate?hosted_button_id=CU9FV329ADNT8 💰 Patreon: https://www.patreon.com/techwithtim ◼️◼️◼️◼️◼️◼️◼️◼️◼️◼️◼️◼️◼️◼️ ⭐️ Tags ⭐️ - Tech With Tim - Programming Expert - Developer Curriculum ⭐️ Hashtags ⭐️ #techwithtim #python #classesvsobjects #programminglanguages
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 A* Path Finding Algorithm(Visualization)
A* Path Finding Algorithm(Visualization)
Tech With Tim
2 Python Programming Tutorial #1 - Variables and Data Types
Python Programming Tutorial #1 - Variables and Data Types
Tech With Tim
3 Python Programming Tutorial #2 - Basic Operators and Input
Python Programming Tutorial #2 - Basic Operators and Input
Tech With Tim
4 Python Programming Tutorial #3 - Conditions
Python Programming Tutorial #3 - Conditions
Tech With Tim
5 Python Programming Tutorial #4 - IF/ELIF/ELSE
Python Programming Tutorial #4 - IF/ELIF/ELSE
Tech With Tim
6 Python Programming Tutorial #5 - Chained Conditionals and Nested Statements
Python Programming Tutorial #5 - Chained Conditionals and Nested Statements
Tech With Tim
7 Python Programming Tutorial #6 - For Loops
Python Programming Tutorial #6 - For Loops
Tech With Tim
8 Python Programming Tutorial #7 - While Loops
Python Programming Tutorial #7 - While Loops
Tech With Tim
9 Python Programming Tutorial #8 - Lists and Tuples
Python Programming Tutorial #8 - Lists and Tuples
Tech With Tim
10 Python Programming Tutorial #9 - Iteration by Item (For Loops Continued...)
Python Programming Tutorial #9 - Iteration by Item (For Loops Continued...)
Tech With Tim
11 Python Programming Tutorial #10 - String Methods
Python Programming Tutorial #10 - String Methods
Tech With Tim
12 How to Overclock a NVIDIA GPU
How to Overclock a NVIDIA GPU
Tech With Tim
13 Python Programming Tutorial #11 - Slice Operator
Python Programming Tutorial #11 - Slice Operator
Tech With Tim
14 Python Programming Tutorial #12 - Functions
Python Programming Tutorial #12 - Functions
Tech With Tim
15 Python Programming Tutorial #13 - How to Read a Text File
Python Programming Tutorial #13 - How to Read a Text File
Tech With Tim
16 Python Programming Tutorial #14 - Writing to a Text File
Python Programming Tutorial #14 - Writing to a Text File
Tech With Tim
17 Python Programming Tutorial #15 - Using .count() and .find()
Python Programming Tutorial #15 - Using .count() and .find()
Tech With Tim
18 Python Programming Tutorial #16 - Introduction to Modular Programming
Python Programming Tutorial #16 - Introduction to Modular Programming
Tech With Tim
19 Python Programming Tutorial #17 - Optional Parameters
Python Programming Tutorial #17 - Optional Parameters
Tech With Tim
20 Python Programming Tutorial #18 - Try and Except (Python Error Handling)
Python Programming Tutorial #18 - Try and Except (Python Error Handling)
Tech With Tim
21 Python Programming Tutorial #19 - Global vs Local Variables
Python Programming Tutorial #19 - Global vs Local Variables
Tech With Tim
22 Python Programming Tutorial #20 - Classes and Objects
Python Programming Tutorial #20 - Classes and Objects
Tech With Tim
23 Cool VBS Script to Prank Your Friends!
Cool VBS Script to Prank Your Friends!
Tech With Tim
24 How to Overclock an AMD GPU
How to Overclock an AMD GPU
Tech With Tim
25 Best GPU'S For Mining Ethereum (2018)
Best GPU'S For Mining Ethereum (2018)
Tech With Tim
26 Recursion and Memoization Tutorial Python
Recursion and Memoization Tutorial Python
Tech With Tim
27 Ethereum Mining Rig - Hardware Guide
Ethereum Mining Rig - Hardware Guide
Tech With Tim
28 Pygame Tutorial #1 - Basic Movement and Key Presses
Pygame Tutorial #1 - Basic Movement and Key Presses
Tech With Tim
29 How to Install Pygame (Windows 8/10)
How to Install Pygame (Windows 8/10)
Tech With Tim
30 How to Trade Your Cryptocurrency (Bitcoin, Ethereum etc.) For Cash!
How to Trade Your Cryptocurrency (Bitcoin, Ethereum etc.) For Cash!
Tech With Tim
31 How to Mine Ethereum 2018 - WORKING (Super-Easy)
How to Mine Ethereum 2018 - WORKING (Super-Easy)
Tech With Tim
32 Microphone Comparison - $10 Mic vs $150 Mic (Blue Yeti USB)
Microphone Comparison - $10 Mic vs $150 Mic (Blue Yeti USB)
Tech With Tim
33 Pygame Tutorial #2 - Jumping and Boundaries
Pygame Tutorial #2 - Jumping and Boundaries
Tech With Tim
34 Pygame Tutorial #3 - Character Animation & Sprites
Pygame Tutorial #3 - Character Animation & Sprites
Tech With Tim
35 Pygame Tutorial #4 - Optimization & OOP
Pygame Tutorial #4 - Optimization & OOP
Tech With Tim
36 OBS Studio Tutorial - Best OBS Settings
OBS Studio Tutorial - Best OBS Settings
Tech With Tim
37 Linear Search Algorithm - Python Example and Code
Linear Search Algorithm - Python Example and Code
Tech With Tim
38 Make Any Mic Sound AMAZING! (WITH OBS)
Make Any Mic Sound AMAZING! (WITH OBS)
Tech With Tim
39 Binary Search Algorithm - Python Example & Code
Binary Search Algorithm - Python Example & Code
Tech With Tim
40 Pygame Tutorial #5 - Projectiles
Pygame Tutorial #5 - Projectiles
Tech With Tim
41 Pygame Game - Mini Golf
Pygame Game - Mini Golf
Tech With Tim
42 Pygame Tutorial - Projectile Motion (Part 1)
Pygame Tutorial - Projectile Motion (Part 1)
Tech With Tim
43 Pygame Tutorial - Projectile Motion (Part 2)
Pygame Tutorial - Projectile Motion (Part 2)
Tech With Tim
44 Pygame Tutorial #6 - Enemies
Pygame Tutorial #6 - Enemies
Tech With Tim
45 Pygame Tutorial #7 - Collision and Hit Boxes
Pygame Tutorial #7 - Collision and Hit Boxes
Tech With Tim
46 Pygame Tutorial #8 - Scoring and Health Bars
Pygame Tutorial #8 - Scoring and Health Bars
Tech With Tim
47 Cloud Mining vs. Hardware Mining - 2018
Cloud Mining vs. Hardware Mining - 2018
Tech With Tim
48 How to Install Pygame on Mac OSX (Fast-Simple)
How to Install Pygame on Mac OSX (Fast-Simple)
Tech With Tim
49 Pygame Tutorial #9 - Sound Effects, Music & More Collision
Pygame Tutorial #9 - Sound Effects, Music & More Collision
Tech With Tim
50 Pygame Tutorial #10 - Finishing Touches & Next Steps
Pygame Tutorial #10 - Finishing Touches & Next Steps
Tech With Tim
51 How to Fade Your Screen in Pygame [CODE IN DESCRIPTION]
How to Fade Your Screen in Pygame [CODE IN DESCRIPTION]
Tech With Tim
52 How to Create a Button in Pygame [CODE IN DESCRIPTION]
How to Create a Button in Pygame [CODE IN DESCRIPTION]
Tech With Tim
53 Pygame Side-Scroller Tutorial #1 - Scrolling Background/Character Movement
Pygame Side-Scroller Tutorial #1 - Scrolling Background/Character Movement
Tech With Tim
54 Pygame Side-Scroller Tutorial #2 - Random Object Generation
Pygame Side-Scroller Tutorial #2 - Random Object Generation
Tech With Tim
55 Pygame Side-Scroller Tutorial #3 - Collision
Pygame Side-Scroller Tutorial #3 - Collision
Tech With Tim
56 Pygame Side-Scroller Tutorial #4 - Scoring and End Screen
Pygame Side-Scroller Tutorial #4 - Scoring and End Screen
Tech With Tim
57 How to Create A Message Box in Python - Tkinter
How to Create A Message Box in Python - Tkinter
Tech With Tim
58 Is Ethereum Mining Still Profitable - Is It Worth It (April 2018)
Is Ethereum Mining Still Profitable - Is It Worth It (April 2018)
Tech With Tim
59 How to Run MAC OSX on a WINDOWS PC (Clover Boot-loader)
How to Run MAC OSX on a WINDOWS PC (Clover Boot-loader)
Tech With Tim
60 Programming Problem #1 - Alphabet Soup (Beginner/Novice)
Programming Problem #1 - Alphabet Soup (Beginner/Novice)
Tech With Tim

Related Reads

📰
How I Built a Free AI Prompt Library With Node.js and PostgreSQL — From Zero to Live in 30 Days
Learn how to build a free AI prompt library using Node.js and PostgreSQL in just 30 days
Dev.to · Gowrishankar Rangasamy
📰
The Python Library That Turned My Messy Automation Scripts Into Professional Desktop Apps
Learn how to turn Python scripts into professional desktop apps using a Python library
Medium · Programming
📰
Your Code Review Happens After Generation. Your Standard Needs to Happen Before It.
Learn to set coding standards before AI-generated code review to ensure consistency and quality
Dev.to · Avery
📰
The AI Creative Stack Just Changed Again: 7 Things Video Producers Should Act On This Week (July…
Video producers can leverage the latest AI creative stack to enhance their workflow and stay competitive, here's how and why it matters
Medium · AI

Chapters (3)

| Intro
0:29 | Classes Vs Objects
5:23 | Example
Up next
GPT 5.6 Sol + Claude Code = Absolutely INSANE (TRY THIS NOW)
Income stream surfers
Watch →