Classes and OOP in Python

Real Python · Beginner ·💻 AI-Assisted Coding ·7y ago

Key Takeaways

Python classes and object-oriented programming are demonstrated, including defining classes, instantiating objects, and understanding class properties and behaviors.

Full Transcript

welcome back to our video series on object-oriented programming in Python in the last video we learned what object-oriented programming is and what problem it solves now let's take a look at how we can define our own objects in our Python programs to create our own objects we use Python classes classes are used to create objects practically as many unique objects as we want they also define a type for example take a look at this code my underscore name equals Austin here we create a new variable called my underscore name with a value of Austin behind the scenes this variable is actually referencing an object the type of the object is STR short for string which is a built in Python data type that you've probably used before Python defines a string as a class and in order to create this object of type string we instantiated the string class to instantiate a class or to create an instance of a class just means to create an object from that class you'll hear the word instantiation thrown around a lot in programming so just remember it means to take a class and create an object from that class to make this a little bit more clear I'm going to use one of my favorite analogies let's say we are writing a program that creates doors we'll start by creating a class for a single door this class here is like the blueprint from which we will build all of our door objects the class will specify some properties and behaviors remember each of our door objects will have these and they'll be independent to each door object a door could have properties like height paint color and whether or not it's currently locked it can also have behaviors like open closed and toggle lock we've to find a door class but we've also just defined a new type we can use in our program let's create a door object with a height of 85 inches painted red and we'll say that when we build it it's unlocked we've just instantiate the door class remember that means that we've created an object of type door from our door class and we aren't limited to creating just one just like how in the real world we can build lots of doors from a single door blueprint we can instantiate many door objects from our single door class here I have instantiated one more notice that each of these door objects is unique they both have different heights and colors and each one can either be locked or unlocked at any time we can change the paint color of a single door and that doesn't affect the rest these objects are all independent and they don't rely on the class anymore the class just tells Python how to create the objects here's a question can you think of any scenarios where classes an object could be used in real-world programs in the next video I'll give you some examples and that will learn about how classes are actually built in Python

Original Description

Learn what Python classes are and how to use them. Python classes define a type. You’ve probably worked with built-in types like "int" and "list". Once you have a class, you can instantiate it to create a new object from that class. We say the new object has the type of the class it was instantiated from. Click here to learn more about this topic: https://realpython.com/courses/intro-object-oriented-programming-oop-python/
Watch on YouTube ↗ (saves to browser)
Sign in to unlock AI tutor explanation · ⚡30

Playlist

Uploads from Real Python · Real Python · 0 of 60

