
Explore the Python data model and how to make your own objects support indexing, length, and operator overloading through special methods, connecting built-in functions to your objects.
Discover how to make your Python objects interact more effectively with language features by using special methods to enhance classes.
Meet a practical, beginner-friendly Python trainer who helps professionals understand Python, delivering well-structured, fun lessons with practical examples that make programming a learnable skill today.
Install Python from python.org, set up Visual Studio Code, create a Python file, install Python extensions, and run code in the terminal to see Hello world.
Install Python from python.org and set up Visual Studio Code as your editor. Create a .py file, install Python extension, and run it in the terminal to verify your setup.
Clarify the Python data model and why this course focuses on it, outlining goals to provide an overview of the data model, use special methods, and integrate with Python features.
Explore how the Python data model lets objects emulate built-in types through special methods, including dunder methods, enabling len, iteration, for-loops, and context management.
Learn to implement __repr__ and __str__ with an employee class to show how print uses __str__, how defaults reveal memory addresses, and how __repr__ supports debugging and recreating objects.
Discover how Python assigns a unique, read-only identity to every object, how the is keyword checks shared memory in CPython, and why identity can vary by implementation.
Implement __eq__ to compare objects by employee ID rather than identity. See how Python uses __eq__ in equality checks, and how in and not equals rely on it.
Explore how to implement comparisons in Python by defining __lt__, __le__, __gt__, and __ge__ to compare magnitudes and support sorting, including using sorted to sort files by type and name.
Explore how the __hash__ method powers hash tables like Python's dictionary, distinguishes hash from equality, and explains collisions, salted string hashes, and the rule that hash must not change.
Learn how Python performs object instantiation and constructors, including __new__ and __init__, how to require arguments, and how to override constructors in subclasses using super.
Explore the power of special methods in Python by making a factory class callable, enabling a single create_user_image method to produce user images with a database connection.
Learn to make your object behave like lists by implementing __len__ and supporting the built-in len function, illustrated with a todolist class and its items.
Learn how to use special methods in Python to make objects subscriptable by implementing __getitem__ and __setitem__, use len and slices, and design simple dictionaries with custom item access.
Compare subscription with iteration by building a range-like class that implements __iter__ and __next__, using iter and next to yield values until StopIteration. Preview operator overloading in the next chapter.
Overload arithmetic operators in Python by implementing __add__ and __mul__ in a Vector2 class, returning new vectors and handling scalar multiplication correctly.
Explore how a context manager and the with statement automate resource management, using __enter__ and __exit__ to safely open and close files, databases, and logs—even after errors.
Explore how Python evaluates truth using the __bool__ method, with __len__ as a fallback, and how truthy and falsy values affect if statements.
Suppress special methods in Python by setting __hash__ to None and triggering type errors, then review a subset of methods, attributes, and terminology around dunder, magic, and the data model.
Explore how to make your classes emulate built-in types using Python's special methods, map built-in functions and operators to dunder methods, and implement containers and context managers.
People say that python is an intuitive language to learn and use. Beginners write their first programs in no time and more advanced programmers make educated guesses how to use parts of Python they have never used before. Even third-party libraries seem to work exactly how you expected.
80% of our time we are using operators like +-/* to calculate, use for-loops to go through lists, use len() to count and indexers to access elements from lists. After a while you don't even have to think about these language features anymore. You just use them and expect things to work the same way everywhere.
But why and how does this work?
In this course, you learn how to design your classes and objects to support common language features so other programmers can make smart guesses about how to use them. You learn how to support iteration, subscripting (indexing), context managers, hashing, equality checking etc.
After this course, you are able to:
- Explain the origin of the names Python datamodel, magic methods, dunder methods and special methods.
- Write an indexer to support subscripting with the __getitem__ method
- Apply __iter__, __next__ to make your objects iterable
- Define the object identity
- Use __hash__ to make objects hashable
- Specify language featureswith special methods __eq__, __ne__, __lt__, __gt__, __le__, __ge__
- Make objects callable with __call__
- Describe how objects are constructed by methods __new__ and __init__
- Wrap existing code into context managers with __enter__ and __exit__
The length of this course is 2 hours. Each topic shows clear examples of how to make your classes more Pythonic by implementing special methods, the secret weapon of your objects. This course has no exercises.
This course is meant for developer with basic Python experience who want to improve their skills or developers who bring experience from another language like c#, java, javascript, c++ and want to learn about Pythons unique language features.
This course is taught by Loek van den Ouweland, a senior software engineer with 25 years of experience. Loek is the creator of Wunderlist for windows, Microsoft To-do and Mahjong for Windows and has 15 years of experience teaching software engineering to a wide variety of students.