Podcast Lookup

StoicCodeCast 9 - Python Classes

StoicCodeCast

  • November 30, 2024
  • 13 min
Episode page
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 publisher

This transcript comes from the podcast’s own feed, shown as the publisher wrote it.

StoicCodeCast 9 - Python Classes

00:00—
  1. Speaker 1

    Welcome back to Stoic Codecast. I am so excited for our deep dive today. We're continuing to work through the Python Straight to the Point book.

  2. Speaker 2

    Yes.

  3. Speaker 1

    And you can find that through a link in the description if you want to follow along and grab a copy.

  4. Speaker 2

    Absolutely.

  5. Speaker 1

    Today we are talking about something really cool, classes, which are like the core building blocks of object-oriented programming in Python.

  6. Speaker 2

    Yeah, they're so cool. What's fascinating to me is that they really let us represent the world around us in code. We categorize things based on shared characteristics and behaviors, and that's what classes allow us to do in code, right?

  7. Speaker 1

    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.

  8. Speaker 2

    Exactly, and that's what makes it so powerful, right? It lets us create these blueprints for objects. Instead of writing the same code over and over again for each individual object, we can define it once in the class and then create multiple instances.

  9. Speaker 1

    Okay, so then the book sort of transitions from dictionaries to classes and talks about how classes give us more structure and functionality.

  10. Speaker 2

    Yeah, and dictionaries are great for those key value pairs, but they don't have that ability to encapsulate data and behavior. That's what classes give us, right?

  11. Speaker 1

    Right. And I think that progression is so helpful for understanding the difference. It's like you're leveling up in how you represent data. OK, so let's jump into a concrete example. The book has this sample employee class that it walks through.

  12. Speaker 2

    Oh, perfect example. Yeah. We've got the init method, which is the constructor. Right. So that gets called automatically when we create a new instance of that class. It initializes the object's attributes like the employee's name and their salary, the company they work for.

  13. Speaker 1

    And then we have the self keyword, which I always struggle with a little bit. It's like it refers to the instance itself, but it's still it's a little fuzzy for me sometimes.

  14. Speaker 2

    Yeah. Think of it like the object is referring to itself. So like me or I within the context of that object. So like it allows the object to access its own attributes and methods, things like that.

  15. Speaker 1

    So if we had two employees, like a Jim and a Dwight, self is like their individual name tag.

  16. Speaker 2

    Precisely.

  17. Speaker 1

    Even though they're both part of that same employee class.

  18. Speaker 2

    Yeah. And that gets us to attributes. So that's like the characteristics of each employee, like their name, salary and company. It's like variables, but specific to that instance.

  19. Speaker 1

    And then we have methods, which are like those functions that are kind of living inside of the class, right? The actions that the object can perform. The book has this example of a promote method, which I'm guessing increases the salary.

  20. Speaker 2

    Right, exactly. And then we have, you know, those special methods like string, which defines like how the object is represented as a string. So like giving it a way to like introduce itself, you know.

  21. Speaker 1

    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.

  22. Speaker 2

    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.

  23. Speaker 1

    So our salesperson is still an employee, but with some special skills.

  24. Speaker 2

    Right.

  25. Speaker 1

    It's like adding another layer to that cake. But then, oh, the book introduces method overriding. What's that all about?

  26. Speaker 2

    Yeah. So method overriding allows that child class, so our salesperson, to modify an inherited method. So like, for example, the salesperson class might override that string method to include their sales performance in their introduction. Right.

  27. Speaker 1

    So it's like they take the basic intro from employee and they give it like a salesy spin.

  28. Speaker 2

    Exactly. It's a customization for that child class.

  29. Speaker 1

    And while we're on the topic of inheritance, the book also mentions multiple inheritance where a class can inherit from multiple parent classes and then multilevel inheritance, which is like a whole family tree of classes.

  30. Speaker 2

    Yeah, and I think that's where it's really important to be careful. You know, as our code base grows larger, inheritance can be amazing, but too much of it can really create a tangled web that's hard to maintain. Sometimes we need to consider other approaches like maybe composition.

  31. Speaker 1

    Composition? What's that? Does that have anything to do with Dendermifflin selling paper and printers?

  32. Speaker 2

    It does. Like, imagine we're modeling those products that they sell. With inheritance, we might create a paper class, a printer class, both inheriting from some generic product class, but that could get really messy if we have a ton of different products.

  33. Speaker 1

    So instead of forcing this hierarchy, we use composition.

  34. Speaker 2

    Right.

  35. Speaker 1

    So we'd have a product class and a salesperson class, and each salesperson has a list of product objects they sell.

  36. Speaker 2

    Exactly. That's composition. It's much more flexible and modular. Like we can combine those different product objects to represent like the inventory of each salesperson.

  37. Speaker 1

    Oh, that's a great way to think about it. It's like we're building with Legos, right? So it seems like composition gives us a lot more freedom and control over those relationships.

  38. Speaker 2

    Yeah.

  39. Speaker 1

    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.

  40. Speaker 2

    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.

  41. Speaker 1

    And data classes, which help us reduce boilerplate code.

  42. Speaker 2

    And then there's Pydantic. That's a third-party library, but it brings professional-grade data validation to the table.

  43. Speaker 1

    Okay, so I'm definitely intrigued. Let's take a closer look at these alternative approaches, I think, after a quick break, and see how we can use them.

  44. Speaker 2

    Sounds great.

  45. Speaker 1

    Okay, so let's unpack these alternative approaches to classes, starting with named tuples. The book describes them as tuples with superpowers, which I think is a pretty great way to put it.

  46. Speaker 2

    I love that. Yeah, they're amazing because you get that simplicity and efficiency of tuples, but then you also have the readability and clarity of having named fields.

  47. Speaker 1

    Yeah, it's like the best of both worlds. So the book gives an example where we define a name tuple called employee with fields for name and salary. It's so much cleaner than setting up a whole class with a constructor and everything.

  48. Speaker 2

    Right. And you can access those fields using dot notation, just like with classes. So you can write like employee dot name or employee dot salary.

  49. Speaker 1

    But there's a catch, right? Named tuples are immutable. The book really stresses that.

  50. Speaker 2

    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.

  51. Speaker 1

    So it's a trade-off, like safety versus flexibility. I guess it makes them perfect for when you have data that shouldn't change, like configuration settings.

  52. Speaker 2

    Absolutely. And they're also very efficient, especially when you're working with massive amounts of data. Since they're immutable, Python can optimize how it uses memory and how quickly it can access that data.

  53. Speaker 1

    That makes sense. Okay, so name tuples are great for that kind of lightweight, unchanging data. But what about data classes? The book talks about how they can help us cut down on boilerplate code.

  54. Speaker 2

    Yeah, data classes are like classes but streamlined. They're specifically designed for defining classes that are mainly focused on storing data. So you don't have to write all that extra code for like the constructor and all those other methods that regular classes need.

  55. Speaker 1

    Yeah, the book shows how we can just define a data class called employee again and just list out the fields we want, name, salary, company, and Python takes care of all the behind-the-scenes stuff.

  56. Speaker 2

    Exactly. It's like magic. Python does the heavy lifting for you.

  57. Speaker 1

    And data classes have built-in support for type hints, which is fantastic.

  58. Speaker 2

    Yeah, type hints are becoming so important, you know.

  59. Speaker 1

    Absolutely.

  60. Speaker 2

    They help you write clearer code, more robust code, and they're basically built right into data classes.

  61. Speaker 1

    But the book also mentions that we don't get as much granular control with data classes compared to regular classes. Is that a problem?

  62. Speaker 2

    It depends. If you need that fine-grained control, then a regular class is probably the way to go. But for most cases, I think data classes are more than enough. They're so much simpler.

  63. Speaker 1

    So again, it's a trade-off. We get conciseness and clarity, but we might give up some control.

  64. Speaker 2

    Exactly. It's like anything in programming, right? You happen to pick the right tool for the job.

  65. Speaker 1

    Speaking of the right tool, we haven't even talked about PyDampic yet. I know it's a third-party library, but it seems like everyone's using it these days.

  66. Speaker 2

    It's becoming so popular, especially for, you know, serious projects and for good reason. It's all about data validation. It helps you define those data models and then make sure that any data you work with actually conforms to those models.

  67. Speaker 1

    It's like having a bodyguard for your data, making sure nothing shady gets in.

  68. Speaker 2

    Exactly. And the best part is Pydantic does all of that automatically. You don't have to write a ton of custom validation code.

  69. Speaker 1

    So the book is this example where we define an employee class using Pydantic. And one of the attributes, branch, has to be one of specific values. Scranton, Stanford, or Buffalo. I love that level of control.

  70. Speaker 2

    Right, so you're defining exactly what your data should look like. Pydantic is all about that. And it's also incredibly fast and efficient.

  71. Speaker 1

    So we get amazing validation and performance, what's not to love.

  72. Speaker 2

    Right, and it's always evolving. The developers are constantly adding new features and improvements.

  73. Speaker 1

    It's so cool to see all the different tools that Python gives us to handle data. It really is like having a toolbox.

  74. Speaker 2

    And like any good craftsperson, you need to know which tool to use when.

  75. Speaker 1

    So, listeners, if you want to learn more about all this, I highly recommend checking out the Python Straight to the Point book. We'll put a link in the show notes.

  76. Speaker 2

    It's a fantastic resource, especially if you want to improve your understanding of data structures and object-oriented programming.

  77. Speaker 1

    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.

  78. Speaker 2

    It is. It's really cool. And they might seem abstract at first, but they're all around us, right?

  79. Speaker 1

    Totally. We've talked about, like, employees at a company, products they sell, even designing video game elements.

  80. Speaker 2

    Yeah. And it's important to remember there's no single right answer, right? Like whether we use a traditional class or maybe a named tuple or data class or Pydantic, it really depends on what we're trying to build.

  81. Speaker 1

    Yeah, it's like that toolbox analogy again, right? You wouldn't use a hammer to screw in a bolt. You have to know which tool is best for the job.

  82. Speaker 2

    Right. And the Python straight to the point book really emphasizes that. It gives you all these explanations and examples that show you when you might use one over the other.

  83. Speaker 1

    Yeah, the book has been so helpful for me. Like, it really made these concepts click in a way they hadn't before.

  84. Speaker 2

    One thing that really stood out to me was the discussion on inheritance versus composition. It's so easy to go overboard with inheritance and end up with this big, complicated hierarchy.

  85. Speaker 1

    Oh, yeah, definitely. And I think the example from the book with the Dunder Mifflin products really showed how composition can be so much better.

  86. Speaker 2

    Totally, yeah. Instead of creating all those subclasses for every single product, you can use composition to make those relationships more flexible.

  87. Speaker 1

    Right. It's much more elegant and it's easier to change and adapt as your project grows, I think. So while traditional classes are really powerful, I'm also excited about these alternatives like named tuples and data classes and pedantic.

  88. Speaker 2

    Yeah, named tuples are perfect when you need that lightweight data that isn't going to change.

  89. Speaker 1

    And data classes, they just make defining classes so much easier. Less code to write.

  90. Speaker 2

    And Pydantic, oh man, Pydantic is a game changer when it comes to making sure your data is correct.

  91. Speaker 1

    Yeah, absolutely. So to our listeners, if you're trying to learn more about Python and really improve your skills, definitely grab a copy of Python straight to the point. We'll put a link in the description. It's a great book.

  92. Speaker 2

    It really is. And remember, mastering these concepts takes time and practice. So don't be afraid to try things out, experiment, see what works for you. There's always something new to learn in Python.

  93. Speaker 1

    That's right. Keep learning, keep experimenting. And with that, I think we've come to the end of our deep dive on Python classes here on Stoic CodeCast. We hope you enjoyed it and learned something new. Until next time, happy coding.