Latest / Talk Python To Me / #404: Clean Code in Python
Transcript
- 0:00Clean code is one of those aspects of your programming career that's easy to put on the back burner, sometimes more by management than yourself.
- 0:07But it's important in the short term for writing more debuggable and readable code.
- 0:11And it's important in the long run for avoiding having your program take on the dreaded legacy code moniker.
- 0:18We're fortunate to have Bob Bilderbos back on the show.
- 0:22He's been thinking and writing about clean code and Python a lot lately.
- 0:26And we'll dive into a bunch of tips that you can use right away to make your code cleaner, more fun to work with, easier to read, and more maintainable.
- 0:33This is Talk Python To Me, episode 404, recorded February 12th, 2023.
- 0:53Welcome to Talk Python To Me, a weekly podcast on Python.
- 0:56This is your host, Michael Kennedy.
- 0:58Follow me on Mastodon, where I'm @mkennedy, and follow the podcast using @talkpython, both on fosstodon.org.
- 1:05Be careful with impersonating accounts on other instances.
- 1:08There are many.
- 1:09Keep up with the show and listen to over seven years of past episodes at talkpython.fm.
- 1:14We've started streaming most of our episodes live on YouTube.
- 1:18Subscribe to our YouTube channel over at talkpython.fm/youtube to get notified about upcoming shows and be part of that episode.
- 1:27This episode of Talk Python is brought to you by Taipy.
- 1:29They're here to take on the challenge of rapidly transforming a Bayer algorithm in Python into a full-fledged decision support system.
- 1:36Check them out at talkpython.fm/taipy, T-A-I-P-Y.
- 1:41And it's also brought to you by my friends over at Brilliant.
- 1:44Stay on top of technology and raise your value to employers, or just learn something fun in STEM at Brilliant.org.
- 1:51Visit talkpython.fm/brilliant to get 20% off an annual premium subscription.
- 1:57Bob, welcome back to Talk Python To Me.
- 2:00Thanks for having me back.
- 2:01I'm excited to be here.
- 2:02Yeah, it's really great to have you back.
- 2:04You've been on a couple of times.
- 2:05We started, I believe, our first discussion on the show way back when was around 100 days of code.
- 2:12We went on quite the 100 days of code journey, writing a couple of really, really long courses that were well-received,
- 2:20but it took a long, you know, like nine months for us to write, which was amazing.
- 2:23And we're back together.
- 2:25It's great to catch up.
- 2:25Yeah, indeed.
- 2:26Yeah, that goes way back to 100 days of Python and two courses that came from it.
- 2:31And I think we also did a Django episode.
- 2:33Yeah.
- 2:34That's right.
- 2:35We did do a Django episode as well.
- 2:36Good stuff.
- 2:37So this time we're back to talk about code quality, writing clean code.
- 2:42What are some of the tools?
- 2:44And also what are some of the techniques, maybe even mindset people are using to help them write clean code, better code, both for yourself to make yourself happy and as well to be a better teammate if you're working in a team, right?
- 2:56Yeah.
- 2:56This topic really excites me.
- 2:58It's in my everyday work.
- 2:59And I think there's a lot to gain from it.
- 3:01So, yeah.
- 3:02Excited to share today.
- 3:03I am too.
- 3:04It's something I really, really care a lot about.
- 3:07It's one of those topics that I think is long lived, which is rare in software development.
- 3:12It's not the latest JavaScript framework or something that's going to last for a year or two.
- 3:18These ideas are the kind of ideas that just no matter what you're doing, even if you don't do Python, if you do something else, it's very likely that these are solid foundations.
- 3:26Some of the ideas will be specifically for Python, but many of them not.
- 3:29Yeah.
- 3:30Timeless stuff.
- 3:31Yeah.
- 3:31Those are the good ones that are worth putting your time into learning.
- 3:34Now, before we jump into that topic, which is going to be good, maybe just tell people what you're up to.
- 3:39These days, so I left Oracle a while back.
- 3:42I think we spoke about that in 2020.
- 3:44So, I've been working on PyBytes full-time for almost three years.
- 3:47Congratulations.
- 3:48I know you and I spoke about this beforehand, and it was really a big step for you.
- 3:53It was.
- 3:54And something you were absolutely looking forward to.
- 3:56But just walking away from a good-paying job, that's scary.
- 4:00Stuff.
- 4:01So, congratulations.
- 4:02Yeah, thanks.
- 4:03This is also around the time that, as PyBytes, we pivoted to doing coaching.
- 4:07So, helping people one-on-one.
- 4:09We created our PyBites Developer Mindset Program.
- 4:12It's a coaching program.
- 4:13And that really took off.
- 4:15And we have worked now with 100-plus people.
- 4:17But I'm super excited about every day when I wake up.
- 4:21Because helping people overcoming the tutorial paralysis, embracing imposter syndrome, helping them shipping to projects end-to-end is just the best thing ever.
- 4:30Yeah.
- 4:30To see people grow and become more confident and get better.
- 4:34And, yeah, it's really excellent work, isn't it?
- 4:36Yeah, it's really fulfilling.
- 4:37Yeah.
- 4:38So, congratulations.
- 4:39It's awesome to hear that you're doing that now.
- 4:41And you can put all of your energy, not just your side hustle energy, into these types of things, which I can tell you feels real good.
- 4:49You know how it is, right?
- 4:50I do, I do.
- 4:51I've been in it for a while.
- 4:52Very fortunate.
- 4:53Okay.
- 4:53So, let's start this off with looking at your article.
- 4:58But before we do, Toon Army Captain has a really great way to sort of kick off this topic, I believe.
- 5:03Clean Code has less or fewer WTFs per line.
- 5:07How about that?
- 5:08Nice.
- 5:08So, yeah, I think that's going to be a theme here.
- 5:12Let's go ahead and jump into it.
- 5:14I reached out to you and said, hey, let's get together and talk about this.
- 5:17Because I knew that you were passionate about these things.
- 5:20But then you also put together a blog post here called Tips for Clean Code in Python.
- 5:26And I thought, okay, that's interesting.
- 5:27Let me start flipping through here.
- 5:29And there's just a bunch of nice ideas that, you know, resonate with me.
- 5:32So, what I thought we could do is maybe you could set the stage.
- 5:36Like, what the heck is Clean Code?
- 5:37Toon Army did do a pretty good job of kind of giving us the colloquial term.
- 5:41But, you know, maybe something a little more saphoric and formal we could go with.
- 5:45Set the stage for what is Clean Code?
- 5:47Why does it matter?
- 5:48And then we could dive into some of the stuff you put into your article.
- 5:52And then also, you and I both threw some ideas in after, like, kind of expanded as well.
- 5:57So, let's start with what is this Clean Code thing?
- 5:59Yeah.
- 6:00To set off the stage.
- 6:01So, Clean Code is really when you write code that's easier to maintain, easier to test, easier to update.
- 6:07Because, as we all know, a project usually starts simple.
- 6:11But over time, it grows.
- 6:12New requirements comes in.
- 6:14You constantly have to change things.
- 6:16And, yeah, the cleanness of your code will definitely determine how easy you can move a project forward, how you can make changes.
- 6:25And, of course, it's also a contribution to your team to make it a more pleasant experience and decrease the amount of WTFs.
- 6:34Yeah, you don't want it.
- 6:35The fewer, the better.
- 6:36Although, maybe you'll leave in one or two in just for a good war story at a conference five years from now.
- 6:40But you don't want too many of those.
- 6:42And I was a free.
- 6:42Yeah.
- 6:42I think it's appreciating the fact that if you do a good job, what you're building will live a long time.
- 6:49Right?
- 6:49It will possibly be still the main focus in five years from now.
- 6:54And you don't always know what that's going to be.
- 6:56Right?
- 6:56It could be, oh, here's a little side project.
- 6:59It's just going to pull in some data.
- 7:01And, like, you know, you show it to somebody at work.
- 7:03I'm like, you know what?
- 7:03That's great.
- 7:04Ship it.
- 7:04Like, whoa, whoa, whoa, whoa.
- 7:05Ship it.
- 7:06No, no.
- 7:06I just threw it together.
- 7:07It's good.
- 7:08Let's go.
- 7:09Like, we're in a hurry, you know?
- 7:10And then that just starts to build and layer on, like, a sediment that just makes it harder and less fun to work with.
- 7:17And so as you just think about this thing is small now, but as it grows, what are both the practices and maybe technological programming things to bring and put in place so that as it grows, you don't start to just really come to a screeching halt to make changes.
- 7:33Tests start to fail randomly as you just touch it in various places.
- 7:38Things like that, right?
- 7:38Yeah.
- 7:39It was also kind of my introduction to Python in 2012, so almost 11 years ago when I made this automation framework at work.
- 7:48And I was a big fan of Perl back then.
- 7:52I guess that goes back to my Unix shell scripting initial programming exploration.
- 7:57And it was not maintainable at all, right?
- 8:00There were no classes in Perl.
- 8:01And it was just a mess.
- 8:02Also, of course, because I did no Perl maybe very well.
- 8:05But then I discovered Python and I could refactor it, make it more modular.
- 8:09And that project was then just a joy to work with.
- 8:13And maybe that says a lot about Python as well, that it does inherently things well.
- 8:18Because if you type import this in the REPL and you get the send of Python, a lot of these statements sound so simple, but they're actually so profound.
- 8:26And they actually tell you a lot about writing clean code, in a sense.
- 8:30So anyway, but I digress.
- 8:32They do.
- 8:33They also talk a lot about the community that cares enough that, you know, you have these sorts of commands in the language itself that remind you to think about writing clean code.
- 8:43And going way back to the late 80s, Gito, when he came up with it, really broke with tradition to use white space for the code structure.
- 8:53And I think it's a little bit less so now, but back then, that was very much to encourage people to write code in a way that was very readable.
- 9:01Whereas, you know, I say less so than now, because you've got a lot of IDEs and stuff where you can push a button and it'll auto format it.
- 9:07And you've got things like black.
- 9:08And, you know, if you had curly braces, like it could still be quickly put into a pretty shape now.
- 9:13But in the early days, that was a really important concept of it.
- 9:16These days were spoiled, right?
- 9:18Having black out of formatting and all that.
- 9:20When we started, that was not the case.
- 9:22How did you react to the white space thing when you first?
- 9:26At first, I hated it.
- 9:27I just really didn't like it.
- 9:28I thought this is just weird.
- 9:29Like, okay, everything else about this language seems pretty nice, but this is weird stuff.
- 9:34Then I started using editors that understood it.
- 9:36It's like, okay, well, I just hit, you know, colon and enter.
- 9:39And it kind of does it for me automatically.
- 9:41And sure, there's three spaces, four spaces.
- 9:43But if I backspace, then it goes back forward.
- 9:46It kind of treats the structure.
- 9:48The tools know the structure.
- 9:49And it makes it really nice.
- 9:51And at the time, this is long ago, I was doing C#, which is an okay language.
- 9:55It's pretty good.
- 9:55But it has all the symbols and formulas and all the angle brackets and all the stuff that you would do, right?
- 10:02It's not quite C++, but it's pretty close.
- 10:04And when I came to Python, I was like, gosh, it's just weird that a lot of that stuff's not here.
- 10:08And it seemed kind of out of place.
- 10:10But then when I went back, I realized, wait, these languages are lying to me.
- 10:14They're saying I need all these symbols to make it hold together.
- 10:16And you just don't.
- 10:17And it's so much nicer to look at code that is not laden with these support structures.
- 10:23So a bit of a diversion.
- 10:25But yeah, I reacted weirdly to it.
- 10:27But after about a week, I'm like, but it's better.
- 10:30It really is.
- 10:31Even though I'm so weirded out by it, it's better.
- 10:33And then, you know, pretty quickly, I'm like, yeah, this is sweet.
- 10:35I'm going with this.
- 10:36Yeah, same here.
- 10:37Indeed.
- 10:38All right.
- 10:39Well, let's start, I guess, start at the top with your article.
- 10:42What inspired you, by the way, to write it right now?
- 10:45Because it's pretty recent, isn't it?
- 10:46Yeah, that's a good question.
- 10:47It's not the first time I've been writing content about clean code and stuff.
- 10:53In 2016, I actually did a formal certification, building maintainable software from the SIG group.
- 11:01Software Improvement Group.
- 11:03They have formal certifications.
- 11:05And yeah, it's a passion of mine, clean code.
- 11:08It's also coming back a lot in the coaching I do day to day, helping people.
- 11:12So it's not the first content piece.
- 11:14But I think this one kind of summarizes in a bit more of a concise way.
- 11:19And of course, there's way much more to it.
- 11:22But I think the 10 points here get you pretty far.
- 11:25So those are pretty important.
- 11:27They sure are.
- 11:28So let's start with one that you already kind of laid this out.
- 11:32Like so many of these techniques, they are like, they sound so simple.
- 11:35And I guess, sure.
- 11:37Obviously, Michael, I wouldn't write a 1000 line function.
- 11:43And you're like, but it's already 700.
- 11:46How did this happen?
- 11:47Right?
- 11:47How do we get here?
- 11:48And the first one solidly falls into that realm, which is smaller units, smaller functions,
- 11:54smaller classes, smaller modules, single responsibility principle, all that, right?
- 11:58Tell us about this.
- 11:59Yeah, so indeed, a function or a unit of code should ideally do one thing.
- 12:04And just a side note, right?
- 12:06That's usually not how we start.
- 12:08Sometimes I do want to make this side note that when we're figuring out a design, it can
- 12:14definitely happen that you have very large units because you're basically trying to figure
- 12:18out what you're building, right?
- 12:19And that's where refactoring comes in.
- 12:21But we'll talk about that a bit later.
- 12:23But yeah, I give a very simple example in this article, right?
- 12:26Where a function parses a CSV file, builds up a result list and also prints it out, right?
- 12:31So now this function is doing three things and it probably works and it's fine, but you
- 12:37cannot really easily reuse something that's doing three things, right?
- 12:41So if you want to plug in this function now into something else, there's a lot of things
- 12:45happening that probably doesn't make it a candidate to reuse in its current shape or form.
- 12:49It's also harder to test because now you have to test parsing and printing.
- 12:54And this is kind of a silly example, but just think about another thing you have in your
- 12:59code that's doing multiple things.
- 13:00It will just be harder to test because there's a lot more going on.
- 13:03And yeah, as you said, right?
- 13:05These things sound very intuitive and easy to grasp.
- 13:09Yet when we look at code bases, right?
- 13:11This happens all the time.
- 13:12And yeah, so if something's harder to test, it's harder to guarantee that it works.
- 13:17If it's hard to extend and when requirements change, it will be harder to update.
- 13:21Yeah.
- 13:22Yeah.
- 13:22It's harder to maintain.
- 13:23It's harder to reason about the more things that it's doing, the more state that it might
- 13:29be changing.
- 13:29It's harder to onboard new people.
- 13:31It's just the whole spectrum.
- 13:35This portion of Talk Python Amey is brought to you by TypePy.
- 13:38TypePy is the next generation open source Python application builder.
- 13:42With TypePy, you can turn data and AI algorithms into full web apps in no time.
- 13:47Here's how it works.
- 13:48You start with a bare algorithm written in Python.
- 13:51You then use TypePy's innovative tool set that enables Python developers to build interactive
- 13:56end user applications quickly.
- 13:58There's a visual designer to develop highly interactive GUIs ready for production.
- 14:03And for inbound data streams, you can program against the TypePy core layer as well.
- 14:07TypePy core provides intelligent pipeline management, data caching, and scenario and cycle management
- 14:13facilities.
- 14:13That's it.
- 14:14You'll have transformed a bare algorithm into a full-fledged decision support system for
- 14:19end users.
- 14:20TypePy is pure Python and open source.
- 14:23And you install it with a simple pip install TypePy.
- 14:26For large organizations that need fine-grained control and authorization around their data,
- 14:30there is a paid TypePy Enterprise Edition.
- 14:32But the TypePy core and GUI described above is completely free to use.
- 14:36Learn more and get started by visiting talkpython.fm/taipy.
- 14:41That's T-A-I-P-Y.
- 14:43The link's in your show notes.
- 14:44Thank you to TypePy for sponsoring the show.
- 14:47And it's write functions that are 10 lines long, not 100 lines long.
- 14:55Now, I'd like to hear your thoughts on this, but my feeling about all of this stuff is,
- 14:59as a general rule, this is what you should do.
- 15:02There will be situations where you might, you know, this is just, I can't think of something
- 15:06better.
- 15:06Or it's way over the top to try to, like, adhere to all of these rules within this small
- 15:12context.
- 15:12I could tell you, like, on the Talk Python training website, there's a couple of places where
- 15:18it's like 75, 80, 90 lines of code for a single function.
- 15:22I'm just like, this is so bad.
- 15:24I just, I wish it wasn't like this, but it's just weirdly unique.
- 15:29And, but there's, you know, 19,000 other lines of code that are all three lines, five lines
- 15:35type of thing.
- 15:36Right?
- 15:36So it's, for me, at least, this isn't a 100% or you're doing it wrong.
- 15:41It's a 98% or 95% of the time you should be doing this.
- 15:45There may be some weird case.
- 15:46And I don't know.
- 15:47What do you, how do you feel about that in general on these topics?
- 15:50Yeah, as I said before, sometimes you just don't get the design right from the start.
- 15:54So inevitably you're going to write some longer classes or functions.
- 15:58But then when you break them out in smaller units, the other thing I have to say about
- 16:02that is it gives you an opportunity to better documents.
- 16:05Because in this example, if you break this long function that's doing three things out
- 16:10into three separate functions, parse CSV, buildup list, bad name, but you get the point,
- 16:16and print results, then all of a sudden we have three units now and every unit has a
- 16:20name.
- 16:20Hence, when you go back after a year and look at that code, which always happens and you
- 16:25always wonder, what the heck was I thinking?
- 16:27Now you add a glance just by looking at the function numbers, you have just already a better
- 16:33idea.
- 16:33These functions, you can give doc strings as well.
- 16:36So at a glance, you have just a much easier time figuring out what this was about.
- 16:41Yeah, I appreciate you actually gave a shout out to me in this first piece here, which
- 16:44I didn't expect.
- 16:45But one of my rules, you know, and I see in this article, you referenced Martin Fowler's
- 16:50refactoring book.
- 16:51And one of the most important things that came out of that is not all the refactorings, but
- 16:54this idea of code smells, right?
- 16:56And maybe tell people real quick about code smells.
- 17:00Yeah, that's a funny name, right?
- 17:02Code smell.
- 17:02It is.
- 17:03Code that is smelling and this analogy I came up with.
- 17:06But yeah, basically it's code that's not following these best practices and it's just not maintainable,
- 17:13not easy to extend, going back to these principles we mentioned.
- 17:16So yeah, not following these guidelines.
- 17:18That code can be smelly.
- 17:20It can be as code smell.
- 17:21Yeah.
- 17:22Yeah.
- 17:22Well, the idea is like, it's not actually not working.
- 17:24It's just not nice.
- 17:26You know, you kind of turn your nose up at it when you see it, but it's, you can't say it's
- 17:30broken because it is working.
- 17:32It's a good point.
- 17:33It smells a little bit off, right?
- 17:35It's not quite broken code, but it's sour.
- 17:38I don't know.
- 17:39And so the reason I bring this up is one of my rules of thumb here that I love, and I think
- 17:44you're kind of hinting that with this little shout out that you gave me in this article
- 17:47is when we come across these code smells often, especially when you're earlier in your career,
- 17:53often the reaction or the first thought is, you know, this part is not nice.
- 17:58This part is complicated.
- 17:59This part is hard to maintain or hard to understand.
- 18:01So I need to put a nice comment.
- 18:02I've been told, comment your code.
- 18:04Don't be a jerk.
- 18:05Comment it.
- 18:05So I'm going to put a big comment that describes why it's really bad and all that.
- 18:09And in the code smells sort of world, these code comments can be seen as deodorant for
- 18:15the smell.
- 18:16Like it doesn't remove the smell, but it kind of obscures.
- 18:19Like it's not as bad if you saw the comment saying why the code that follows is bad, but maybe
- 18:25you could just make it not need a comment.
- 18:27And a lot of times, like you just said, that's, well, these five lines of code need an explanation
- 18:32because they're mixed in with all this.
- 18:34But if they had a function that had a name, whose name was basically the comment, well,
- 18:39then you don't need a comment because the name is the comment and it's now small and understandable,
- 18:43right?
- 18:43Like there's a lot of these little iterative things that go on.
- 18:46Exactly.
- 18:46Yeah.
- 18:46That's a great point.
- 18:47And it's, it's very easy to make that refactoring, just turn the comment into a function and we
- 18:52have created another unit.
- 18:53We can extend, we can test, et cetera.
- 18:55So yeah, it's interesting because there are kind of mixed feelings about commands.
- 18:59Some people say, voracious, command your code.
- 19:01But mostly in their literature, I'm reading that commands are actually not that good.
- 19:06And sometimes even labeled as a code smell in the sense that your code should be kind of
- 19:10intuitive.
- 19:11I think it's going back to that, like half of the time when people are actually putting
- 19:15those comments, it's because there's something kind of messy and they're trying to help you
- 19:19get over that mess or deal with that mess rather than just fixing the mess, you know?
- 19:23Yeah.
- 19:24It might be an indication of a bad design, right?
- 19:26Yes, exactly.
- 19:27So you might even have to go back to the drawing board.
- 19:28Yeah.
- 19:29But that said, I definitely see a place for commands where sometimes you just want to remind
- 19:33yourself and of course your team members, not what the code is.
- 19:37They can read the code obviously, but why you took that decision, what the,
- 19:41and there might just be some extra context that you need to give.
- 19:44Yeah.
- 19:44And it's also different than doc strings potentially, right?
- 19:48Which is kind of a form of a comment, but it's meant to talk about the API and not cover
- 19:53up some weird thing you had to do in the middle.
- 19:55Exactly.
- 19:56Yeah.
- 19:56Doc strings are basically your API documentation.
- 19:59Yeah.
- 19:59Yeah, exactly.
- 20:00All right.
- 20:01Well, good, good stuff.
- 20:03I mean, even GitHub Copilot and those things are starting to use these comments as a way to
- 20:07say, well, I'll just make a function that kind of is named what the comment is.
- 20:11You know, so it's even our robot overlords are going to, going to help us by taking these
- 20:18comments and removing them, putting the functions there.
- 20:20Help us not replace us, right?
- 20:22Yeah.
- 20:22Hopefully.
- 20:23Fingers crossed.
- 20:24Another thing related to this, strongly related to this, because the more things you have
- 20:29that are smaller units, you're probably still going to have about the same amount of codes.
- 20:32You have more functions, more classes, more modules, files, and so on.
- 20:36What gets tricky can be naming them, right?
- 20:39So choosing good names also, I think, probably is a message in here that people need to think
- 20:44about.
- 20:44What are your thoughts on that?
- 20:45Yeah, it's funny.
- 20:46Again, another thing that sounds very intuitive, but wasn't there a saying like there are two
- 20:51or three complex problems in science.
- 20:53One other one I'm going to remember and naming things.
- 20:56Yeah.
- 20:57There's two problems that are hard in computer science.
- 21:00Naming things, cache and validation, and off by one errors.
- 21:04Thank you.
- 21:05Yeah.
- 21:05So naming is up there, right?
- 21:07It is.
- 21:08That's right.
- 21:09I think it goes back again to knowing your design and what you're building.
- 21:12And sometimes you don't really have that figured out.
- 21:15Hence, naming becomes more difficult.
- 21:16But I think that's an experience thing.
- 21:18You get better at it the more code you write.
- 21:21Maybe we can touch on the magic numbers there.
- 21:24Yeah, absolutely.
- 21:24That's the next one is magic numbers.
- 21:26And magic, the number part, I think, could be expanded a little bit broader even.
- 21:31You know, magic, static values, right?
- 21:34But if you see 360, you know, think just in the code, like, well, is that like a degrees?
- 21:40Right.
- 21:41What is that?
- 21:41Probably degrees, but it might not be.
- 21:43I don't know.
- 21:44So yeah, magic numbers.
- 21:45That's your next recommendation.
- 21:46I'm presuming to avoid them.
- 21:48Yes.
- 21:48So when you see some sort of random integer in the code, which might be an intuitive one,
- 21:54like 365 number of days in a year, which we might kind of guess.
- 21:59But yeah, any guessing in code is not good.
- 22:02So it's very easy to then replace that 365 integer with a number on the score of days in a year
- 22:10or something like that.
- 22:11Something that expresses meaning.
- 22:12Yeah.
- 22:13And again, this is kind of an obvious example.
- 22:15But if you have some magic five or six or 87 in there, the reader of this code probably
- 22:20doesn't know what that means.
- 22:21Right.
- 22:21So if you have a constant uppercase with underscores as per PEP 8 defined at the top
- 22:26of your module, I usually put them in the top of the module.
- 22:29And then in the code, you see like max underscore, blah, blah, blah, a real name.
- 22:34And then immediately makes sense.
- 22:36Right.
- 22:36And it's also a nice way of grouping those variables in one place.
- 22:41I said top of the module.
- 22:42But if you have many, you could also have a constants.py in your package to group them.
- 22:47You can also use enums to group various constants together if they logically pertain to the same
- 22:53group.
- 22:54And again, all more readable.
- 22:55Yeah.
- 22:56Enums are a great recommendation because instead of saying, well, the default Sunday, so we're
- 23:00just going to put Sunday here.
- 23:01Oh, why is Sunday here?
- 23:02This is weird.
- 23:03Or you say the function can take a string when really it can only take seven strings,
- 23:07seven particular values of string.
- 23:10Right.
- 23:10You could really clearly communicate that with enums, which is quite nice.
- 23:15Yeah.
- 23:15And the use of constants.
- 23:16That's really a win.
- 23:17I'm 100% on board with you on constants.
- 23:19All caps, maybe snake case, otherwise, you know, underscores, separate them.
- 23:25But I have tooling that I work with all the time that'll say, oh no, you misnamed this variable.
- 23:30Python, you know, they want to use lowercase variables, not uppercase
- 23:33variables.
- 23:33I'm like, but for constants, they say to use uppercase.
- 23:36Why don't you know this?
- 23:37It drives me crazy.
- 23:38So I was thinking about sort of expanding on this a little bit.
- 23:41Are you familiar with this, the typing.final when this was added in 3.8?
- 23:47Recently discovered that.
- 23:48Yes.
- 23:49Super nice.
- 23:50So it's also the idea is that it comes along and is meant to be used in this scenario in
- 23:56addition to what we've already discussed.
- 23:59So the example in the Python docs says there's a max size, capital max, capital size.
- 24:04Instead of just saying it's 9,000, you say it's colon final 9,000.
- 24:09Now that means nothing to Python.
- 24:12It means nothing.
- 24:14But it means something to type checkers, right?
- 24:16So if you have like mypy or something and somebody tries to change the value of it later, it's
- 24:22going to come at least as a linting type of error, if not a full on runtime error.
- 24:27So this is something people could leverage to go a little bit further.
- 24:30That's super nice type hinting.
- 24:31As you will paraphrase, it's meaningless to Python.
- 24:35But if you now run your checker and it's somewhere in your code that you try to overwrite that
- 24:40variable, which is perfectly fine, right?
- 24:42Because Python doesn't enforce, doesn't say anything if you now would assign a new value
- 24:46to max size.
- 24:47But there is no constant Python.
- 24:49Yeah.
- 24:50Mypy picks up on that final.
- 24:51Hey, it was final.
- 24:52Shouldn't overwrite that.
- 24:54So super powerful.
- 24:55I should use this more.
- 24:56Yeah.
- 24:57Yeah.
- 24:57And so you can say final or I would think preferably final of int.
- 25:01So you say what type it actually is in addition to that.
- 25:04Now related to this, related to this is it's not exactly a magic number, but I often find
- 25:10magic numbers appearing in this situation.
- 25:13And that has to do with, I want this thing to have maybe a default value or a value that
- 25:20says clearly communicates.
- 25:21I haven't set a value for it, but I want it to be.
- 25:24Like in this case, a number, it still needs to be an int.
- 25:26Well, what integer are you going to put there?
- 25:28Hmm.
- 25:28Zero.
- 25:29Well, zero valid value.
- 25:31Do you want it to be falsy?
- 25:32Like there's all, could it be negative?
- 25:34Which negative values?
- 25:35And then you see weird tests.
- 25:37Like if it's less than zero, you're like, why is it testing that?
- 25:39Well, that means it's not set.
- 25:40Like, okay, that's weird.
- 25:42So another idea that I think comes in that's pretty interesting is the sentinel pattern where
- 25:48you come up with a particular number and sort of store that and say this number or value,
- 25:54something you say, if it is this, that means it's unset or it's just a weird case or it's,
- 26:00it's kind of like setting it to none.
- 26:02But if that's not going to be a case you can use, sometimes none is, is that thing.
- 26:06Right.
- 26:06But not always.
- 26:08Are you familiar with this?
- 26:09Do you do anything like this?
- 26:10I have not really used it.
- 26:11So I had to read a read up on it.
- 26:13I think I use none quite a bit, but I think the, I mean, that's at least better maybe than
- 26:19minus one is the find example at the top.
- 26:22Yeah.
- 26:22But I think this even wins by being more explicit even, right?
- 26:27By having an object then to test against.
- 26:29Yeah.
- 26:29So one example of a standard library is the config parser has a underscore unset as a global variable
- 26:36and that's its, its sentinel value.
- 26:38BZ2 has the underscore sentinel to just call out the pattern directly there and so on.
- 26:45My favorite one out of this actually is a variation is this, this null object pattern.
- 26:51So null, because it comes, you know, sort of earlier in the life cycle of languages where
- 26:56null, not none, but where a lot of times if you see if this value is none, then we can
- 27:01do a thing other than otherwise we can do actually work with it.
- 27:05Right.
- 27:05And you'll see that these, these tests all over.
- 27:08And so this pattern is come up with a sentinel value that is not none, but kind of behaves
- 27:14in a no op way.
- 27:16Right.
- 27:17So you could give it a value of a person who is a sentinel and it just, if you try to
- 27:21get his name, it's just going to return, I don't know, nothing or something like that.
- 27:24Right.
- 27:25So you can, if it makes sense in that situation, you could remove a lot of these, if thing is
- 27:29none, do something else, do something else just globally, which is really nice.
- 27:34So you don't have to do this non check here.
- 27:36So are you saving an if else?
- 27:38Yeah.
- 27:39You're saving, you're saving the cyclomatic complexity.
- 27:41You're saving like branching at the cost of maybe allocating more stuff or, or having it
- 27:47possibly have this value that doesn't really mean it has a value, you know?
- 27:50So you got to trade it off and figure out what it makes.
- 27:52That's interesting because I think I do the is none quite a bit.
- 27:55So yeah, I do too, because sometimes it's kind of hard to deal with that or you don't control
- 28:00it.
- 28:00A lot of times kind of going back to when I was talking before about the 95% case and I told
- 28:05you about this really gnarly bit of code.
- 28:07that's because I'm consuming two other APIs that I have no control over and they're junky.
- 28:12They're real junky.
- 28:13So, you know, I've kind of got to like fit it together and, and you don't get a pick whether
- 28:18they use the null object pattern or not.
- 28:20You just got to make it work.
- 28:21Right.
- 28:21Yeah.
- 28:21That's a good point.
- 28:22Yeah.
- 28:22Yeah.
- 28:22Indeed.
- 28:23All right.
- 28:24So those are all kind of under the magic number category, I would say, even though technically
- 28:28some of them are not, like I said, not numbers.
- 28:29This one, honestly, I'll tell you, you asked me about kind of my first experiences with Python
- 28:34long ago.
- 28:35And this one weirded me out a little bit because it's something I had spent a long time in C,
- 28:39C++, C#, trying to just remove and weed out.
- 28:43And that's global scope.
- 28:44Tell us about this and maybe why to watch out for it.
- 28:47If we leave something in global scope, for example, we have a global variable and we're
- 28:52pulling that into a function with a global keyword and we're making changes to it.
- 28:58Now this function has side effects, right?
- 29:00So you have the stuff that's going in the function, but it's also mutating some external object,
- 29:05right?
- 29:06And when you write it, you probably are very aware of it because you're doing it, but you're
- 29:11going back and all of a sudden this might bite you.
- 29:13It will surprise you.
- 29:15Side effects overall are bad, right?
- 29:17And the global scope has that potential because it's global scope.
- 29:20It's not local scope to some class or function.
- 29:24Yeah.
- 29:24Basically that, that, it can lead to surprises and we don't want surprises in code.
- 29:29Yeah.
- 29:30And it's not an argument you pass to a function.
- 29:32It's not a return value from a function.
- 29:34You might not even know that that is in play as you interact with part of your code, but
- 29:38somewhere out there, it's something deep down is reaching up and seeing that value.
- 29:44This portion of talk Python to me is brought to you by brilliant.org.
- 29:47You're a curious person who loves to learn about technology.
- 29:50I know because you're listening to my show.
- 29:52That's why you would also be interested in this episode's sponsor, brilliant.org.
- 29:57Brilliant.org is entertaining, engaging, and effective.
- 30:00If you're like me and feel that binging yet another sitcom series is kind of missing out
- 30:05on life, then how about spending 30 minutes a day getting better at programming or deepening
- 30:09your knowledge and foundations of topics you've always wanted to learn better like chemistry
- 30:14or biology over on brilliant.
- 30:16Brilliant has thousands of lessons from foundational and advanced math to data science, algorithms,
- 30:22neural networks, and more with new lessons added monthly.
- 30:25When you sign up for a free trial, they ask a couple of questions about what you're interested
- 30:29in as well as your background knowledge.
- 30:31Then you're presented with a cool learning path to get you started right where you should be.
- 30:35Personally, I'm going back to some science foundations.
- 30:38I love chemistry and physics, but haven't touched them for 20 years.
- 30:41So I'm looking forward to playing with PV equals NRT, you know, the ideal gas law, and all
- 30:48the other foundations of our world.
- 30:50With Brilliant, you'll get hands-on on a whole universe of concepts in math, science, computer
- 30:55science, and solve fun problems while growing your critical thinking skills.
- 30:59Of course, you could just visit brilliant.org directly.
- 31:02Its URL is right there in the name, isn't it?
- 31:04But please use our link because you'll get something extra, 20% off an annual premium subscription.
- 31:09So sign up today at talkpython.fm/brilliant and start a seven-day free trial.
- 31:15That's talkpython.fm/brilliant.
- 31:17The link is in your podcast player show notes.
- 31:19Thank you to brilliant.org for supporting the show.
- 31:21You know, one of the areas where this comes back, where I think maybe things might have
- 31:28been different if the people were able to see the future 30 years in advance, which I'm not
- 31:32expecting they should be able to, would be the global interpreter lock itself with its global
- 31:37description right there.
- 31:39It's first G, you know?
- 31:41You know, Eric Snow did a lot of work to try to create a per sub-interpreter lock.
- 31:47So it's still a global lock, but global for that sub-interpreter and try to share those
- 31:51within processes.
- 31:52And I recall I talked about just reworking hundreds or thousands of global variables so that they
- 31:58were no longer globally shared, but were more local.
- 32:01And it's this kind of stuff that can grow and really makes it hard to go forward and make
- 32:06changes.
- 32:07Yeah.
- 32:07Interesting.
- 32:08I think you have had an episode on the GIL and then the refactoring, right?
- 32:12Yes.
- 32:13A couple.
- 32:14I have not had Sam Gross on for his, his like true GIL-less Python work, but he's welcome
- 32:19anytime he wants to come.
- 32:21Of course.
- 32:21Cool.
- 32:21All right.
- 32:22Let me see.
- 32:22I want to make sure I don't skip my, anything on my list.
- 32:25So let me just throw this out here for you.
- 32:27So global scope says, look, you should avoid having global variables.
- 32:30Sometimes you'll need global variables, but they shouldn't be the default.
- 32:34Is there a way you can pass this down to the thing that needs it and not just make it global
- 32:38and shared and right.
- 32:39So minimize what is global, I guess, is one of the things.
- 32:43When I first saw that, well, Python is full of just a bunch of modules and functions and
- 32:49the shared state is going to end up in these global variables.
- 32:51And that seems really, oh my gosh.
- 32:52But if you look at languages like Java, C#, C++, you end up with a lot of static
- 32:58classes.
- 32:59And just because there's a class namespace between your global variable and not, they're
- 33:05the same thing.
- 33:06Right.
- 33:06And so modules and module level variables are very, there's something that you don't want
- 33:11to have too many of, but they're not as out of whack with the rest of programming as
- 33:17I think maybe people might initially see them are.
- 33:19Right.
- 33:20There's no real difference between a static class and a function and a global variable
- 33:24and a module.
- 33:25Because there's scope to that module.
- 33:26Right.
- 33:27So unless you bring in that module, you don't have access to it.
- 33:31You mean?
- 33:31Yeah, exactly.
- 33:32And if, if you have a class and it has one value and if you have a module as one value,
- 33:37it's really like, it's really semantics on whether they're different.
- 33:40So I don't feel like putting them in, you know, some languages I'm maybe thinking like, oh,
- 33:44well, we don't have that because we have classes and we put stuff in class.
- 33:46Like if they're static classes, so the same thing.
- 33:49Yeah.
- 33:49Sometimes what confuses people though is, is having a static method or just a plain function
- 33:55outside of the class.
- 33:56And I've been a long time proponent of just a function, but I did see a Raymond Hattinger
- 34:02talk where he showed that if you do now, if you have the static method, so you bring basically
- 34:08the function in the class without it handling the instance, it does show it as part of your
- 34:15API.
- 34:15So if you now do a DURL on an instance of that class, that static method shows up, which
- 34:19I found kind of interesting.
- 34:21That is interesting.
- 34:22Yeah.
- 34:22I think there's a lot of places where people think I need a static class or, or something
- 34:26where it could just be a module with functions and maybe a global variable or two.
- 34:30But if you do have classes and I guess, and you're trying to look what's part of this class,
- 34:34then making that, go ahead and bringing that into the class.
- 34:37That totally makes sense to me.
- 34:37I can see that.
- 34:38I didn't see that presentation, but that makes sense.
- 34:40Yeah.
- 34:40As a form of grouping, but I'm with you with modules and functions.
- 34:44It can get very far.
- 34:45I would say more people error on trying to build up stuff into a bunch of classes because
- 34:50they've seen that in other languages they're coming from, then they don't quite group it
- 34:55right.
- 34:56You know, like it's, it's more of not idiomatic code, which I guess we're going to get to as
- 35:00well as some of the other stuff.
- 35:01So one of the things that we kicked off this conversation with is, well, you've, you said
- 35:07we were spoiled.
- 35:08I take offense to that.
- 35:09I think it's absolutely fair.
- 35:11We are so spoiled with the tools we have these days.
- 35:14And I remember pre-internet, it was hard programming.
- 35:16So pre-web anyway.
- 35:18So linters, tools that automatically fix things.
- 35:22That's what's next.
- 35:23Tell us about this.
- 35:24Yeah.
- 35:24This is really a no brainer in the times we live.
- 35:27And actually, yeah, we're spoiled because there was a time I was just manually fixing
- 35:31stuff like 8 was giving back till the Blackout of 4 Murder came along.
- 35:35Bob, it was worse.
- 35:36There was a time where we would argue about how it should be fixed.
- 35:39And we, this would be like a conversation.
- 35:41And we'd be like, no, no, no.
- 35:42We put the commas here and we space it like this.
- 35:45It's like, is this really how we should spend our day?
- 35:48And it was time for somebody to step in and give them defaults.
- 35:51Because I asked the questions.
- 35:53And enter Lukas.
- 35:54Yeah.
- 35:54I asked the question on Twitter the other day, like single or double quotes.
- 35:57And there were like 50 commands.
- 35:59And there's really, yeah, people are really torn about the default style should be.
- 36:03But yeah, Flake 8 to be compliant with Pep 8, which every Python developer should be.
- 36:08Black for auto-formatting.
- 36:10mypy to the type checks, right?
- 36:12As we said before, Python doesn't enforce it.
- 36:15So you need a tool for that.
- 36:16mypy, hence.
- 36:17Yeah.
- 36:18And then there's also, Flake 8, for example, has a lot of plugins we might go into next.
- 36:23But first I want to highlight, like, you want to automate as much as possible.
- 36:27And a pre-commit tool is just awesome.
- 36:29It's called pre-commit, right?
- 36:31So it's a tool that runs before you try to make a commit.
- 36:34So it's a great enforcer locally to not commit any code that's not formatted or that has style
- 36:40violations in it.
- 36:41Very easy to use.
- 36:43Very easy to set up.
- 36:44It might take a little bit of work fixing.
- 36:46I mean, black auto-formatting is automatic, but maybe you need to fix some Flake errors.
- 36:51But I see that as a little bit of work or a little price to pay with every commit to
- 36:56avoid a lot of technical debt over time.
- 36:59One of the challenges I see for a lot of these things, and linters and testing, they're both
- 37:03in this category, is different people on the team who are all working on the same code base
- 37:08have very different levels of commitment to, say, writing or running unit tests or formatting
- 37:16your code so that it all looks nice and clean before they check it in, right?
- 37:20I mean, I've had experiences where like, oh, the build is broken again.
- 37:23It's like, well, why is the build broken again?
- 37:25Well, someone checked it in.
- 37:26Why didn't they notice?
- 37:27Well, because they don't run any of the tools.
- 37:29Why do they run the tools?
- 37:30Because they don't really want to run the tools.
- 37:31Like, okay.
- 37:32But we're all in this together.
- 37:35Let's see what...
- 37:36And tools like the pre-commit stuff just mean it's just automatic, right?
- 37:40There's not a person to be blamed for saying, well, why didn't you run it?
- 37:43Or that person makes me run this tool?
- 37:45Or, you know, like there's none of that weird friction.
- 37:48It's just like the software said, this is what we agreed on and it's not ready.
- 37:52You've got to go, you know, format this line or it'll often just do that itself, right?
- 37:56Yep.
- 37:57What's the saying?
- 37:58If it's not automated, it is broken.
- 38:02Yeah, that's a good saying.
- 38:02This is as simple as putting the YAML in place.
- 38:05I mean, that's a one-time thing.
- 38:07Then that's committed to a version control.
- 38:09So your collaborator teammate pulls that in.
- 38:11You do a pre-commit install.
- 38:13It installs the hook locally in your .git folder and you get to go.
- 38:17Now all this stuff is enforced.
- 38:19You can also do that with GitHub actions.
- 38:21But I think the more you do locally at the individual developer level, the better.
- 38:25You do both.
- 38:26Oh, yeah.
- 38:26So ideally, none of the malformed code gets checked in.
- 38:31And then fixed.
- 38:32And then it shows up as a git diff and, you know, things like that.
- 38:35So you could have it on the server just as a safety net.
- 38:39Who knows how that got checked in?
- 38:40But also have it as a pre-commit hook for most cases.
- 38:43Yeah, exactly.
- 38:43All right.
- 38:44Let's see.
- 38:45Next up.
- 38:46Narrow exception blocks.
- 38:48Yeah, this is something I see quite a lot when I'm reviewing code.
- 38:53It's these long blocks between the try and accept.
- 38:56So 20, 30 lines.
- 38:57And my code review command is always the same.
- 39:01Are all these lines of code susceptible to this exception?
- 39:04And of course, often they're not.
- 39:06Right?
- 39:06So narrow your exception blocks, meaning put only code in the try accept that can actually raise that exception.
- 39:14And yeah.
- 39:14I think it's good advice.
- 39:15I think it doesn't have to be just one try accept block either for one huge function.
- 39:19You know, you could have try accept do some stuff.
- 39:22Try something else if you really need to.
- 39:24You know, you can't disambiguate them by exception type.
- 39:27But also worth pointing out, I suppose, is the accept star stuff, which is not going to be maybe Python.
- 39:35Oh, that's new.
- 39:35Yeah.
- 39:36Yeah, that's new.
- 39:37And I don't know how to search for it because it seems like the star.
- 39:41Well, whatever.
- 39:42I could just describe.
- 39:42But it's really interesting because you can catch multiple exceptions.
- 39:45And maybe this actually changes the advice a tiny bit.
- 39:48You can catch multiple exceptions for something going wrong.
- 39:51So if you were doing it primarily around async, but not just.
- 39:55You could say, I'm going to try to talk to the database and this API.
- 39:59And if they both break, you know, the accept star will let you catch both.
- 40:03Add two separate executions.
- 40:05The database exception and the API exception potentially.
- 40:08Right.
- 40:08Right.
- 40:09Interesting.
- 40:09Yeah.
- 40:10I think that's new.
- 40:11Brand new in 3.11.
- 40:12Yeah.
- 40:13That's a 3.11 thing.
- 40:14So it'll be a while till we see that really coming along.
- 40:16But, you know, one of the places that might be relevant is there's cool libraries like Tenacity,
- 40:20which say, I want to call this function.
- 40:23And if it fails, just wait and call it again.
- 40:25And then try again and, you know, maybe back off until too much time has passed or give
- 40:29it a certain number of attempts.
- 40:30But if it failed, but it failed differently three times, you might want to know, well, what
- 40:35are all the ways in which it failed?
- 40:36Not just the last or the first.
- 40:38Right.
- 40:38And with the accept star, you could actually catch all of those errors, say, from like a
- 40:42retry block or something.
- 40:43So.
- 40:44Yeah.
- 40:44Interesting.
- 40:45I don't know.
- 40:45It doesn't make this cleaner or simpler to go with.
- 40:49It only complicates it.
- 40:50But still the idea of like smaller code blocks, I think good advice there.
- 40:54Yeah.
- 40:54And always name your exceptions.
- 40:55Right.
- 40:55So sometimes I see try except colon.
- 40:58And then we're like, what's exception?
- 41:00Because that will just catch anything.
- 41:03Right.
- 41:03So always be explicit in the exception.
- 41:05I just think you understand how this works.
- 41:07When you put that in there, there's no more errors in the code.
- 41:10It just keeps running.
- 41:11It used to crash.
- 41:12And if I put except colon and just keep going, way more reliable.
- 41:17Yeah.
- 41:17What's the saying in the center of Python?
- 41:21Errors should not pass silently.
- 41:22Yes.
- 41:23Exactly.
- 41:24I remember.
- 41:24Yeah.
- 41:25Yeah.
- 41:25I've seen some of this code.
- 41:27And it's not always bad.
- 41:28But it's usually.
- 41:29It's usually not good.
- 41:31So.
- 41:31Yeah.
- 41:32Yeah.
- 41:32Catch the exceptions by specific type.
- 41:34Handle different ones potentially differently.
- 41:36And so on.
- 41:37Yeah.
- 41:37And maybe that kind of leads us into number six here more broadly.
- 41:41That's one of the Pythonic or idiomatic things of Python.
- 41:44Right.
- 41:44But there's a whole bunch more.
- 41:45And that's your next tip.
- 41:46Yeah.
- 41:47This can, of course, be a whole series of articles because there's so much what can be considered idiomatic or not.
- 41:53But yeah, there's sometimes you see people reinventing the wheel and they can perfectly well use the standard library.
- 42:00There's also a very rich set of built-ins.
- 42:03Right.
- 42:04Like all any those built-ins.
- 42:06But then also style-wise, there's this concept of leaping, checking before you leap versus just do something and ask for forgiveness rather than permission.
- 42:16So, for example, code that overly checks if a file exists or can be opened.
- 42:21So all these conditionals.
- 42:22What's often considered more Pythonic is to just try open a file, try to do something and then catch the exception.
- 42:29Right.
- 42:30So that's why it's called.
- 42:31Right.
- 42:31It's easier to ask for forgiveness than permission.
- 42:34Yeah.
- 42:35Yes, exactly.
- 42:36As opposed to like a C language or something where you do six or seven checks to make sure everything is set up just right.
- 42:44And then, you know, I'm going to check that the file is not just that it's not null and that it has a null terminating character.
- 42:52And now I'm going to check that I have access to the files.
- 42:56You know, you're like there's if you write, if you go and read a lot of C code, there's like check, check, check, check, check, check, check.
- 43:00Do the thing.
- 43:01And usually that's because there's a page fault and the program just goes, whoof, and just goes away if you touch it wrong.
- 43:07Whereas Python almost always, not always, but almost always the result is here's an exception you can catch and handle.
- 43:13And it tells you what went wrong instead of trying to think of all the checks.
- 43:17Because there's probably a check you forgot.
- 43:18Just give it a shot.
- 43:20Right?
- 43:20Right.
- 43:20And it's also, it's kind of the positive mindset.
- 43:23Like usually it works.
- 43:25And if it doesn't work, we deal with the consequences.
- 43:27Whereas if you do all these if checks, that's code that always runs and might not be necessary.
- 43:31So it might even be faster.
- 43:33Right?
- 43:33For sure.
- 43:34So more of the ideas that came to mind when I was reading your idiomatic code section was just one use that you did mention this a little bit, but I'll reinforce it is you said use a standard library.
- 43:43And one of the things I learned from working with you is there, you know, sort of, I would try to pull another, other library, other things from IPI or something.
- 43:51You're like, look, this is built in.
- 43:52Why don't you just use, you know what?
- 43:53You're right.
- 43:54That's built in.
- 43:55That seems pretty handy.
- 43:56Right?
- 43:56Like, for example, counter is definitely one that I use all the time because of you.
- 43:59And a collection.
- 44:00Yeah, exactly.
- 44:02So embracing what's there instead of maybe getting another library or just trying to write that algorithm yourself because who knew it existed?
- 44:10Didn't study the standard library enough.
- 44:12I just came from C and this is how we do it in C.
- 44:14So I'm going to try to do that here.
- 44:15Right?
- 44:16Like the idiomatic steps that you can take are really good there.
- 44:19But also lambdas versus regular functions for like little inline things.
- 44:24You mentioned some of the ones that take generators for like all and any and these other tests.
- 44:29And there's just a lot of cool little language features.
- 44:31Yeah.
- 44:32Decorators.
- 44:32I've got to use later.
- 44:33Yeah.
- 44:34Yeah.
- 44:34Decorators.
- 44:34Definitely.
- 44:35I would put it in that space as well.
- 44:37And the wit statement.
- 44:38Context managers to automatically clean up resources.
- 44:41Pretty important feature.
- 44:42Mm-hmm.
- 44:43Mm-hmm.
- 44:44Another one that is near and dear to my heart.
- 44:46You're number seven on this list.
- 44:48Near to dear because I have suffered badly the consequences of choosing wrongly is choose the right data structures.
- 44:56Tell us about that.
- 44:56Yeah.
- 44:57This is very important for performance.
- 44:59Right?
- 44:59So where you kind of need to understand just the fundamentals here.
- 45:02Right?
- 45:03Like list versus sets and dictionaries and how they perform.
- 45:06For example, if you're very big collection of a million items and you would leave that as a list, yet you want to do searches or lookups.
- 45:13Then how this works is they traverse them item by item.
- 45:16Right?
- 45:16Where if you would make that a set, it's now in big O notation, O1, because there's hashing going on.
- 45:22So it can immediately find the value because they're hashed.
- 45:26Right?
- 45:26So that's a small, well, a pretty fundamental example because we work with lists and sets and dictionaries all the time.
- 45:34But those are very important to know.
- 45:36They are.
- 45:37The dictionary and the set one can't emphasize that enough.
- 45:40I mean, obviously, we use dictionaries so much.
- 45:43But if you've got a list, but you think you might need to look up stuff in that list based on, say, if it's a list of objects, some field of that object.
- 45:51If you would make that a dictionary, make the key of the field, then it's not a little bit faster.
- 45:56It's unbelievably faster.
- 45:58My mind just never ceases to be amazed.
- 46:01Like, you know what?
- 46:02It really, it went through a million items that fast, like instantly.
- 46:05Right?
- 46:06It's so different.
- 46:07Or if you see people writing code, here's some more of the idiomatic stuff is I'm looping over a thing.
- 46:12I want to build up this list of them.
- 46:14But I want duplicates.
- 46:15You might see if this thing is not, you know, look in the list for it, then you can add it.
- 46:20Like, just don't do that.
- 46:21Just make a set and just jam it in there.
- 46:22And it'll reject the duplicates.
- 46:24Right?
- 46:25Like, there's a lot of things where this ties back to your idiomatic one as well.
- 46:28Yeah.
- 46:28Another one I highlight there are decks.
- 46:30Right?
- 46:31So when you do inserts and leads on both ends of a sequence, that's also generally slow with a list for the same reason.
- 46:39Because as the search example is that it needs to remap values, right?
- 46:43Massively.
- 46:44And a deck is designed to do that fast on both ends.
- 46:48So anytime I need to, if it's a big collection and I need to insert stuff on at the start, then a collection, a deck from the collections module is a better choice.
- 46:57Yeah, absolutely.
- 46:58Another thing, I guess, giving a little quick shout out here is the default dict is really nice, too.
- 47:02Oh, yeah.
- 47:03For removing tests and checks and initialize.
- 47:07Because you don't have to check if a key is in the dictionary.
- 47:09You can just assume it's there.
- 47:11And, yeah, that leads.
- 47:13You can ditch a whole bunch of if statements.
- 47:15Yep, exactly.
- 47:17It's the job of the dictionary, not your job now.
- 47:19Yeah, nice.
- 47:20All right.
- 47:20Let me see.
- 47:21Do I have any others that I want to squeeze in here?
- 47:23Okay, so here's one that I wanted to sneak in before we get further beyond this is to use guarding clauses.
- 47:32And so often what I'll see, especially when people are new, is writing code that looks like this.
- 47:37It's like, it looks to me like a saw, a lumberjack would use or something.
- 47:42It's just like zip, zip, zip, out, and then back, and then out, and then back.
- 47:45And you're like, whoa.
- 47:46You know, it's usually like it starts in the middle of the editor window and it goes to the right.
- 47:52You're like, what is this?
- 47:53And, you know, it's usually if some case and then if some other case, maybe do a little step, then if another case.
- 48:00And it's like assuming that it's got to build up a bunch of these steps before it can sort of go down the happy path.
- 48:07So it's going to check everything that could possibly go wrong before it tries to do that.
- 48:11And I don't know how you feel about guarding clauses.
- 48:13I love guarding clauses.
- 48:14Yeah, you do?
- 48:15So tell people about this sort of alternative.
- 48:17It's so simple, but it makes such a difference.
- 48:20Yeah, it goes back to the Xenophython again.
- 48:22Flat is better than nested.
- 48:24So nested, deeply nested code is just inherently, it's more complex.
- 48:29Painful.
- 48:30And it's harder to maintain.
- 48:32Yeah, it's called the arrow, arrow shape.
- 48:35I learned from this article that it's also called the staircase, which is nice.
- 48:39But yeah, you can, if you look up our arrow shape, it explains more about it.
- 48:42But yeah, it's just more complex because, yeah, it's again, that cyclomatic complexity, right?
- 48:48The amount of paths you have through your code.
- 48:50And if you, in this case, reverse all these statements and early return, the code is now way flatter.
- 48:56It's easier to read and less complex, right?
- 48:59So this code looks really reasonable.
- 49:02And yeah, the other is just very hard to follow.
- 49:05You don't even know.
- 49:07And there's all these weird interplays of like, well, what if this is true, but that's false?
- 49:11And could you actually get into that if case?
- 49:13Whereas these guarding clauses just say, if this is wrong, bail out of this function.
- 49:18If this other case is wrong, bail out.
- 49:20Just like return false or don't do the thing or whatever.
- 49:22And then eventually, once you've hit all those with very little indentation, then you have the thing you wanted to get to.
- 49:28And it also kind of makes them a little bit unrelated to each other.
- 49:32Yeah.
- 49:32Unlike the super nested style.
- 49:34Yeah, I guess, yeah, the cyclomatic complexity, right?
- 49:36The amount of combinations you can have with this is less.
- 49:40Hence, it's less complex.
- 49:41Hence, it's easier to maintain.
- 49:43And readability here is way better.
- 49:46Yeah, I totally agree.
- 49:47So when you see things like flat is better than nested in the Xen of Python, you're like, cool, but I got to test some stuff.
- 49:53So it's getting nested, right?
- 49:55Like it's, I just, there's nothing in that statement that it tells you, well, what are the mechanics that you consider to avoid that, right?
- 50:03It's just, well, it's complicated.
- 50:04I'm sorry.
- 50:05It's going to be nested, right?
- 50:06It would be better if it weren't, but it is.
- 50:07Well, not actually.
- 50:08Like guarding clauses almost always will lessen it, if not really, really decrease it.
- 50:13Yeah, that's a good point.
- 50:14Like the Xen tells you the what, but not necessarily the how.
- 50:18And the guard clause is that this is a particular technique.
- 50:21Another one in refactoring is the extract method refactoring.
- 50:24And that's where we pull out a bunch of code into a new function, which also often leads to less nested code.
- 50:30Indeed.
- 50:30Is that your next one?
- 50:32Refactoring?
- 50:32Could be.
- 50:33I think that was a bonus.
- 50:34I skipped over a bunch.
- 50:35That's right.
- 50:36It was a bonus here at the end.
- 50:38Absolutely.
- 50:39So that's where you gave a shout out to the refactoring book, which also had those code smells in them.
- 50:44Totally.
- 50:44Yeah.
- 50:45Do you still recommend people read that?
- 50:46I mean, I read it with bated breath in 1999 or whenever that thing came out.
- 50:51It was really an important book.
- 50:53And I'm like, this has changed everything.
- 50:54But, you know, that's 2025 years ago, something like that when it actually came out.
- 50:59Yeah, that's a good point.
- 50:59I think the tooling has improved.
- 51:02But still, I think conceptually, this is still an important book.
- 51:04I was reading parts of it again.
- 51:06Honestly, I do like the first edition a bit better sometimes because it uses Java, where this uses JavaScript.
- 51:13And I don't know, the paper quality.
- 51:16I'm not a fan of Java, but I think I might actually prefer the Java or the JavaScript, to be honest.
- 51:22But it's very fundamental stuff.
- 51:23And, yeah, it still has, it's also nice that it's a catalog, so you don't have to read it end to end.
- 51:30You can just zoom into parts that are relevant for you at that moment.
- 51:34So, the collection of things.
- 51:36Okay.
- 51:36Very interesting.
- 51:37So, I guess another one, I don't remember if it was directly in your article.
- 51:42I don't think so.
- 51:43It was just sort of implied throughout, was, you know, embrace refactoring.
- 51:48You opened the, alluding to this at the beginning of this conversation, is I see a lot of people who get stuck thinking about, I've got to get this right.
- 51:56I know if I start wrong, I'm going to hate it.
- 51:59It's going to be hard to work on.
- 52:00It's going to be so bad.
- 52:01So, I've got to sit here and think for, like, two weeks to get started on this project so I get it right.
- 52:06You know what you could also do?
- 52:08Just pick one way that seems right at the time.
- 52:10Spend a week on it.
- 52:12If it's not working, it'll refactor to the other.
- 52:14Or even throw it away and go to the other option, right?
- 52:17Like, there's a lot of people kind of stuck.
- 52:19And I think embracing the idea of refactoring, maybe not in the traditional by-hand effect of Martin Fowler's original book, but, you know, we have tools that do a lot of this automatically or almost automatically, you know, right-click and ask it to.
- 52:31So, what are your thoughts?
- 52:33I definitely think people should at least catch the zen of it, if not go through the book, because the tools will take care of it.
- 52:39Yeah, you still want to know the concepts here.
- 52:42The different techniques, as I already highlighted, the extract method, for example, grouping constants into enums we've mentioned before.
- 52:49So, a lot of actually what we've spoken about so far can be found in this book as well, right?
- 52:54And I think overall, the goal of refactoring is to keep that technical depth in check, right?
- 53:01So, avoid, suffer a rut, they also say.
- 53:04And, yeah, as you start to add more code and cover more scenarios and the always changing requirements, inevitably, what happens?
- 53:14You kind of need to learn.
- 53:16You thought it was one thing, but as you got further into it, you're like, you know, that part's actually, that's where the problem is.
- 53:21I didn't think so, but now I know, because that's where it's hard, right?
- 53:24Which you only learn as you go, right?
- 53:26Yeah, that's a good point.
- 53:27Right, but the people who are stuck, I don't think they internalize that, like, the only way to know where you're going to get stuck further down the road is to go down the road.
- 53:34You can't think about it very well.
- 53:36There are a couple of interesting comments here with respect to garden clauses.
- 53:41Alvaro says, you could go and put assert statements at the beginning of functions to indicate some of the assumptions that could be disabled in production and give nice errors on pytest.
- 53:51So, pytest, rather.
- 53:52Yeah, possible.
- 53:54What do you think, Bob?
- 53:55Yeah, asserts are great, but, yeah, I'm happy that you highlight the caveat that they can be disabled, so you cannot always rely on them.
- 54:01I definitely see a place for asserts.
- 54:04And, of course, you use them in pytest all the time to assert different ways your function performs.
- 54:10I use most of my asserts in pytest, yeah.
- 54:13But I can definitely see a place for this scenario that Alvaro highlights.
- 54:18Yeah, and then Brandon Outlawdience has a very realistic problem that people run into is, I work a lot of places where they don't give you the time to do the refactoring and make things better.
- 54:28And so it just builds up until it becomes not ideal, as his second comment here implies.
- 54:34That's a good point.
- 54:34That is a real problem.
- 54:35And I'll tell you how I solved it.
- 54:38Bob, maybe we're getting short on time, but you can tell me real quick how you solved it.
- 54:42For me, when my managers and folks would say, Michael, what is your estimate for how long this is going to take?
- 54:47If I thought it was going to take five hours, but maybe I would need to do an hour or two of refactoring, this feature took seven hours.
- 54:53Like, you want it done, it's going to take seven hours.
- 54:56Because if I'm going to do it, I'm going to do it right, because I'm going to have to work on it afterwards.
- 54:59And I know it's just going to get worse if I...
- 55:02So I wouldn't exactly lie to them, but I would include the time to make it right, not just make it work as part of my estimate.
- 55:10Yeah, maybe even double it, right?
- 55:12Because there's the initial version, and there's the testing, there's the documentation, there are bug fixes, there are inevitably are requirement changes in the sprint meetings.
- 55:23So it might, five hours might just be that initial draft almost, but maybe it should then be 10 hours.
- 55:30Yeah, perhaps.
- 55:31But whatever it is, clean code should be part of the deliverable, in my opinion.
- 55:36And it should be part of the estimate.
- 55:38I was saying estimate times three plus two weeks.
- 55:41That's a nice one.
- 55:42Exactly.
- 55:43That is a good one.
- 55:45Very good.
- 55:45All right.
- 55:46Well, we're getting short on time here.
- 55:48Let me do something.
- 55:49I want to do something that's kind of different, Bob, because I think this is actually...
- 55:53Let me share a different thing here.
- 55:55I think that this is something people are going to have to start thinking about as well as maybe an option here.
- 56:00So what about asking AI assistants?
- 56:03Things like Google Copilot or ChatGP.
- 56:06There might be some cases where people say, here's my code.
- 56:09Hey, thing, could you make it better?
- 56:11So for example, let's do this to close out the show.
- 56:13Okay, Bob?
- 56:13Sure.
- 56:14How can we write plain code in Python?
- 56:17I'm going to ask ChatGP.
- 56:19We have already covered this.
- 56:23Are you sharing?
- 56:23What else?
- 56:24All right.
- 56:24Oh, hold on.
- 56:25I thought so.
- 56:26I did.
- 56:26I forgot to add a stream.
- 56:27So I'm going to ask ChatGP here.
- 56:29And what else does it say?
- 56:30So I gave it all of your tips.
- 56:32And I said, we already discussed that.
- 56:33What else can we do?
- 56:34Is it based in the article as well?
- 56:35No, I didn't give it the article.
- 56:37Just the headings.
- 56:38So it says you can document your code.
- 56:40Keep it simple, which it didn't understand.
- 56:42Follow PEP 8.
- 56:43This is an interesting one.
- 56:45Exceptions are for error handling.
- 56:46So anyway, I just thought it might be kind of fun to think about, you know,
- 56:50could we ask these things to help us and improve our code?
- 56:53I don't know if you played with any of this, but it could be fun.
- 56:56I have not yet.
- 56:56That's Julian's department.
- 56:58He's geeking out over the tool.
- 57:00But I'm high on my list to try it out because it's really mind-blowing.
- 57:06What this tool is doing.
- 57:08Yeah.
- 57:08I had a program that I wrote.
- 57:11And let's just see if I can go to, let's grab some random gist here on GitHub.
- 57:17Let's see.
- 57:18So you can take these things, this random code, and say, you know, things like,
- 57:23here's a Python program.
- 57:25How can I make it more readable?
- 57:28You can give it this long thing and it'll say, you know, look, there were no doc strings.
- 57:33There's some places where you had meaningful variables are missing.
- 57:36Yeah.
- 57:36Could have grouped the imports better.
- 57:38Could add more type hints.
- 57:39And then it rewrites it.
- 57:41So, for example, this is my code to integrate with turnstile.
- 57:45And so it's, you know, you see it putting type hints and stuff as it goes.
- 57:49Typing there.
- 57:49Yeah.
- 57:49Yeah.
- 57:50Yeah.
- 57:50Yeah.
- 57:51So anyway, I don't know.
- 57:52I feel like this is, as much as looking back 25 years and seeing refactoring go that seminal,
- 57:58you know, maybe, maybe, maybe this kind of stuff is going to have to be something that we take into consideration going forward.
- 58:05So.
- 58:05Yeah.
- 58:06Wow.
- 58:06Pretty impressive.
- 58:07Yeah.
- 58:08Yeah.
- 58:08We will see.
- 58:08We'll see.
- 58:09So anyway, that was fun, but let's, let's close it out.
- 58:12Yeah.
- 58:12Excellent work.
- 58:13I mean, I feel like we only just touched the surface, right?
- 58:16This could go on and on.
- 58:17JF out in the audience asked about unit testing and we barely touched on that.
- 58:22There's a bunch of, bunch more areas, but I think we're out of time.
- 58:25Yeah.
- 58:25The sun is coming up in your world.
- 58:27I can see.
- 58:27Thousand pager books have been written about this.
- 58:31It's, it's a whole field.
- 58:32So yeah.
- 58:33Yeah.
- 58:33We, we mentioned a couple of important things, I think.
- 58:36Yeah.
- 58:36We sure did.
- 58:37Well, it's certainly a lot of things that people can take and they do sound simple.
- 58:41Like, well, write smaller programs and give them good names or write smaller parts of your program and give it good names.
- 58:46But that it's like a never ending journey to do that right.
- 58:49And to keep on it.
- 58:51Right.
- 58:51So there's a lot of nuance as well.
- 58:53Right.
- 58:54There is.
- 58:54Absolutely.
- 58:55All right.
- 58:55Well, quick two final questions before you get out of here.
- 58:58If you're going to write some clean Python code editor, what do you use these days?
- 59:03Well, the coaches, the team almost had me on VS Code, which is an amazing idea.
- 59:09But it was not as super fast, of course, but for me, for my workflow, it was not as fast as Vim.
- 59:16So I stuck with Vim and I do everything.
- 59:19It's just a setup, right?
- 59:20With all the plugins and shortcuts.
- 59:23And it makes me fast.
- 59:25And the muscle memory.
- 59:26Yeah.
- 59:27Yeah.
- 59:27It's one of those things where, you know, you could change to something potentially, but how long are you going to be in a degraded state of working before you gain enough experience?
- 59:36And will it actually be that much better?
- 59:38You know, there's a lot to it, right?
- 59:39And once a tool introduces a mouse and I have the other option of only using a keyboard, then it's still the keyboard.
- 59:45Yeah.
- 59:46Yeah.
- 59:46Yeah, for sure.
- 59:47Okay.
- 59:47And the notable PyPI package you've come across lately?
- 59:50Yeah.
- 59:50Yeah.
- 59:50This is such a hard question.
- 59:52I think I've mentioned requests in the past.
- 59:55These days there's HTTPX because we're using a lot of code to consume APIs.
- 1:00:01And now you can do that synchronously.
- 1:00:03But now maybe the shout is really for Sebastian Ramirez.
- 1:00:06SQL model, FastAPI, typer.
- 1:00:09Apart from amazing tools, the documentation, it's so pleasant.
- 1:00:13I keep going back.
- 1:00:14And because I teach those tools as well, right?
- 1:00:16To other developers.
- 1:00:17I can just teach them from the documentation.
- 1:00:20It's really good with solid code samples.
- 1:00:23So FastAPI, I guess it is then.
- 1:00:25Right on.
- 1:00:27And all those things you named there, an important piece in the mix are Python types and Pydantic.
- 1:00:31Yeah, exactly.
- 1:00:32Exactly.
- 1:00:33That really took the type hints to the next level.
- 1:00:36It did.
- 1:00:37Yeah.
- 1:00:37Yeah.
- 1:00:38It definitely did.
- 1:00:38All right.
- 1:00:39Well, final call to action.
- 1:00:40People are interested in this.
- 1:00:42Maybe they're interested in getting to know some of your coaching stuff better as well.
- 1:00:46What do you tell them?
- 1:00:47Yeah.
- 1:00:47Go to pybit.es, our website.
- 1:00:49Check out our PDM program.
- 1:00:51And yeah, for anything, you can also join the PyBite Slack.
- 1:00:55Hit me up there with any questions or comments.
- 1:00:57I'm pybob, add pybob there.
- 1:00:59And I think that's it.
- 1:01:01Pybob?
- 1:01:02Yeah.
- 1:01:02That's a good name.
- 1:01:03I like it.
- 1:01:04It's on my handle.
- 1:01:05So yeah, perfect.
- 1:01:06So people can go check it out there.
- 1:01:09And you've got the cool aspects of the Spanish domain suffix.
- 1:01:14So pybit.es.
- 1:01:18You can actually have just the word.
- 1:01:19That's very cool.
- 1:01:20Yeah.
- 1:01:20Yeah.
- 1:01:21Excellent.
- 1:01:21All right.
- 1:01:22Well, thank you for being on the show.
- 1:01:23Thanks for writing the article.
- 1:01:24And good to catch up with you as always.
- 1:01:26Yeah.
- 1:01:26Thanks for having me.
- 1:01:26This was so much fun.
- 1:01:27I'm happy to share.
- 1:01:28You bet.
- 1:01:29Bye.
- 1:01:29Bye, everyone.
- 1:01:30Bye-bye.
- 1:01:30This has been another episode of Talk Python To Me.
- 1:01:34Thank you to our sponsors.
- 1:01:36Be sure to check out what they're offering.
- 1:01:38It really helps support the show.
- 1:01:39TypeI is here to take on the challenge of rapidly transforming a bare algorithm in Python into a full-fledged decision support system for end users.
- 1:01:48Get started with TypeI Core and GUI for free at talkpython.fm/typeI.
- 1:01:53T-A-I-P-Y.
- 1:01:54Stay on top of technology and raise your value to employers or just learn something fun in STEM at brilliant.org.
- 1:02:02Visit talkpython.fm/brilliant to get 20% off an annual premium subscription.
- 1:02:08Want to level up your Python?
- 1:02:10We have one of the largest catalogs of Python video courses over at Talk Python.
- 1:02:15Our content ranges from true beginners to deeply advanced topics like memory and async.
- 1:02:20And best of all, there's not a subscription in sight.
- 1:02:22Check it out for yourself at training.talkpython.fm.
- 1:02:25Be sure to subscribe to the show.
- 1:02:27Open your favorite podcast app and search for Python.
- 1:02:30We should be right at the top.
- 1:02:32You can also find the iTunes feed at /itunes, the Google Play feed at /play,
- 1:02:36and the direct RSS feed at /rss on talkpython.fm.
- 1:02:41We're live streaming most of our recordings these days.
- 1:02:44If you want to be part of the show and have your comments featured on the air,
- 1:02:47be sure to subscribe to our YouTube channel at talkpython.fm/youtube.
- 1:02:52This is your host, Michael Kennedy.
- 1:02:54Thanks so much for listening.
- 1:02:55I really appreciate it.
- 1:02:56Now get out there and write some Python code.
- 1:03:01I really appreciate it.
- 1:03:19I really appreciate it.
- 1:03:19I really appreciate it.
- 1:03:19I really appreciate it.