Making Your Class Iterable
00:00
You’ve implemented membership checking with __contains__, but then you know this, this doesn’t support iteration because those are two different concepts.
00:07
So there is actually a fallback that if you do implement __iter__, which is primarily making your class iterable, you also get membership checking for free basically.
00:17
So the simplest way to get both of those functionalities is to implement instead of __contains__, just implement __iter__. Let’s try that out. So I go ahead and actually remove __contains__, and call it __iter__.
00:33 You’re not going to need to pass an item
00:35
and instead of doing a membership check in here, you just pass self.wares to the built-in iter() function. And with this, you get both. Let’s try it out.
00:47
I’m going to start a new Python REPL, and then again from storehouse import Storehouse,
00:56
paste the Storehouse object, and then try again. Is rose in storehouse? True. Alright, so membership checking still works.
01:08
And now you can also do for ware in storehouse:
01:12
print(ware) and this works as well.
01:16
So you may wonder why do you want to ever implement __contains__ instead of just doing __iter__ where you get more functionality?
01:24
The answer to that is that with __iter__, Python just falls back to iteration to do membership checking, which means it may be slow depending on what you’re working with, right?
01:33
So if you care for the growth of your merchant friend’s business and they implement their inventory in some sort of a hash data structure, like a dictionary or a set, then you wouldn’t want to just implement __iter__ because that’ll then default to iterating to check for membership, but then instead you want to implement both.
01:52
You want to do __contains__ for membership checking and a separate __iter__ for iteration.
01:58
And you have a similar situation with __getitem__, which I’ve also mentioned here. That’s another way you could also just implement __getitem__ and get access to membership checking.
02:08
But the primary special method that Python uses for membership checking is __contains__.
02:15
I’ve also mentioned that you can do it using __getitem__. So this is what an implementation with __getitem__ would look like.
02:23
And if you implement just __getitem__, then you get membership checks, iteration, and also indexing. So additionally to what you get with implementing __iter__, you also can use square brackets and access items by their index.
02:39
And why you wouldn’t want to just do that is the same as with __iter__, that it defaults to sequentially processing items, which means that you have less control over how membership checks work and how iteration works, and it’s generally less efficient.
02:53 So generators, custom classes, what would Shakespeare say to all of that? Maybe, “Enough, no more; ‘Tis not so sweet now as it was before.” Or hopefully, maybe just “Enough, no more.” I’ll stick with that and call it a course.
03:08 In the next lesson, you’ll go over what you learned, and I’ll give you a couple of other resources that you can continue learning with.
Become a Member to join the conversation.