← Previous Next →
1 A better Python REPL – bpython vs python interpreter
A better Python REPL – bpython vs python interpreter
Real Python
2 Introducing large-type.com – A Utility Website
Introducing large-type.com – A Utility Website
Real Python
3 Reading Hacker News Without Wasting Tons of Time
Reading Hacker News Without Wasting Tons of Time
Real Python
4 Forward References and Python 3 Type Hints
Forward References and Python 3 Type Hints
Real Python
5 Using Sublime Text as your Git Editor
Using Sublime Text as your Git Editor
Real Python
6 Python Code Linting and Auto-Complete for Sublime Text
Python Code Linting and Auto-Complete for Sublime Text
Real Python
7 Make your Python Code More Readable with Custom Exceptions
Make your Python Code More Readable with Custom Exceptions
Real Python
8 Write Better Tests with Sublime Text's Split Layout Feature
Write Better Tests with Sublime Text's Split Layout Feature
Real Python
9 How to Use Sublime Text from the Command Line
How to Use Sublime Text from the Command Line
Real Python
10 Rename Variables with Multiple Selection in Sublime Text
Rename Variables with Multiple Selection in Sublime Text
Real Python
11 Sublime Text Settings for Writing PEP 8 Python
Sublime Text Settings for Writing PEP 8 Python
Real Python
12 Write Cleaner Python with Sublime Text's Indent Guides
Write Cleaner Python with Sublime Text's Indent Guides
Real Python
13 Sublime Text Whitespace Settings for Python Development
Sublime Text Whitespace Settings for Python Development
Real Python
14 Function Argument Unpacking in Python
Function Argument Unpacking in Python
Real Python
15 Python Code Review: Debugging and Refactoring "Conway's Game of Life" +  Automated Tests
Python Code Review: Debugging and Refactoring "Conway's Game of Life" + Automated Tests
Real Python
16 Using "get()" to Return a Default Value from a Python Dict
Using "get()" to Return a Default Value from a Python Dict
Real Python
17 A Python Shorthand for Swapping Two Variables
A Python Shorthand for Swapping Two Variables
Real Python
18 Python Code Review: Refactoring a Web Scraper, PEP 8 Style Guide Compliance, requirements.txt
Python Code Review: Refactoring a Web Scraper, PEP 8 Style Guide Compliance, requirements.txt
Real Python
19 Click & Jump to Test Failures from the Command Line (iTerm2)
Click & Jump to Test Failures from the Command Line (iTerm2)
Real Python
20 Setting up Sublime Text for Python Developers
Setting up Sublime Text for Python Developers
Real Python
21 Sublime Text + Python Guide Overview
Sublime Text + Python Guide Overview
Real Python
22 Python Code Review: Adding Pytest Tests to an Existing Python Web Scraper
Python Code Review: Adding Pytest Tests to an Existing Python Web Scraper
Real Python
23 Type-Checking Python Programs With Type Hints and mypy
Type-Checking Python Programs With Type Hints and mypy
Real Python
24 A Shorthand for Merging Dictionaries in Python 3.5+
A Shorthand for Merging Dictionaries in Python 3.5+
Real Python
25 Python Code Review Flask Web Security Tutorial + Virtualenvs, requirements.txt
Python Code Review Flask Web Security Tutorial + Virtualenvs, requirements.txt
Real Python
26 My Python Code Looks Ugly and Confusing – Help!
My Python Code Looks Ugly and Confusing – Help!
Real Python
27 Setting Up a Programmer Portfolio/Developer Blog – How To Get Started
Setting Up a Programmer Portfolio/Developer Blog – How To Get Started
Real Python
28 Do I Need a GitHub/GitLab/Bitbucket Profile as a Developer?
Do I Need a GitHub/GitLab/Bitbucket Profile as a Developer?
Real Python
29 Programmer Portfolio – Example and Walkthrough
Programmer Portfolio – Example and Walkthrough
Real Python
30 How to Get Your 1st Speaking Gig at a Tech Conference
How to Get Your 1st Speaking Gig at a Tech Conference
Real Python
31 How to Build Your Public Speaking Skills as a Developer
How to Build Your Public Speaking Skills as a Developer
Real Python
32 The Object-oriented Version of "Spaghetti Code" is "Lasagna Code" ?!
The Object-oriented Version of "Spaghetti Code" is "Lasagna Code" ?!
Real Python
33 Setting up Sublime Text for Python Developers – Lesson #1
Setting up Sublime Text for Python Developers – Lesson #1
Real Python
34 Cool New Features in Python 3.6
Cool New Features in Python 3.6
Real Python
35 "is" vs "==" in Python – What's the Difference? (And When to Use Each)
"is" vs "==" in Python – What's the Difference? (And When to Use Each)
Real Python
36 Emulating switch/case Statements in Python with Dictionaries
Emulating switch/case Statements in Python with Dictionaries
Real Python
37 Python Function Argument Unpacking Tutorial (* and ** Operators)
Python Function Argument Unpacking Tutorial (* and ** Operators)
Real Python
38 What Code Should I Put On My GitHub/GitLab/BitBucket Profile?
What Code Should I Put On My GitHub/GitLab/BitBucket Profile?
Real Python
39 A Crazy Python Dictionary Expression ?!
A Crazy Python Dictionary Expression ?!
Real Python
40 String Conversion in Python: When to Use __repr__ vs __str__
String Conversion in Python: When to Use __repr__ vs __str__
Real Python
41 Method Types in Python OOP: @classmethod, @staticmethod, and Instance Methods
Method Types in Python OOP: @classmethod, @staticmethod, and Instance Methods
Real Python
42 Optional Arguments in Python With *args and **kwargs
Optional Arguments in Python With *args and **kwargs
Real Python
43 Python Context Managers and the "with" Statement (__enter__ & __exit__)
Python Context Managers and the "with" Statement (__enter__ & __exit__)
Real Python
44 Installing Python Packages with pip and virtualenv / venv
Installing Python Packages with pip and virtualenv / venv
Real Python
45 "For Each" Loops in Python with enumerate() and range()
"For Each" Loops in Python with enumerate() and range()
Real Python
46 Python Code Review: LibreOffice Automation and the Python Standard Library
Python Code Review: LibreOffice Automation and the Python Standard Library
Real Python
47 Managing Python Dependencies With Pip and Virtual Environments – Lesson #1
Managing Python Dependencies With Pip and Virtual Environments – Lesson #1
Real Python
48 Python Tutorial: List Comprehensions Step-By-Step
Python Tutorial: List Comprehensions Step-By-Step
Real Python
49 Leveraging Python's Implicit "return None" Statements
Leveraging Python's Implicit "return None" Statements
Real Python
50 What's the meaning of underscores (_ & __) in Python variable names?
What's the meaning of underscores (_ & __) in Python variable names?
Real Python
51 Python Data Structures: Sets, Frozensets, and Multisets (Bags)
Python Data Structures: Sets, Frozensets, and Multisets (Bags)
Real Python
52 Writing automated tests for Python command-line apps and scripts
Writing automated tests for Python command-line apps and scripts
Real Python
53 How to find great Python packages on PyPI, the Python Package Repository
How to find great Python packages on PyPI, the Python Package Repository
Real Python
54 Immutable vs Mutable Objects in Python
Immutable vs Mutable Objects in Python
Real Python
55 PyPI vs Warehouse, the Next-Generation Python Package Repository
PyPI vs Warehouse, the Next-Generation Python Package Repository
Real Python
56 pep8.org — The Prettiest Way to View the PEP 8 Python Style Guide
pep8.org — The Prettiest Way to View the PEP 8 Python Style Guide
Real Python
57 My Experience at PyCon 2017 in Portland
My Experience at PyCon 2017 in Portland
Real Python
58 Pylint Tutorial – How to Write Clean Python
Pylint Tutorial – How to Write Clean Python
Real Python
59 "Reverse a List in Python" Tutorial: Three Methods & How-to Demos
"Reverse a List in Python" Tutorial: Three Methods & How-to Demos
Real Python
60 Python Refactoring: "while True" Infinite Loops & The "input" Function
Python Refactoring: "while True" Infinite Loops & The "input" Function
Real Python

