I love sets.
Of the four main Python collection types - the others being dict, list and tuple - sets are under-appreciated and under-used. They're more efficient than lists, when you don't need to preserve order or have dupes. And personally, I find it easier to reason about algorithms that use them.
But I have a more interesting story today, with code:
Yesterday I was writing a script that works with customer email addresses. So my program stores those in memory, as a set of strings.
But first I have to load them from a text file: one email address per line. And while I'm at it, skip the blank lines.
One way to do this: have a function that takes the file path as an argument, and returns a set - the set of email address strings.
That's not what I did. Instead, I did this:
- class CustomerEmails(set):
- def from_file(cls, path):
- with open(path) as handle:
- return cls(email for line in handle
- if (email := line.strip()) != '')
Use it like:
- emails = CustomerEmails.from_file('emails.txt')
Let's savor this:
I wrote this class without even thinking about it. Just in the zone, coding, and thinking about how I needed to compose everything to solve the problem. A moment of concentration, and I suddenly banged it out, then moved on. Only later did I realize this little class brings together a lot of interesting ideas.
I think that's important to be able to do. It's one thing to learn about new features of the language, in isolation. But in real code, you don't use different language features in isolation, do you?
Nope. You moosh them all together. Pretty much on every line of code.
Something to think about as you continue growing in your craft.
But before you can moosh them, you first have to learn those tools and features of the language.
Our professional trainings range from $3,000 to $12,000. To find out if Powerful Python is right for you, schedule a 30-minute discovery call with our team here: