Locked learning resources

You must own this product to watch this lesson.

Locked learning resources

You must own this product to watch this lesson.

Using Special Methods to Explore Python's Core Topics

Welcome to Week 2. It’s day 6.

00:00 Welcome to week two and in this week we’re going to properly start digging underneath the surface of Python. I use this analogy often, either this one or looking behind the scenes.

00:11 So apologies if you hear me say it often, but this is where I think the fun really starts. In Python, you know how to use Python at a sort of, at a high level, but once you really know what’s, what’s linking everything together underneath the surface, that’s why we need to dig underneath the surface.

00:27 Everything starts to make more sense and a lot of how objects we’ve seen last week that everything is an object in Python. So objects are clearly an important aspect of Python because everything is an object.

00:39 How are they linked to the various core functionality in Python? And the answer is almost always these special methods or Dunder methods, the ones that have underscore, underscore before and after.

00:52 The name, the most common one or rather one, you see, the first one you’re looking at classes is the __init__. We often, we often call them Dunder init or whatever, so it’s underscore underscore init underscore underscore.

01:05 That’s the one that initializes the object. But there are many more and they tell the objects or rather they tell Python how the object should present itself to all sorts of Python functionality.

01:25 For example, what happens when you type print( and then you put an object in the parentheses. Now I know you know how print() works and if you put in an integer there or a string or a list, you know what’s going to happen.

01:40 But what about more complex objects, especially classes you may have defined yourself? What happens when you put them inside the print()?

01:48 It’s up to you or whoever has designed the class to decide that. And that’s why we have, in this case, the Dunder method. Dunder __str__. Although the way an object presents itself when you want to display it, it’s actually more complex than that because there are two different special methods, Dunder, __str__, and Dunder __repr__.

02:10 This will all make sense as you go through today’s material. Then we’re going to look at other things. So for example, what happens when you add two objects together?

02:21 Of course, if you have two ints, five plus five, we know what’s going to happen. You’re probably familiar with string plus string, so you have the string, hello plus the string world, you’re going to get one string concatenation.

02:34 You may also be familiar what happens. For example, you have a list plus a list. Try it out.

02:41 Now in all of those cases, the plus is doing something different. You’re using the same symbol, the plus symbol, but in one of them it’s adding numbers. In another one, it’s putting strings together.

02:52 In another one, well, a spoiler alert, we’ll take the two lists and merge them into one list.

02:58 So how does Python know what to do when there’s a plus? It must do something different depending on what the objects on either side of the plus are. How does Python know that every object carries with it the information for Python to do that.

03:15 And this is another Dunder method in this case, it’s the Dunder __add__ for addition. And you can imagine there is one for every operator. Anyway, this is meant to be a short introductory video.

03:27 It’s giving you an idea of how we are going to start linking the objects and especially the Dunder methods that they have when you define a class with all sorts of Python functionality.

03:41 We’ll also at the end of the week, look at, this is one of my favorite topics, iteration in Python. You’re all familiar with the for loop, but how does Python deal with that?

03:51 What data types, what objects can you use in the for loop? Surprise surprise, there are Dunder methods that come in there as well. So everything is linked to Dunder methods.

04:01 That’s the theme for this week and for future weeks as well. We will carry on the momentum. Dunder methods will appear throughout this course.

You must own this product to join the conversation.