This video teaches the basics of Python classes and object-oriented programming, including defining classes, instantiating objects, and understanding class properties and behaviors. It provides a foundation for creating custom objects in Python programs.

Key Takeaways
  1. Define a class using the class keyword
  2. Instantiate an object from a class using the class name
  3. Understand class properties and behaviors
  4. Create multiple objects from a single class
  5. Recognize the independence of objects
💡 Classes define a type and can be instantiated to create multiple unique objects, each with their own properties and behaviors.

Related Reads

📰
I Thought You Couldn’t Test AI Apps. Then I Built an Eval Pipeline That Caught 47 Regressions.
Learn how to build an evaluation pipeline to test AI apps and catch regressions, a crucial step in ensuring the reliability of AI systems
Medium · Programming
📰
AI Coding Tools and the Missing Blueprint Step
Learn how to effectively integrate AI coding tools into your development workflow by identifying the missing blueprint step and implementing a spec-driven approach to ensure maintainable codebases.
Dev.to AI
📰
Your AI coding agent starts every session like it just met you
Learn how AI coding agents like Claude Code start every session without memory, and how this affects their functionality
Dev.to · Ohad Krispin
📰
The AI read langchainrb cold. Sense still cut the cost 22%.
Apply AI to optimize Ruby projects and reduce costs by 22%
Medium · AI
Up next
How to Start a SaaS Business in 2026
Learn With Shopify
Watch →