- Podcasts
- StoicCodeCast
- StoicCodeCast 9 - Python Classes
About this episodeEpisode #9, 2024-11-30 Step into the world of object-oriented programming (OOP) with Python classes! Whether you're new to OOP or looking…
Episode #9, 2024-11-30
Step into the world of object-oriented programming (OOP) with Python classes! Whether you're new to OOP or looking to deepen your understanding, this episode is your gateway to creating robust, reusable, and organized code.
Follow up the podcast with the book.
The best way to send money abroad or have virtual cards: create a wise account and select a bonus!
Support our work.
Get full access to Stoic Coding at costa86.substack.com/subscribe
Transcript
From the publisherThis transcript comes from the podcast’s own feed, shown as the publisher wrote it.
StoicCodeCast 9 - Python Classes
Yeah, and the book starts off with this great analogy of a cake mold, and I think it's such a good one. So you have the mold, which represents the class, and then each cake you make using that mold is like an instance of that class. Right. They can have different flavors, different decorations, but they all share that same fundamental shape.
So far, so good. But then things get even more interesting, I think, when we talk about inheritance. It's like passing down traits from parent to child. The book uses the example of a salesperson class that inherits from the employee class, which makes sense if you think about like a Dunder Mifflin.
Yeah, it's all about that code reuse and modeling those relationships we see in the real world. So that salesperson class automatically inherits all the properties and methods of the employee class. We don't have to rewrite it all, but it can also have its own unique attributes and methods, like maybe a sales target attribute or a calculate commission method.
Okay. So we've covered the basics of classes, inheritance, and even a little bit about composition. It's like a whole new level of understanding when it comes to organizing our code. But the book doesn't stop there, right? It goes on to explore these alternative approaches to traditional classes. And that's where things get really cool.
Yeah, yeah. Sometimes we need something a little simpler, a little more specialized than a full-blown class. And Python actually gives us a bunch of alternatives. Each one has its own strengths and its own use cases. So like we have named duples, which are those lightweight, immutable data structures.
Right. Immutability, yeah, that's key. It can be a good thing or a bad thing, depending on what you need. On one hand, it guarantees that the data is not going to change, right? Yeah. So you can be sure that it won't get accidentally modified. But on the other hand, if you do need to update the data, well, you have to create a whole new named tuple.
All right, so we've covered a lot of ground here. We talked about named tuples, data classes, and even Pydantic. But the journey's not over yet. We'll be right back to wrap things up and leave you with some final thoughts. Welcome back to Stoic Codecast. We've been exploring these Python classes, and I'm, I don't know, I'm just really struck by how these concepts can be used in so many different situations.
