Python for the .NET Developer Transcripts
Chapter: OOP: Object-Oriented Python
Lecture: Concept: Python interfaces and magic methods

Login or purchase this course to watch this video and the rest of the course contents.
0:00 While Python itself doesn't have separate interfaces outside of the concept of just maybe an abstract base class
0:07 you did see that there are certain types of functions we can optionally implement that have the same basic effect. For example, our parking lot
0:16 we wanted to be able to use it in a for in loop. How do we do that? Well, we have to add the __iter__ method here.
0:23 And that was super easy. We just created a little generator. We said for item in self.spot.items yield item, right? No big deal for us to do that
0:31 yet this is how we were able to use the lot within a for in loop to get those items back out. This method here, this __iter__
0:40 this is Python's equivalent of what would happen in C# if you implement IEnumerable<T>. There are tons of these special methods.
0:51 There's actually a guide over here by Rafe Kettler who did a really nice job of combing through each one explaining what it does what it's about
0:59 when you use it, and so on. This is a huge long article. I don't want to go through all of them. I just touched on a few of the key ones
1:05 __str__, __init__, __iter__, __repr__, alright. There's a bunch more that you'll want to know about or at least know that they exist
1:13 and then you go learn about 'em like, Oh, I actually probably could implement this and, you know, make it do whatever it is
1:18 you're trying to do. Our parking lot now effectively, in like C# terminology implements IEnumerable by using this magic method
1:26 which they're sometimes called, or dunder method.


Talk Python's Mastodon Michael Kennedy's Mastodon