Loading video player…

Python's Magic Methods in Classes (Overview)

As a Python developer who wants to harness the power of object-oriented programming, you’ll love to learn how to customize your classes using special methods, also known as magic methods or dunder methods. A special method is a method whose name starts and ends with a double underscore. These methods have special meanings for Python.

Python automatically calls magic methods as a response to certain operations, such as instantiation, sequence indexing, attribute managing, and much more. Magic methods support core object-oriented features in Python, so learning about them is fundamental for you as a Python programmer.

In this video course, you’ll:

  • Learn what Python’s special or magic methods are
  • Understand the magic behind magic methods in Python
  • Customize different behaviors of your custom classes with special methods
Download

Course Slides (.pdf)

5.6 MB
Download

Sample Code (.zip)

3.3 KB

00:00 Welcome to the Python’s Magic Methods and Classes video course. I’m Negar and I’ll be your guide on this journey through magic methods on how to use them in your classes.

00:11 What are they? Why do you need them, and why are they magical? Magic methods are special functions in Python that begin and end with double underscores. These methods allow you to define custom actions for your classes, making your objects behave in specific ways when they interact with Python’s built-in operations.

00:33 In other words, they let you define all kinds of custom behaviors for operations like addition, comparison or even how your object is printed. Let’s break it down with an example.

00:45 Imagine you go to a bank and open up two bank accounts. You want to use Python to compare the balance of these two accounts,

00:55 so you create a class called BankAccount and add a class attribute for the balance.

01:02 Next, you set up your two bank accounts, account1 and account2. Finally, you try using the greater than symbol to check if one account has more money than the other, expecting this simple, true or false, but instead, you get a type error: greater than symbol, not supported between instances of BankAccount.

01:25 By default, Python doesn’t know how to compare these objects because it doesn’t automatically know what attributes or values to compare.

01:34 Okay, but how can you add this capability to your class? You guessed it. The answer is magic methods. You can use specific magic methods to teach Python how to compare your bank accounts based on their balances.

01:48 You’ll learn about how to use magic methods later, but for now, just know that they help your custom classes work naturally with Python’s built-in features. What makes them magic is how they allow your objects to blend right in with Python’s core operations, just like the built-in types.

02:08 That is what brings that magical touch to your code.

02:12 Magic methods are often called dunder methods, short for double underscore, because, well, they start and end with double underscores. They’re also referred to as special methods because they give your classes special functionality and even sometimes they’re called double underscore methods.

02:32 These terms all describe the same concept.

02:36 You’ve actually probably have seen them around: __init__, __repr__, __getattr__, __len__. All of these are examples of powerful and essential magic methods,

02:47 but there’s a lot of them, isn’t there? And they could kind of come off as intimidating. This could be you right now, kind of intrigued, kind of confused.

02:57 By the end of this course, you’ll love these magical methods and appreciate just how handy they can be.

03:04 Before you start watching the next lessons, make sure you refresh your memory. You can go ahead and try these tutorials and video courses. To remember what iterators and iterables are and how to use them you can go through “Iterators and Iterables in Python: Run Efficient Iterations”.

03:23 And you’ll be dealing with classes all the time, so make sure you go through “Python Classes: The Power of Object-Oriented Programming and “Object Oriented Programming in Python”.

03:36 In this video course, you’ll learn the essentials of Python’s magic methods, explore the creation and initialization of objects using __init__ and __new__, discover how to represent objects as user-friendly and developer-friendly strings using the __str__ and __repr__ magic methods.

03:58 You’ll also understand how to build your own iterables and iterators making use of the __iter__ and __next__ magic methods. You’ll learn to make objects callable by implementing the __call__ magic method.

04:13 And finally, you’ll understand how to create custom sequences and mappings.

04:21 Next up is getting started with magic methods.

Become a Member to join the conversation.