Latest / Talk Python To Me / #411: Things I Wish Someone Had Explained To Me Sooner About Python
Transcript
- 0:00What advice would you give someone just getting into Python?
- 0:02What did you learn over time through hard work and a few tears that would have really helped you?
- 0:07It's a fun game to play, and we have Jason McDonald on the podcast to give us his take.
- 0:12This is Talk Python To Me, episode 411, recorded March 2nd, 2023.
- 0:17Welcome to Talk Python To Me, a weekly podcast on Python.
- 0:34This is your host, Michael Kennedy.
- 0:36Follow me on Mastodon, where I'm @mkennedy, and follow the podcast using @talkpython, both on fosstodon.org.
- 0:43Be careful with impersonating accounts on other instances.
- 0:46There are many.
- 0:47Keep up with the show and listen to over seven years of past episodes at talkpython.fm.
- 0:52We've started streaming most of our episodes live on YouTube.
- 0:56Subscribe to our YouTube channel over at talkpython.fm/youtube to get notified about upcoming shows and be part of that episode.
- 1:03This episode is brought to you by Sentry and us over at Talk Python Training.
- 1:08Please check out what we're both offering during our segments.
- 1:11It really helps support the show.
- 1:13Jason, welcome to Talk Python To Me.
- 1:16Thank you for having me.
- 1:17Yeah, it's great to have you here on the show.
- 1:19I'm looking forward to talking about a bunch of things I wish people taught you about Python sooner.
- 1:25I think there are many, many things that fall into that category.
- 1:29And part of that, I guess it's true with all languages, but I think it's maybe a little extra true in Python because getting started is so easy.
- 1:37But getting really good at it takes a lifetime sort of thing, you know, and so there's like this big mismatch of how accessible it feels.
- 1:44But then what you can get out of it if you go further.
- 1:47Right.
- 1:47So I think that we'll touch on that going further part, maybe.
- 1:49Absolutely.
- 1:50Yeah, for sure.
- 1:51Now, before we get into all those things, let's start with your story.
- 1:55How did you get into programming in Python?
- 1:56Well, I got into programming by a route that I don't recommend to anybody.
- 2:00I fell down a staircase, hit my head on the banister and ended all hopes of ever becoming a doctor.
- 2:05Oh, so went from 4.0 straight A student, college level reading in high school to family pre-game material.
- 2:13Two extra years to climb back up where I was academically a bit longer after that for other things.
- 2:19But somewhere along the way, I discovered that I had a natural inclination towards an attack for coding and got into that because I wanted to make games.
- 2:28I wanted to make educational games.
- 2:29They had been part of my recovery.
- 2:30So I'm like, I want to make games.
- 2:31So I learned how to code.
- 2:33And so I taught myself BB.net and then felt very constrained by the .NET ecosystem and broke out into Python.
- 2:41Found my home, stayed put until I wound up going into a bunch of other languages.
- 2:46I used Flash for quite some time.
- 2:49I used C++.
- 2:50I'm not coming out in whatever, Sandy, but Python is definitely near to dear to my heart.
- 2:55Yeah, absolutely.
- 2:55Well, if you started out in BB.net, I think it's a little less true these days.
- 3:00But in the earlier days, that was a really different ecosystem than Python, right?
- 3:05There was like, well, what is the framework for this thing that Microsoft recommends?
- 3:09Like, how do I do the web?
- 3:10I do ASP.net because that's what they provide me.
- 3:12How do I do database?
- 3:13I do this because that's what they provide me.
- 3:15And you come into Python, you're like, wait, there's a thousand different ways to do this.
- 3:18And I have to pick and decide and I can mix and match.
- 3:21And it's both amazing and like.
- 3:23Terrifying.
- 3:23Yeah, it's terrifying.
- 3:24Like, what do I make the wrong choice?
- 3:26I've how do I decide?
- 3:27I have no basis, right?
- 3:29I'm both new.
- 3:29So I have no basis for deciding.
- 3:31But I also, I've got to decide to get started.
- 3:33So where do we go, right?
- 3:34Like, it's a different world.
- 3:35It is where I first started running into trouble with the paradox that I wound up addressing in my book on back of that.
- 3:43But the paradox that a lot of people find themselves in is like, I know all this stuff, but I don't know enough to do this thing.
- 3:50And so you wind up with this weird little gap in a lot of training material because you have like the absolute basic elementary.
- 3:56And the examples there are almost insultingly simplistic.
- 3:58And it's like, oh, you just have to type this stuff into this file and you run the file and look at that.
- 4:04You have a hello world.
- 4:05Aren't you smart?
- 4:06And then you go all the way to the extreme and it's something like super complicated.
- 4:09You don't understand any of it.
- 4:10And nobody sat and wrote down.
- 4:13Here's how you structure your multi-Python project in a way that's going to, at least when I started, that wasn't documented anywhere.
- 4:18Like no one had ever written it down.
- 4:20So it's just like, what do I do now?
- 4:22Yeah, it's really tricky.
- 4:23A lot of the tutorials and stuff are like, you just put all the stuff into one file and then it's magic.
- 4:29But you should never, ever do that.
- 4:30Well, maybe in simple cases, but in realistic apps, like no realistic app belongs in one file.
- 4:36Just like you wouldn't have one function or just like no functions, right?
- 4:39You're like, not really.
- 4:40That's not how you do it.
- 4:41But so there is this, even when you're sort of hunting through tutorials, there is this mismatch of the simple way to show it, the simplest way to show it.
- 4:50And then like maybe how you should.
- 4:52Yeah.
- 4:52Yeah, exactly.
- 4:53It's the example I've given is that there's two types of book slash article slash tutorial.
- 4:58out there.
- 4:59There's here's basics of variables, voice and curls.
- 5:01And then the next level after that is here's how to program a natural language recognition, OCR, AI, machine learning on a Raspberry Pi, your grandmother's base or running on a potato battery.
- 5:11And there's nothing in between.
- 5:13Yeah.
- 5:14Yeah, that's true.
- 5:16It does make it quite challenging.
- 5:18All right.
- 5:18How about now?
- 5:19What are you up to?
- 5:20These days?
- 5:20Oh, gosh.
- 5:21I'm actually kind of veering these days more into actually more into kind of the facilitation business analyst side of things more.
- 5:28Interestingly, the last last couple of years, that's become a point of particular interest.
- 5:32But, you know, in terms of coding, I'm working on I'm working on some interesting projects that, you know, at work building, you know, an API for an internal project.
- 5:39And that's been that's been fun getting to get my hands dirty with FastAPI and SQLAlchemy.
- 5:45And that's been that's been fairly enjoyable.
- 5:47So that's been the majority of my programming bandwidth lately.
- 5:50But it's fun.
- 5:51It's it's the sort of project you hope you're going to get at work because it's an interesting topic.
- 5:55You have enough love leeway.
- 5:59That's where we have enough leeway to do things right and do things to some degree your way if you can justify it well enough.
- 6:05And, you know, other tech lead on it.
- 6:07So I'm able to make sure, like, you know, we're making good decisions early and paying down technical debt and building something that's really, really cool.
- 6:14So it's it's a fun project.
- 6:15I'm enjoying it.
- 6:16Yeah.
- 6:16Those kind of projects, they're they're fun, but they're also important.
- 6:20Right.
- 6:20Like if your work, if you work, if you work at a place where they say, you know, you need to use 10 year old technology and there's no opportunity to try new things like FastAPI and other others.
- 6:31What you end up with is a whole bunch of people who that work there who don't want to learn any new technologies and don't are not deeply passionate about programming.
- 6:39And it's kind of the self-fulfilling prophecy.
- 6:42And I think it's cool that the place you're working at allows that.
- 6:45Right.
- 6:45Because I think it's it's one of the signs of a healthy tech, not necessarily a startup or whatever, but all these companies have like small software teams within it.
- 6:54It's a sign of a healthy small software team, even if it's just building internal software.
- 6:58Oh, definitely.
- 6:59Yeah.
- 6:59And, you know, and there's a place for older technologies.
- 7:01Well, it's good to be well-versed in both and be able to move between it.
- 7:04But I mean, yeah, if you're if you're just stuck in the past, then you either, like you said, get a lot of people that are afraid of change or are really wanting that change.
- 7:13They're dissatisfied with the fact that you can't get it.
- 7:15But on the other hand, if you just if you're only new technology, you'll just spontaneously combust because you can't.
- 7:20You know, there's just there's so much constant invention and reinvention and research and unexpected bugs and blockers and whatever.
- 7:28It's like sometimes the nice thing about a 10 year old library is that there's less bugs because it's years old.
- 7:33Or you can find documentation about issues or whatever.
- 7:36You know, they have a term.
- 7:38It's like a term in the software industry about this constant churn that's like going too quick and especially not appreciating stuff that's been around.
- 7:46It's called JavaScript.
- 7:47You're not wrong.
- 7:51JavaScript is the embodiment of shiny object syndrome.
- 7:53It is.
- 7:54I mean, I'm not bashing on JavaScript per se, but the life cycle of some of these frameworks is just so brief.
- 8:00You know, it's nuts.
- 8:01Everybody wanted to use Dino for about 10 minutes.
- 8:04I remember that.
- 8:05There was a thousand articles a minute on Dino.
- 8:07And now it's now we've moved on to other things.
- 8:10And it's funny because you get a new technology like, OK, the topic of the minute, ChatGPT.
- 8:17And everybody on LinkedIn is an expert in ChatGPT.
- 8:19You know, have you noticed that?
- 8:20Every other post is about ChatGPT.
- 8:22And it's like, well, it's interesting, but it's just a bit early.
- 8:26And the early adopters have a lot of egg.
- 8:28They're still trying to wash off their face in the last six fans.
- 8:30So it's like, let's see what happens.
- 8:33But chances are, this is just an early hiccup.
- 8:36Yeah, for sure.
- 8:37So you mentioned FastAPI and SQLAlchemy.
- 8:39And Yasser out there says SQLAlchemy is the best library.
- 8:42SQLAlchemy is pretty great.
- 8:44And it's cool to see Mike Bayer and crew updated to 2.0 and add async and await to it and stuff.
- 8:50But there's also, especially when you're in Pydantic world, say with FastAPI,
- 8:55there's a SQL model also from Sebastian Ramirez that says,
- 9:01hey, we are already doing Pydantic for the API or front-end exchange.
- 9:06Like, what about making that your database?
- 9:07Did you all evaluate or consider a SQL model?
- 9:10I did.
- 9:11It's one of those things like, this would be great.
- 9:13And I think we can move towards that later.
- 9:15But sometimes when you're paying down technical debt, you have to make a distinction between,
- 9:19do I want to make the monthly payment as it were more than a monthly payment?
- 9:24Or do I want to try and pay it all off in one thing?
- 9:27And sometimes when you're having to budget that time, you know, for us, it made the most sense to migrate from the 1.0 that we were working with earlier on to using, you know, SQLAlchemy 2.
- 9:38And so there was a relatively smaller set of changes that we needed to make.
- 9:41I see.
- 9:42Moving to something like a SQL model would be great, but it would also require us to rewrite some major things.
- 9:49And so that's one of those things is like, well, this might be a good option.
- 9:53I mean, it looks really cool, but let's defer that for right now so that we can actually get some stuff done.
- 9:58It's a constant balancing act.
- 9:59Like, do we pay down technical debt or build new features?
- 10:02And the answer is yes.
- 10:02You have to do both, but you have to keep it in balance.
- 10:06Pay down the credit card, but also make sure you're buying your groceries, you know?
- 10:09Yeah, absolutely.
- 10:10I feel like if you wanted to go that way, the change is pretty straightforward.
- 10:14But if you already have it all set up in the other way, again, it's like, eh, what is the value?
- 10:18Just one thing that kind of timely as of a couple days ago, let's see, as of two days ago, the Pydantic folks, Samuel Colvin and Terrence Dorsey,
- 10:27posted, hey, we're excited to announce the first alpha of Pydantic v2.
- 10:32And I had Samuel on like a year ago to talk about what they were doing there.
- 10:36Wow.
- 10:36But the big news is, the headline news in here is, they rewrote much of the core of Pydantic in Rust.
- 10:44And now Pydantic version 2 is 5 to 50 times faster.
- 10:48Nice.
- 10:49That's cool, right?
- 10:50That's gorgeous.
- 10:51Thank you.
- 10:52This has just put more work on my plate.
- 10:54That's okay.
- 10:54This will be good.
- 10:57I'll have to bring this up at work.
- 10:58It's like, hey, we should look at this later.
- 11:00Yeah.
- 11:00I mean, I don't think that there's any, I don't think anything will, obviously nothing will change for your SQLAlchemy side.
- 11:06But even for FastAPI, I'm sure that this is going to get rolled in in a smooth way.
- 11:10But it does mean that all that data exchange should just go way, way faster, which for basically no effort on users of things like FastAPI and SQL Model and Beanie and all those.
- 11:21So that's cool news.
- 11:22Yeah, definitely.
- 11:23That is neat.
- 11:24Other than the fact that if you do a lot of work directly in Pydantic models, there might be a few small breaking changes.
- 11:30But let's talk about our things.
- 11:33The things we wish someone had explained to us sooner.
- 11:36Yes.
- 11:37And I guess we can start with this.
- 11:38Like these things were motivated.
- 11:40We'll come back and talk a bit more about this at the end.
- 11:42But they were motivated.
- 11:43This whole conversation was motivated.
- 11:44This big, long book that you wrote here, Dead Simple Python.
- 11:48Give people the elevator pitch and then we'll get to all things.
- 11:50Yeah, exactly.
- 11:50So Dead Simple Python is the book I wish I had.
- 11:53And yeah, it's, I get teased a lot.
- 11:55It's like dead simple and it's that thick.
- 11:57But it's dead simple in the same way that Python is obvious.
- 12:01It's obvious retrospective.
- 12:03Things are dead simple.
- 12:04Once you understand it, you look back and go, oh, okay.
- 12:07That clicked.
- 12:07That just makes sense.
- 12:08A lot of things in Python are like that.
- 12:10You look into it.
- 12:10It's like, that looks really complicated.
- 12:12How is this ASIC supposed to work?
- 12:14And then you look back and go, oh, that's all?
- 12:16Okay.
- 12:17No, that's.
- 12:17Yeah, yeah, exactly.
- 12:18Why was I afraid?
- 12:19Why did I put learning, put off learning this for six months?
- 12:21Exactly.
- 12:22It's, you know, things look more intimidating than they are in practice.
- 12:28But basically this is the book for those persons who already know another programming language,
- 12:34or maybe they've even been working with Python for a while.
- 12:37Like they're familiar with the basics.
- 12:39They do not want to have their hand held through the, you know, this is what a variable is.
- 12:44And this is what a function is.
- 12:45They've been through this probably more times than they want to admit to.
- 12:50And they want to get into what's different about Python and how to use Python as Python.
- 12:56I think that's kind of the core of the book is there's a big difference between Python code
- 13:00that works and Python code that makes the most of the language and its patterns, what we would
- 13:06refer to as Pythonic code.
- 13:08And there's a big difference between the two because it's very tempting to come in and write Python as
- 13:12BB dot that in my case, because that's where I started or write Python as C or write Python as Java or write Python as Ruby.
- 13:19And we wind up holding ourselves back and having really convoluted and hard to maintain code because we don't understand the patterns.
- 13:25So the approach I took with the book is let me explain why we do things this way.
- 13:30Let's go all the way down into the internals and how this actually works under the hood.
- 13:37So by the time you get to the forward with statements, you know, the meta class or whatever, you're like, oh, I understand what this is, why it's there, how it's used and when not to use it.
- 13:46Yeah, that's a great service.
- 13:48So you're saying if I created a class called like user factory and it implemented I user factory and then I would use dependency injection.
- 13:58I might be doing that wrong.
- 13:59You might be writing C# and Python.
- 14:01Yeah, indeed.
- 14:03Yeah, indeed.
- 14:03This portion of Talk Python To Me is brought to you by Sentry.
- 14:08Is your Python application fast or does it sometimes suffer from slowdowns and unexpected latency?
- 14:15Does this usually only happen in production?
- 14:18It's really tough to track down the problems at that point, isn't it?
- 14:21If you've looked at APM application performance monitoring products before, they may have felt out of place for software teams.
- 14:28Many of them are more focused on legacy problems made for ops and infrastructure teams to keep their infrastructure and services up and running.
- 14:36Sentry has just launched their new APM service.
- 14:40And Sentry's approach to application monitoring is focused on being actionable, affordable and actually built for developers.
- 14:47Whether it's a slow running query or latent payment endpoint that's at risk of timing out and causing sales to tank,
- 14:53Sentry removes the complexity and does the analysis for you,
- 14:57surfacing the most critical performance issues so you can address them immediately.
- 15:00Most legacy APM tools focus on an ingest everything approach, resulting in high storage costs, noisy environments,
- 15:08and an enormous amount of telemetry data most developers will never need to analyze.
- 15:13Sentry has taken a different approach, building the most affordable APM solution in the market.
- 15:19They've removed the noise and extract the maximum value out of your performance data while passing the savings directly onto you,
- 15:25especially for Talk Python listeners who use the code Talk Python.
- 15:29So get started at talkpython.fm/sentry and be sure to use their code Talk Python all lowercase
- 15:36so you let them know that you heard about them from us.
- 15:39My thanks to Sentry for keeping this podcast going strong.
- 15:45So I like the idea of it.
- 15:47Let's talk.
- 15:48Let's go through the topics.
- 15:50We both contributed a little bit.
- 15:52Mostly, I'll throw in a few if we got extra time.
- 15:54Yeah, yeah.
- 15:55So the first one has to do with this, I guess, like really early stage writing code, like how do I create variables?
- 16:01What are the deals with types and variables, but also types and objects?
- 16:05Tell us about that.
- 16:05Yeah, exactly.
- 16:06How names and values actually work.
- 16:08And this is something that I had to learn early, but I didn't fully understand.
- 16:13So when I first logged into the Python IRC room back when I learned this language, you know, back in the Cretaceous period, I logged in and I, you know, I'm coming from a strongly typed background.
- 16:23So I'm saying, okay, so how do I declare a data type on a variable?
- 16:27Because I'm used to VB.
- 16:28I'm like, yeah, dim in the name of the variable, the type equals whatever.
- 16:31How do I declare a type?
- 16:32It was before type hints existed.
- 16:34And one of the guys in the room goes, you're a data type.
- 16:37And I'm like, sorry, what?
- 16:38And he goes on to explain, well, it doesn't quite work this way in Python, but I didn't realize just how different it was.
- 16:44So because I just sort of had this vague, like, oh, you just have the name equals the value.
- 16:48Cool.
- 16:49Type still seems kind of important.
- 16:50Maybe I should start doing Hungarian notation.
- 16:52Don't do that.
- 16:53By the way.
- 16:54S Z username.
- 16:56No.
- 16:57Exactly.
- 16:58Exactly.
- 16:59Int age or whatever.
- 17:00Yeah.
- 17:01And I thought I was being super clever and I wasn't.
- 17:03But eventually someone explained to me, I think it was actually Ned Batchelder.
- 17:07Someone gave me the link to Ned Batchel.
- 17:08And Batchelder's talk on this is that Python's variables work very differently than other languages.
- 17:14So much so that some smart elics like to say, well, Python doesn't really have variables.
- 17:18Yes, it does.
- 17:19But they're variables in a different sense.
- 17:21You have names, which are just like they're like labels and you can call it whatever you want.
- 17:25But a name has a scope.
- 17:27You know, so the name exists in the context of your whole program or in the context of your function or whatever.
- 17:32And a name can be bound to whatever you want.
- 17:34Imagine drawing, you know, basically having this label on a corkboard, thing like a conspiracy theorist corkboard.
- 17:41You know, you have the tack in a label and you put a little string around it and you tie that string to this other thing over here.
- 17:48And that's your value.
- 17:49Your value has a tie.
- 17:50It has.
- 17:51It is one thing.
- 17:53It's a Boolean or it is an integer or it is a string or it is a list.
- 17:56We've all seen the error.
- 17:57You cannot combine integer and string with plus operation.
- 18:01Like they know what they are, right?
- 18:02Exactly.
- 18:03Those values know what they are.
- 18:04And that's something that throws people off coming from JavaScript where everything's weakly typed.
- 18:08It'll just try to coerce them together.
- 18:09Python says, hey, these two values are different.
- 18:11I'm not going to try to figure this out.
- 18:12That's because it's working with the values.
- 18:14The values have type, but the values don't have scope.
- 18:17And this is where people get thrown off because if you say, you know, let's say I'm going to use the example here.
- 18:25X equals spam.
- 18:26If you were to say Z equals X, you're binding Z to the same value as X.
- 18:31And then if you decide to change Z, that works.
- 18:34X is unchanged.
- 18:35Z is changed.
- 18:36You might think, oh, cool.
- 18:37Okay.
- 18:37This is just, it just made a copy.
- 18:39Not exactly.
- 18:40Because we have this whole thing about mutable and immutable types.
- 18:44Some things can't be changed.
- 18:46Boolean, an integer, a string, a tuple.
- 18:48They're not changed.
- 18:49The value is never modified in memory.
- 18:51A new thing is made instead.
- 18:53But then some, some, and by some, I mean most.
- 18:57Sorry about my clock.
- 18:58Most types in Python, like lists and dictionaries and the objects that you're creating with your fancy little classes are mutable.
- 19:05So if you create a list or a dictionary and you bind that to two different names, you modify, you add a value to that list.
- 19:14It doesn't matter which of the names you use, you're modifying the value in memory, in place.
- 19:19And now that is accessible from either of those names.
- 19:22Yeah.
- 19:22And that can be confusing, especially when you start to understand that functions don't pass by value.
- 19:26They don't pass by reference.
- 19:27They just bind the value to the name of that argument.
- 19:31Pass by assignment is what we usually call it.
- 19:34So you're just having one more handle, one more alias pointing to that value in memory.
- 19:38So if you change on that argument, you're just changing your spot number.
- 19:42Many languages have values that are like local and copied types of values.
- 19:49So you mentioned VB.net, which like the integer and float numbers and things like that.
- 19:56If you assign one, it's a copy of a thing.
- 19:58Whereas Python, literally everything is a pointer, right?
- 20:02This corkboard conspiracy theory thing, right?
- 20:07There's no concept of, oh, I created this on the stack.
- 20:09And when the function returns, it's gone.
- 20:11It's everything is a, you know, even like a, just an integer is a pi long star down in the runtime.
- 20:17Right.
- 20:18That's a bit of a mental thing people got to get used to just like how much, because when
- 20:24you come from a language like C++ or something, pointers and these reference type type of ideas
- 20:29are like right in your face, right?
- 20:31Like you see this, like I just said, pi long star, like the star means it points at things
- 20:36without the star.
- 20:37It's on the stack, right?
- 20:38It's a, you are way down in those details.
- 20:41Whereas in Python, you never see pointers.
- 20:43Like you could imagine, like there's no such, like we don't even have pointers in Python.
- 20:46Right.
- 20:47But the irony is like literally everything is basically managed in that on the heap reference
- 20:52type style of work.
- 20:54So that's certainly something I wish I knew.
- 20:56Yeah.
- 20:57And, and of course, then people come from C++ and say like, oh, cool, that's easy.
- 21:01So it's all pointers.
- 21:02Well, once again, Python being Python, not quite because it's actually technically called a
- 21:07reference, not a pointer.
- 21:08And that's important because a Python reference or a Python value has two things.
- 21:15It has, yes, it does have a pointer, at least we're talking about CPython, but it also has
- 21:19a reference counter, which is a self and integer.
- 21:21And that's adding overhead.
- 21:22So if you ever do actually go down and look at the sizes of things, you will notice that
- 21:28your base value has this additional overhead.
- 21:31Yeah.
- 21:32two numbers.
- 21:33One of them is your pointer and the other one is your, the other one is your, your reference
- 21:38counter because it has a reference counter garbage collection.
- 21:40Yeah.
- 21:41Often a lot more.
- 21:41I think trying to, I guess I could check, but either an integer or a single character.
- 21:48I can't remember which is 28 bytes.
- 21:50Right.
- 21:51And that's in C, the single character would be one or two bytes and the integer would be
- 21:56two, four or eight.
- 21:57Right.
- 21:58And so there's just a big difference in the way you got a sort of like the implications
- 22:02of that.
- 22:02So related to that, kind of moving on to the next, before we move on, the other thing that
- 22:07you mentioned, and I think it's also a really big deal in this area is the scope of a variable.
- 22:13And so in some languages, you have very fine grained control of the lifetime of a variable
- 22:19by where you declare it.
- 22:21So for example, if you declare a variable in a C++ or VB.net or whatever in a if block,
- 22:29when you leave the if block, the thing goes away.
- 22:31Like if there's curly braces around it in a lot of languages, like that is the lifetime of
- 22:36the variable innermost bracket of curly braces, like outside of a loop, the thing goes away
- 22:42in Python.
- 22:43We don't have that.
- 22:44And that's a, you know, kind of the variable persists and lives after afterwards.
- 22:48Right.
- 22:49Which is interesting.
- 22:50Yeah.
- 22:51difference.
- 22:51Yeah.
- 22:51Because the garbage collection is something you're never supposed to have to deal with.
- 22:55So even if you call delete on something, it's not necessarily guaranteed to be cleaned up.
- 23:00And when you expect it, like the language is going to handle things when it, when it sees
- 23:05fit for whatever reason.
- 23:06So yeah, there's only to my knowledge, there's only three, there's only actually three scopes
- 23:11in Python.
- 23:12There's global level.
- 23:13There's module level and there's, I don't even, that gets a little pedantic and then you
- 23:20have function level classes are not their own skull.
- 23:23Classes actually do not, a class exists in the global level.
- 23:28And whenever you have an attribute sitting on a class or sitting on an instance is actually
- 23:32in a dictionary, unless using slots.
- 23:34I'm not going into that.
- 23:35Actually sitting in a dictionary that is a part of that object in memory.
- 23:41Yep.
- 23:41It's special, but it works out.
- 23:43It's just something you got to kind of get used to.
- 23:45Definitely.
- 23:45All right.
- 23:45Next one.
- 23:46Ducks.
- 23:47Quank.
- 23:47Duck typing.
- 23:49Yeah.
- 23:49And, and it's, this is where I went.
- 23:52Oh, haywire.
- 23:53When I, when I was doing that, that Hungarian notation, I mentioned it.
- 23:56I'm thinking, I'm being so clever.
- 23:57I'm just putting the name of the, or the type in the name.
- 24:01And then I know it.
- 24:02But unfortunately you're coming from a stronger type language.
- 24:05You're actually, it's easy to underestimate just how much strong typing can actually hold
- 24:10us back in some cases, because we started thinking about what a thing is rather than
- 24:14what it can do.
- 24:15And we see this in Java.
- 24:16Like you have to have these complex inheritance patterns because you want to be able to say,
- 24:19well, this thing is, it is this matches this interface.
- 24:23You know, it, it does this particular thing.
- 24:25Python's take is I really don't care what it is.
- 24:29As long as I can interact with it, according to a particular kind of set of assumptions,
- 24:34then I don't care what it's really doing under God.
- 24:37And this can be really beneficial because it allows you to create new things that act like
- 24:41integers, even though they're not integers or new things that act like Booleans that aren't
- 24:45Booleans, new things that act like strings that aren't strings.
- 24:47Python doesn't care.
- 24:48It's just interfacing with those objects on the basis of how it can use them and what certain
- 24:57things are going to return.
- 24:58That's all it cares about.
- 24:59So the concept of the duck typing being, it doesn't care whether it's a duck or a picture
- 25:03of a duck or a statue of a duck or a moose in a duck suit.
- 25:05As long as it can interact with it in the way it needs, it's good.
- 25:08Yeah.
- 25:09And that's very free.
- 25:10And talks like a duck and quacks like a duck.
- 25:12And it's probably a duck.
- 25:13Yeah, exactly.
- 25:14And it's very freeing once you get that because you don't have to muck about with all this weird
- 25:18inheritance anymore.
- 25:19It's like, well, it, it does what I need.
- 25:22And that's the important part.
- 25:23There is certainly less inheritance and less complex hierarchies in Python.
- 25:27Not, not that there's not an important place for object-oriented programming and classes.
- 25:31They're just often simpler.
- 25:33A lot more straightforward, definitely.
- 25:35And it frees us up to then the saying I coined a couple of years ago during a conference talk was,
- 25:41was that you need to define objects by their constituent data.
- 25:44So when you're writing a class or you're designing an object, you're thinking about what is it storing?
- 25:50Like what, what sort of state, if you think in a functional way, or what sort of data am I really putting in here?
- 25:57And you can design based off of that.
- 26:01And then you can think about, well, how do I want it?
- 26:03How does the system need to interact with that data?
- 26:07But you don't really have to sit there and think about encapsulating behavior because that's not really the point.
- 26:11The point is you can think about, like with the example I use is, we always see the example of,
- 26:16I'm going to create a class called cat and a class called dog and they inherit from animal.
- 26:19And that's actually a really lousy example we need to stop using because that's starting from the concept of behavior.
- 26:26Well, it barks, it meows, it eats, whatever.
- 26:29And in real systems, what we think about is I have a, you know, someone designing a game.
- 26:35I have a monster.
- 26:36Okay.
- 26:37What is a monster?
- 26:38Well, it's, it's a bad guy.
- 26:40No, no, no, no, not what it does.
- 26:42What is it?
- 26:42Well, it's, that's a collection of statistics about the character, you know, like the, how, how much strength it has and how much health it has.
- 26:49Okay.
- 26:50That is not a monster.
- 26:52That's an, that's an entity.
- 26:53That's a, you know, that you don't have to put what it's doing into the, into that code.
- 26:58It is this collection of information.
- 27:01And when you start, when you shift your thinking to a data first mindset about object oriented, then you find that you can actually use it with functional paradigms that are sometimes a lot easier to reason about.
- 27:12Yeah.
- 27:12The, the grouping together of data is an important part.
- 27:14I would say the most extreme example of duck typing in Python, maybe a couple of areas.
- 27:22And they all seem to involve, revolve around the magic methods or the Python data types, like the Dunder methods.
- 27:28Right.
- 27:28I love those.
- 27:29I could.
- 27:29Yeah, I know.
- 27:30So I could have a function, which I could call, or I could have a class that implements Dunder call is a callable, but it's actually an instance of an object.
- 27:40Right.
- 27:40And, but as far as Python is concerned, it doesn't matter.
- 27:43It can call it and pass parameters to the thing it called.
- 27:46Right.
- 27:47And there's no real type that disambiguates those two things.
- 27:50Like one is a function type.
- 27:51One is a, you know, a class with a particular method on it.
- 27:54Right.
- 27:55But they work just fine.
- 27:56And that's a pretty extreme example.
- 27:58You know, another one would be, I have a list I can iterate, or I have something that implements the iterator pattern through the Dunder methods.
- 28:07Right.
- 28:07Like the Dunder iter and so on.
- 28:09Right.
- 28:09Well, yeah.
- 28:10One of the classes I, one of the examples of the book I gave was like a, a cafe queue.
- 28:15So a queue of customers in a line.
- 28:17And yeah, I wrote it as inevitable, but there was all this additional stuff in there.
- 28:22It wasn't just a basic list.
- 28:23There was more to it than that.
- 28:25And, but because I implemented as an iterable, I didn't care.
- 28:28I could use it like I could use a list.
- 28:30Yeah.
- 28:30But it was able to do this additional work with data behind the scenes that made it just easier to use the, to reason about this line of customers.
- 28:41And then you get generator methods with the yield keyword and yield from, and like all sorts of stuff.
- 28:46You, you get pie test fixtures that allow you to set up and tear down without actually writing.
- 28:53Try finally blocks and all these things.
- 28:55A lot, a lot of interesting places.
- 28:57While I was thinking about this conversation and this topic in particular, I ran across an article called static duck typing in Python with protocols.
- 29:05How's that to like crash two ideas together?
- 29:08Yeah.
- 29:09It's well, when we look at data types, it's like the way Python even does its data types is yeah.
- 29:16You can say that you want to say, okay, this is a list and you can type it as a list, but you could also just actually type it as an iterable.
- 29:24And basically tell Python what you care about.
- 29:27Do you really care that it's a list?
- 29:30Probably not.
- 29:31Yeah.
- 29:31Do you care that it's an iterable that you can, that you can work with similar to a list?
- 29:36They're like, how are you going to use it?
- 29:37That's what you ask yourself.
- 29:38Then you base your annotations around that.
- 29:40And so you can say, well, I care measurement like I want it to, I want it to be usable as a measurement.
- 29:47I don't really care what it is.
- 29:48It doesn't need to be this particular type of class.
- 29:51And so that's moving from like we're talking about moving from Java thinking into Python thinking is, well, it's not what it is.
- 29:57It's what it does.
- 29:58Yeah.
- 29:58A lot of the types people put down are like, I need to treat it like some duck typing situation.
- 30:04So it needs to have these attributes about it.
- 30:08Let's try to put that aspect of it into the formal static typing rather than earlier.
- 30:13Yeah.
- 30:13Okay.
- 30:14Maybe a quick audience question or a bit of feedback here before we move on to the next one.
- 30:18It's relevant.
- 30:19Nick says, early in my Python journey, I wish I had gotten better understanding of all the intricacies, differences, and use cases for various data structures like set versus list.
- 30:28Right.
- 30:28Yeah.
- 30:29Versus dictionary.
- 30:30And those are certainly, even if you've done programming before, maybe that environment language didn't deeply embrace these data structures.
- 30:37So certainly those are table stakes for being good at Python.
- 30:40Absolutely.
- 30:41Use cases hits the nail on the head because it's like, what's the difference between them?
- 30:45Well, you know, it's a collection.
- 30:46And so maybe that's all you care about.
- 30:49But I find myself using all of them in different cases because it's like, all right, I never need to add or remove things from it.
- 30:54I'd probably a tuple.
- 30:55Or I need to, or I need to be able to hash it, use it as an immutable.
- 30:59That's a tuple.
- 31:00Or I need to be able to see what's in group A and not group B.
- 31:03That's set.
- 31:04Or I care about the order.
- 31:05That's a list.
- 31:06Or I want to be able to associate two things.
- 31:08That's a dictionary.
- 31:08So knowing your different data structures is helpful.
- 31:12And, you know, it's tricky because Python's data structures aren't really all that unique, but they have different names than maybe are used to whatever languages.
- 31:19You know, you come across lists and what is that?
- 31:22Well, it's a C++ vector or a JavaScript array.
- 31:24You know, it's like it's, it's, which is where it's helpful to come back to Python kind of with a fresh mind.
- 31:30It's like, what is this?
- 31:32Not assuming that things have the same names, but being prepared to get a different vocabulary.
- 31:36This portion of Talk Python To Me is brought to you by us over at Talk Python Training with our courses.
- 31:43And I want to tell you about a brand new one that I'm super excited about.
- 31:47Python web apps that fly with CDNs.
- 31:50If you have a Python web app, you want it to go super fast.
- 31:54Static resources turn out to be a huge portion of that equation.
- 31:58Leveraging a CDN could save you up to 75% of your server load and make your app way faster for users.
- 32:05And this course is a step-by-step guide on how to do it.
- 32:09And using a CDN to make your Python apps faster is way easier than you think.
- 32:14So if you've got a Python web app and you would like to have it scaled out globally,
- 32:19if you'd like to have your users have a much better experience and maybe even save some money on server hosting and bandwidth,
- 32:26check out this course over at talkpython.fm/courses.
- 32:30It'll be right up there at the top.
- 32:31And of course, the link will be in your show notes.
- 32:34Thank you to everyone who's taken one of our courses.
- 32:36It really helps support the podcast.
- 32:37Now back to the show.
- 32:43Iteration, iterables, iterators, living.
- 32:45Yes.
- 32:46I was working in some code last year and I came across something that just, I was, I cringed and then I laughed out loud because this was written.
- 32:55So this code was written by who I best guess university data scientists.
- 33:01So, you know, to their favor, this is not a software engineer.
- 33:05What they had written and they could tell it had been refactored a few times, but I am not kidding.
- 33:10They had said, all right, so I have this thing that it's iterable.
- 33:13It was already something that we could use in a loop.
- 33:15And we'll come back to what that is for anyone who's curious.
- 33:17But we had this iterable.
- 33:18I'm going to convert that into a list.
- 33:20By the way, we're talking about, in this case, tens of thousands of items in that list.
- 33:25I'm going to convert that into a list.
- 33:26Then on the next line, they say for index, comma, value in enumerate.
- 33:33Enumerate's a thing that lets you get the indexes as well.
- 33:36Enumerate that list.
- 33:37And then it says, you know, and then it accessed the value again inside the loop by going to the original list and accessing using the index.
- 33:46And I just sat there and I kind of half laugh, half cry.
- 33:50It's just like there is so much.
- 33:52This is so convoluted.
- 33:54And all I had to do was take all that logic out and just say for item in original iterable.
- 34:00And then item was the thing.
- 34:02But what had happened was that they didn't understand what was really happening in a for loop.
- 34:06They were still thinking C or C++.
- 34:08And the thing is that Python has these two magic methods called iter and next.
- 34:13Itter just says, give me the thing I can use that has the next operator that knows where it is in the list.
- 34:19I'm going to leave it at that.
- 34:20And the next just says, give me the next item.
- 34:22Yeah.
- 34:22That's all it is.
- 34:23Just give me the next item.
- 34:24All a for loop ever does is it grabs that, is it calls that next.
- 34:28It's like, give me the next thing.
- 34:29All right, good.
- 34:30I'm done.
- 34:30Give me the next thing.
- 34:31Give me the next thing.
- 34:31Give me the next thing.
- 34:32You can write entire classes that behave like this, even generate the value at that moment.
- 34:38It's called lazy iteration.
- 34:40You can actually generate the next value when it's wanted, not a moment sooner, which means you only ever have one value in memory sometimes.
- 34:47Yeah.
- 34:48And then you can just put that in the for loop.
- 34:49You come up with any arbitrary name for when you access it for each thing in iterable, name a thing, whatever you want, and then use it directly in your code.
- 34:58And I wish someone had told me that a lot sooner, discovered that about three years into my journey.
- 35:03I'm like, oh my gosh, I've been making lists of everything and I've been doing it.
- 35:06It's like, what a waste.
- 35:07Yeah.
- 35:07And, you know, the yield keyword, obviously, is a super easy way to do that lazy iteration that you talk about.
- 35:12But it also is really important for things like databases, right?
- 35:16Yeah.
- 35:16If you talk SQLAlchemy, right?
- 35:18I'm going to do a query.
- 35:19I could call the all method and turn it into an in-memory list, or I could, if I don't need that, I could just loop over it, right?
- 35:25And kind of read it cursor style, potentially stop talking to the database halfway through.
- 35:29Exactly.
- 35:29If I want to need it, I don't need the rest of the data.
- 35:31Exactly.
- 35:32Yeah, exactly.
- 35:32Yeah, interesting.
- 35:33Cool.
- 35:34Yeah, I think iteration is quite, it's settled because there's only two types of loops in Python.
- 35:41And usually we're using the for loops.
- 35:43But the variations, there's just so many of them, right?
- 35:47You can have your for loop that just goes over a thing.
- 35:50But what if you need the index like you described?
- 35:52Not for this improper use.
- 35:53But sometimes you actually still do need the index.
- 35:55So you could do enumerate of that thing and get a tuple.
- 35:59But you could take it to another level, do tuple unpacking and say like index comma value in enumerate of things.
- 36:04Right.
- 36:05And you get it back.
- 36:06And you can unpack a dictionary too.
- 36:08You can unpack a tuple with that in the dictionary.
- 36:10There's this whole library and it built into Python called iter tools.
- 36:14And if you ever have, you know, 10 minutes over coffee, just pull up the documentation for iter tools and just kind of scroll through.
- 36:19There's some neat stuff.
- 36:21Like you can, you can like find every possible combination of three different lists.
- 36:25I just give it three different lists.
- 36:27And this is one that will take all three of them.
- 36:28Just give you every single possible combination of it.
- 36:31I actually use it in the book.
- 36:32And it's just, there's all these neat things you can do with iterables.
- 36:35Some of them don't even have to stop.
- 36:36There are infinite iterables where you can just say, I want to get this sequence repeating infinitely.
- 36:42One, two, three.
- 36:42One, two, three.
- 36:43One, two, three.
- 36:43One, two, three.
- 36:44One, two, three.
- 36:44One, two, three.
- 36:44And two lines of code.
- 36:46And you can have that repeating forever.
- 36:47And if you're using an old fashioned while loop or an old fashioned C++ style for loop, that would be multiple lines of code.
- 36:53Like, you know, you have to keep track of the state and all that stuff.
- 36:56Yeah.
- 36:56I think it's two lines.
- 36:57Yeah.
- 36:57It's amazing.
- 36:58Yeah.
- 36:59So there's, there's a lot of depth, even though it seems so simple when you first look at it there.
- 37:02Closely related to that are comprehensions.
- 37:04Yeah.
- 37:05Like a list comprehension, right?
- 37:07People often see a list comprehension or a set comprehension and go, especially a list comprehension, they're like, oh, that's the same thing as a for loop.
- 37:15Where I create, before the for loop, I create a list.
- 37:17And then in the for loop, I add the thing to the list.
- 37:19And these are the same.
- 37:20May have the same outcome, but they're not actually the same thing.
- 37:24Like the runtime performance is not the same.
- 37:26The bytecode implementation of those two things is not the same.
- 37:30This is like a nonstop debate I have on YouTube whenever I put a video and mention something about list comprehension.
- 37:35They're like, that's just a for loop.
- 37:37It's not actually just a for loop.
- 37:38It's like a kind of accomplishing the same thing.
- 37:41But we now have for potentially 312, Carl Meyer put out a PEP 709 called inlined list comprehensions, which are supposed to take some of the mostly invisible behind the scene implementation and make comprehensions two times faster.
- 37:59I believe they're already faster than this looping story I talked about, but this would make them faster still, which is pretty cool.
- 38:06Wow.
- 38:06How does this how does this syntax work?
- 38:09I'm curious.
- 38:09It's just like it's just an internal change.
- 38:11Okay.
- 38:12So the syntax is unchanged.
- 38:14Yeah.
- 38:15So the way here's the magic of list comprehensions.
- 38:17That's funky that people maybe aren't aware of is it.
- 38:20It used to actually create a list comprehension object that implemented the loop in a function.
- 38:26And then it called that function as if you'd like nested a function to create the isolation of the list comprehension.
- 38:32So that created a stack frame.
- 38:34I created an object.
- 38:35Then it did function in direction and all those things made it slower than it needs to be.
- 38:40So now they create a new load fast and clear byte code operation that like stores the state of the function, then does the list comprehension and then pops it back off the stack to unwind it.
- 38:52So anyway, comprehensions are interesting.
- 38:55I just wanted to this is like kind of a brand new thing.
- 38:57That's cool.
- 38:57That's coming along.
- 38:59That might be interesting for people.
- 39:00Well, I think it's worth mentioning then that what a list comprehension really is.
- 39:05So like if I unpack it because people think, oh, people use that term very loosely.
- 39:09A lot of times they want the list comprehension.
- 39:11But what is a list comprehension?
- 39:12And if I unpack that, what it really is, is it is a generator expression that is being unpacked into a list.
- 39:23So what's a generator expression?
- 39:25Generator expression is a type of generator.
- 39:27What's a generator?
- 39:27A generator is a function or type of function that is itself iterable.
- 39:34So that's where this is.
- 39:36That's where this is critical is that a generator you can you can write a generator that what when call it that it has.
- 39:43You mentioned that earlier, this yield keyword.
- 39:45And so you can do whatever you want in this function.
- 39:49You can write it however much logic you want.
- 39:51Every time you hit the yield statement, it will return that value.
- 39:55And then it's going to wait for the next call.
- 39:56Then the next iteration of that, like the next call to next on that generator.
- 40:01And then it's going to run through again until it hits a yield.
- 40:03And then it's going to stop, turn that value and wait again.
- 40:06And you can do some incredible things with these.
- 40:08But once you understand the generator, which are incredibly powerful tools,
- 40:13then you can write a generator expression, which is I discovered later.
- 40:17A generator expression is to a generator as a lambda is to a function.
- 40:21It's an anonymous generator.
- 40:23That's all it is.
- 40:24A lambda is just a very small inline anonymous function.
- 40:29Has the logic.
- 40:30It can take in some values, return some values.
- 40:32It's a lot simpler, a lot shorter.
- 40:33And you do it right in place.
- 40:35So you're not having to go off somewhere and write a different function and come back.
- 40:38That's why we have anonymous functions is so that we can just do it like,
- 40:42like you see there at the bottom, reduce and just some rules right there instead of writing a separate function.
- 40:47Generate expression, same sort of thing.
- 40:49We're doing writing some generator logic.
- 40:51It's shorter.
- 40:52It's creating a generator, but it's just a lot smaller.
- 40:56And if you just wrap it in in parentheses, you can use it as a generator.
- 40:59But when you change those parentheses to be square brackets, what happens is you're running that generator and then you're loading all the values into the list.
- 41:08You do it in in curly braces.
- 41:10You're creating a set.
- 41:12Or if you're if you're doing the same thing, but you are the return value in each step is a key value pair.
- 41:18Then you're going to create a dictionary.
- 41:19But all you're really doing is unpacking that generator into a data structure.
- 41:23So sometimes you want to think when you're mucking about with these, do you really want the data structure when you're done or do you just want to go over the values?
- 41:32Maybe you just write a generator expression.
- 41:34Yeah.
- 41:34The difference is super subtle.
- 41:35Like, do you have square brackets or do you have parentheses around the expression?
- 41:39But the effect or consequence of that is very significant at runtime, right?
- 41:44It's kind of like that example you talked about putting everything in a list and looping over it or just looping over it.
- 41:49Right.
- 41:49Exactly.
- 41:50And because these are these are these one liners and we as coders tend to get a text of these one liners.
- 41:55It's like, oh, look at all the cool things I could do.
- 41:58I have a couple of examples in the book.
- 41:59Some real list comprehensions I've come across or some colleagues of mine have come across where it's like it's multiple lines.
- 42:05And that's a bad list comprehension.
- 42:08That's a bad generator expression.
- 42:09Just like you don't want to have a multi-line lambda at that point.
- 42:12Write a function.
- 42:13Generator expressions and lambdas are supposed to be these really small, short, in place bits of logic.
- 42:18And if it's getting complicated, write the full size thing.
- 42:22It's a lot easier to debug.
- 42:23If nothing else is usually to debug, but it's also a lot easier to read.
- 42:26It is.
- 42:27It is.
- 42:28Just why we're kind of ranching on these things.
- 42:31Like, I wish there was a way to specify a sort in these list comprehensions, generate expressions, and maybe not the generators because that gets tricky.
- 42:40But so often I find myself I'm doing a list comprehension and then I got to turn around and sort it.
- 42:44Like if it could just just add that in there, that would make me happy.
- 42:47All right.
- 42:48Moving on.
- 42:48Packaging.
- 42:50Yes.
- 42:51Deciding on packaging and all that right up front.
- 42:54Actually, before we get to that, I guess the other one that you threw in here is virtual environments.
- 42:58Indeed.
- 42:59Yeah.
- 42:59Virtual environments.
- 43:00Use them.
- 43:01They're the only.
- 43:02I avoided them for years because I thought, oh, these are scary.
- 43:04I don't understand them.
- 43:06They're not scary.
- 43:07I'm actually going to.
- 43:08I'm actually going to hijack something.
- 43:09I learned from one of my guests on my own podcast recently is what containers actually are.
- 43:14By the way, Docker containers.
- 43:15This is where this is relevant.
- 43:17You know what a Docker container actually is?
- 43:19Is it's the folder on your operating system?
- 43:20And it's just changing the route.
- 43:22So when you're working inside of it, it's changing the route.
- 43:24There's a bunch of magic logic around that.
- 43:27But that's all it is.
- 43:27It's like it's pretending that that folder is the route and it's running things accordingly.
- 43:32That's all a virtual environment really is doing either.
- 43:35So there's not a lot of difference between.
- 43:37It lies to Python about the path.
- 43:39Yeah.
- 43:40There's not a lot of difference between the virtual environment and the Docker image.
- 43:43Like they're the same fundamental concept.
- 43:45So all you're doing is you're creating this little spot on your, this little folder.
- 43:49And you're saying, these are my Python packages.
- 43:51And unless you tell it otherwise, don't go looking anywhere else.
- 43:55These are the ones I want to use.
- 43:56And they're super easy to create.
- 43:59It's like one line to create a virtual environment.
- 44:01And it lets you maintain exactly the packages you want for that particular situation.
- 44:06And it's great.
- 44:07It is.
- 44:08Yeah.
- 44:08It's once you get used to it, it's great.
- 44:11I remember it being frustrating at the beginning.
- 44:13You're like, ah, I don't remember exactly.
- 44:15How do I create the environment?
- 44:17How do I activate it?
- 44:17How do I know?
- 44:18Do I really need this?
- 44:19And yeah, I don't, I don't start any projects without having them as just straight up using
- 44:25them right away.
- 44:25It's like, absolutely.
- 44:26Yeah.
- 44:26I probably have created them for projects that don't even have external dependencies because I was just
- 44:30like on autopilot.
- 44:31Like, oh, actually I didn't need that, but whatever.
- 44:32It's still good to have.
- 44:33Yeah.
- 44:33I would absolutely control which Python version you're using.
- 44:36And then you can install whatever packages you want.
- 44:38And you don't not worry about messing up your system or user scope packages.
- 44:42And I had a coworker recently say, I dream of the day when I can actually remember how to
- 44:46activate this, but it's actually really easy.
- 44:48So once you, so assuming you know the name of the folder, so you can pick the name of the
- 44:53folder you use for your virtual environment.
- 44:55Traditionally, it's called like them, V-E-N-B.
- 44:57And it's, here's the thing to remember.
- 44:59You don't actually have to activate your virtual environment.
- 45:02Put that out of your mind for a moment.
- 45:03You can run things directly in the virtual environment.
- 45:06So in Unix systems like Linux and Mac, there's a bin folder.
- 45:11Vim slash bin slash, and then whatever Python, whatever Python executable you want to run.
- 45:16It could be Python.
- 45:17It could be pip.
- 45:18It could be your own package.
- 45:19You can actually just navigate into that bin folder.
- 45:22And so I can say Vim, bin, pip, and I've got the pip.
- 45:25And that knows to only work within its virtual environment.
- 45:29It's not going to break out of that container.
- 45:30Once you understand that, here's where they activate script is.
- 45:33Vim, bin, activate.
- 45:35It lives in the bin file.
- 45:37And if you're on Windows, the only thing you're changing is you're going from bin to capital S scripts.
- 45:42And then the things have an ending of like either .bat or .ps1, depending on what show you're using.
- 45:49But that's the structure of a virtual environment.
- 45:52And once you understand that, they're a little overscuried.
- 45:54Yeah, my RC file for my user profile on my Mac is just full of here's the Python executable in some virtual environment.
- 46:02And here's the script and commands to send to it for like little aliases and stuff.
- 46:06And like none of those activate the virtual environments.
- 46:08They just run the Python in the virtual environment to make all the magic happen.
- 46:12Another thing I would recommend is people think about creating aliases that make virtual environments happier and easier to use.
- 46:20So, for example, and you could do this in PowerShell on Windows.
- 46:23You can obviously do it in your RC files on Mac and Linux.
- 46:26So, for example, I have vnv, which is for venv, but I don't want it to accidentally change the directory.
- 46:32And if you haven't, like whatever.
- 46:33Like I just have a short version, so I know it's something slightly different.
- 46:36But it'll go run the virtual environment.
- 46:38And it'll always remember to add --prompt equals dot, which will make the virtual environment not just be called venv,
- 46:45but it'll be called the name of the project that it's in.
- 46:48So, for example, if it's in like talk Python, like the virtual environment, when I activate it, we'll say it's the virtual environment talk Python.
- 46:54Right.
- 46:55And then it activates it.
- 46:56And then I think I think this is almost a bug in Python that most of the time I would say like 49 weeks out of the year.
- 47:05When I create a virtual environment, the first thing I hear is warning your pip is out of date.
- 47:10You know, like, OK, well, I'm starting a new project.
- 47:13How about we start with a new one?
- 47:14Let's just go with that right now.
- 47:15Right.
- 47:15And so my just that is I always want the new one.
- 47:19I don't ever want the old one because this is brand new.
- 47:22So I want the new one.
- 47:23Why don't we fix that?
- 47:24So my alias then says pip install dash you pip setup tools, wheels, pip-tools, et cetera.
- 47:30And just ditches that output.
- 47:31So like, yeah, right.
- 47:32Immediately, like I have when I create a virtual environment, it's immediately active and it's immediately named the project and it's immediately up to date.
- 47:39So I don't see those silly warnings, at least for a while.
- 47:41And all I got to remember is type V and V.
- 47:44Boom.
- 47:44And that's it.
- 47:45Right.
- 47:45So if you get into this, if you do a few little steps.
- 47:47I do this too.
- 47:48Yeah.
- 47:48Yeah.
- 47:49Yeah.
- 47:49I imagine you did.
- 47:50And so it's I just strongly encourage people to somehow either do this in PowerShell or do this in their terminal setup because like that big, long thing to type, that's a hassle.
- 48:00Three letters is not a hassle.
- 48:02And they just makes it.
- 48:03There's something about lowering the friction that makes it really nice.
- 48:06And there's tools you can also install that do it.
- 48:08But like I recommend just writing your own aliases too, because everyone's got a sub of a different workflow instead of perfect anyway.
- 48:14That's why Vim has so many options.
- 48:16So writing this yourself, it's super easy and it teaches you a bit about how it's working so that you're not surprised at the results.
- 48:24Yeah.
- 48:25Yeah.
- 48:25Indeed.
- 48:26Okay.
- 48:27Now packaging.
- 48:27Yes.
- 48:29And I'm going to keep this one super short because it's a deep, deep, deep, deep, deep, deep, deep rabbit hole.
- 48:35Figure out your packaging day one.
- 48:37Like you get, I will figure out packaging before I even figure out what I'm, you know, how I'm building stuff.
- 48:42I am just going to set up a basic hello world and then I'm going to get the packaging working.
- 48:48Because I actually, and I learned this the hard way.
- 48:51I built a project once.
- 48:52It took me one month.
- 48:54I was very happy with it.
- 48:55It was gorgeous.
- 48:56It was a game.
- 48:56You can actually find it online now.
- 48:58I was very happy with myself.
- 49:00And then I spent the next two years trying to package the dark bag.
- 49:02Turned out it was impossible to package because of certain assumptions that one of the libraries I was using made.
- 49:08Had I actually tried to package it from the beginning, I could have saved myself a heck of a lot of time and just use a different library.
- 49:12Yeah.
- 49:13So you figure it out from day one.
- 49:16And then the benefit you get to this is that then you are constantly, especially if you're doing out of place testing, which I strongly recommend.
- 49:23Run your tests.
- 49:24Your tests should not be in your source folder.
- 49:26Put them separate.
- 49:27That's going to force you to actually install your package into a virtual environment and then run the test against that package.
- 49:34It is not as scary as it sounds.
- 49:35I have a whole, by the way, if you go to my website, code mouse92.com, I've got a whole talk on this called Escape in the Gargo Cult.
- 49:41And I explain how these patterns work.
- 49:43But when you do it that way, that means from day one and ever onward, you are testing your packaging.
- 49:48You're testing your assumptions about the working directories and all that.
- 49:53And you are testing it in the same way that it's going to be used on someone else's computer.
- 49:58So you never again have to have the thing of, well, it works on my machine because you are, all of your tests are depending on actually installing your package.
- 50:08So if something breaks, you bring in a dependency that just doesn't work with your packaging scheme, you know, right away.
- 50:14And you can make a decision about it.
- 50:15Yeah, that's good advice.
- 50:16There's a lot of depth to the packaging thing.
- 50:18I had a whole panel on it.
- 50:19We're in Talk Python not long ago.
- 50:21Definitely a lot there.
- 50:22Oh, yeah.
- 50:23Deep rabbit hole.
- 50:24But you know what?
- 50:24Don't copy it.
- 50:25Don't don't copy pasta.
- 50:27Don't copy paste.
- 50:27Find out what the things you're using are doing because.
- 50:31All right.
- 50:33Last one.
- 50:34Yeah.
- 50:35Currency.
- 50:35Currency.
- 50:36Yes.
- 50:37Which for years I heard concurrency, asynchrony, threading, parallelism, multiprocessing.
- 50:42I heard those all used as as synonyms for one another.
- 50:46They're not completely different things.
- 50:50So and actually I have to thank the Go language for that for for teaching me this.
- 50:55Thank you, Rob Pike, for his talk.
- 50:57Concurrency and parallelism are not the same thing.
- 51:00But here's the thing to understand.
- 51:01Concurrency is doing.
- 51:03You're switching between tasks.
- 51:04You're checking your phone and you're talking to your friend.
- 51:07Are you doing both at once?
- 51:08No, you're not reading stuff on Twitter and listening to what your friend says.
- 51:13You're actually switching between the two contexts rapidly.
- 51:16You're not really multitasking.
- 51:18You're just changing your focus fast enough that no one ever notices the difference.
- 51:21That's concurrency.
- 51:23So you have one CPU or potentially potentially multiple or involved in this where people get
- 51:28confused.
- 51:29Python's nice because you're locked to one CPU for concurrency.
- 51:32So it's I'm gonna work on this.
- 51:35Then I'll work on this.
- 51:35Then I'll work on this.
- 51:36Then I'll work on this.
- 51:37It doesn't make your code faster.
- 51:38It'll never make your code faster.
- 51:39Just like multitasking will not make you get your work day faster.
- 51:42What it's going to do is it's going to slow you down.
- 51:44But it doesn't look like it.
- 51:48In some cases, it'll make your code look faster because while you're waiting on the user to
- 51:52type in their answer, this example I use in the book, while you're waiting on the user
- 51:55to type in their answer to a question, you're doing some math in the background.
- 51:59You're IO blocked.
- 52:00You're waiting on something over which you have no control.
- 52:02You can do some other work while you wait.
- 52:04That's concurrency.
- 52:05And you can do that with a synchrony where Python's handling that multitasking, or you can do
- 52:10it with threading where the operating system is handling that multitasking.
- 52:13That's different than parallelism, which is where you're actually using different CPU cores and you're
- 52:19doing two things at the same time because you have two different CPUs, two different cores
- 52:24working in parallel.
- 52:26And they're not depending on one another.
- 52:28They're not waiting.
- 52:29So you're doing multiple tasks at once.
- 52:32Both of them have overhead.
- 52:33Near one is a silver bullet for performance.
- 52:35You're really going to have to think a lot about it.
- 52:37But I actually like the GIL for this reason.
- 52:39Global interpreter lock, the much aligned thing that stops us from getting free multiprocess,
- 52:45free parallelism, free multiprocessing with every thread.
- 52:48Because this forces us to actually stop and think, which thing do I need?
- 52:53Do I need concurrency or do I need parallelism?
- 52:56Because you can get both in Python, but the GIL makes sure that you have to make a decision.
- 53:01Because if you want multiprocessing, you're going to have to create different processes,
- 53:05spin them off, manage them separately.
- 53:07There's more overhead, but you're going to have to address all of those things of how do
- 53:12we share resources, which is a really snarly topic.
- 53:14And Python doesn't let you pretend that that's not going to be an issue.
- 53:17It makes you think about that.
- 53:19It does make you think about it.
- 53:20While you can't get more done, you can't make an individual thing go faster by using concurrency
- 53:26on it.
- 53:26You can start a bunch of things that run outside of your control and outside of your program
- 53:31and then get them back, right?
- 53:33Like I could issue a web request, a three API endpoints, all of them, and then start gathering
- 53:38up the answers as the first one comes back, right?
- 53:39Like you have to think about, I think the main thing to think about here is where am I waiting
- 53:45on something else when you're looking for the async and await type of benefits?
- 53:50Like am I waiting on a thing?
- 53:51Could I be doing something while I'm waiting?
- 53:54Async and await amount makes it super easy to do stuff while you're waiting.
- 53:58But your program itself is still constrained by the GIL.
- 54:01And often with async and await, there's not even a second thread involved, right?
- 54:06It's just slicing up, time slicing up the current thread.
- 54:09Yeah.
- 54:09Which I mean, technically is all that's going on with the threads anyway.
- 54:12Like when you have multiple threads, the operating system is now just doing the slicing up.
- 54:16You're still constrained to one process.
- 54:18Then you need a separate process if you want to do multiple things.
- 54:22Yeah.
- 54:22But like my machine has 10 cores.
- 54:23It will go across the cores according to the OS.
- 54:25But the effect in Python because of the GIL means it doesn't actually make any difference.
- 54:30It's still the same, right?
- 54:31Right.
- 54:31But if you get multi-processing them for sure.
- 54:33So yeah, super, super interesting.
- 54:34I think one of the things people tell, I think people say two things.
- 54:38One, asynchronous programming is so hard you should never try it.
- 54:42I hear that all the time.
- 54:43It's so complicated.
- 54:44It's so hard.
- 54:45You're going to end up with race conditions.
- 54:46Just don't try it.
- 54:47Mere mortals don't dare.
- 54:50They only regret.
- 54:51Maybe if you're doing like signaled events in C++ across, you know, hardware notification.
- 55:00Like there are scenarios that are super, super tricky.
- 55:03Using async and await generally don't fall into that.
- 55:06Like you write regular code without changing the structure.
- 55:10You don't have to have callbacks and signals and locks.
- 55:12You just write regular code where you have the async stuff you put away.
- 55:15It's kind of the end of the story.
- 55:17And it's not necessarily as simple as straight up regular programming, but it's, you know,
- 55:20it's like 10% more.
- 55:22It's not 10x more.
- 55:23Yeah.
- 55:23I think a lot of people for some, I don't understand why, but a lot of people, I think
- 55:27they want to feel like I've conquered this dragon.
- 55:31And if I'm going to tell other people how to do it or what to do, the first thing I'm
- 55:36going to tell them is how hard it is and how daunting it is.
- 55:39And like, yet I've conquered it and now I will help you.
- 55:41Right.
- 55:41That doesn't do people a favor.
- 55:43Right.
- 55:43They want to feel good about themselves.
- 55:45Yeah.
- 55:45I guess so.
- 55:46But I feel like threading and parallelism lands deeply in that space where people are like,
- 55:51oh, it's super hard.
- 55:53And it's not, it really isn't.
- 55:55But where it's hard is you, you have to be prepared to really think about why do I need
- 55:59it?
- 55:59And if I can briefly say, I have two entire chapters in this book on that topic.
- 56:05Yeah.
- 56:05Cool.
- 56:05And I pick one example and it has nothing to do with waiting for a webpage to download
- 56:09because that is both overused and something that most of us don't need.
- 56:12And I apply synchrony.
- 56:14I apply threading.
- 56:14I apply parallelism.
- 56:16And you actually see all those little concerns of how do you handle deadlocking, live locking?
- 56:22How do you handle starvation?
- 56:24How do you handle the producer consumer problem?
- 56:26How do you handle the fact that, huh, I just added 15 multi, multi processing workers.
- 56:31And now this thing is like way slower.
- 56:33Why?
- 56:33I go into all of that.
- 56:35And I think that's one thing I wanted to say is when I was writing this book, that was the
- 56:40thing that was my chief delight was coming up with examples for all of the topics from the,
- 56:44from the simplest, all the way to the most complicated, where I did not hold back on the
- 56:48relevant complexity.
- 56:49My list comprehensions were the starliest list comprehensions you're ever going to find in
- 56:54this tutorial.
- 56:54You know, my, you know, I have, I go into recursion.
- 56:57I go into, yes, I go into meta classes, whichever one's afraid of.
- 57:01I go into all of these things.
- 57:02I dig into them, but the complexity is always relevant.
- 57:06So you're only going to get the complexity from the topic and you're going to understand
- 57:09where all these bits and pieces interplay.
- 57:11But the, in the complexity coming in the old, from other irrelevant topics, I try to
- 57:17keep to a minimum.
- 57:18So you can really focus on understanding, not just the happy path of coding, not just the
- 57:23happy path of like, okay, well you can async this.
- 57:25Look how easy that was.
- 57:26Well, it's not easy, but it's also not painfully hard.
- 57:30You just have to be prepared to think through those different factors.
- 57:35And so I focused a lot of time on really helping you learn how to think through those factors
- 57:39as it relates to your code.
- 57:41Excellent.
- 57:41It sounds like a bit of the Einstein philosophy.
- 57:43Simple as possible, but not simpler.
- 57:45Right.
- 57:45Exactly.
- 57:46Yeah.
- 57:46Cool.
- 57:47I, you know, last thoughts on the async stuff.
- 57:49Like I think a sprinkling of async and await makes massive difference for a lot of scenarios.
- 57:53And that's pretty straightforward.
- 57:54If you're going to try to write trio or some full on framework that deeply coordinates and
- 57:59understands it.
- 57:59Like, yeah, that's super, super hard.
- 58:02Again, some of the signaling stories I talked about with like C code, that's super hard, but
- 58:07this stuff people, it's pretty approachable.
- 58:09So don't, don't forget to check it out.
- 58:10All right.
- 58:11Well, I think that's it for our things.
- 58:14We wish we knew if we had more time, we could wish upon more things, but that's pretty much
- 58:19definitely covers it.
- 58:20Doesn't it?
- 58:20Yeah, I think so.
- 58:21Nice.
- 58:22All right.
- 58:22Well, before you get out of here, let me ask you the final two questions.
- 58:25If you're going to write some code, what editor to use?
- 58:28Yeah.
- 58:29You know, for the longest time I was a VS Code guy.
- 58:31I still love it, but I broke down and I bought PyCharm Professional and I love that because
- 58:36I do a lot of refactoring and PyCharm saves my butt so much when it comes to refactoring
- 58:41that I fell in love with it.
- 58:43And I can't imagine working without it now.
- 58:44I hear you.
- 58:45So I'm spoiled.
- 58:48It does surprise me how people, not just a PyCharm comment, but in general, there's
- 58:53a lot of people who are like, oh, I don't really want to get like a new computer or I don't
- 58:57really want to pay 20 bucks for this thing.
- 58:59That would save you thousands of dollars worth of time as you go for it.
- 59:04You know, like, so I'm glad.
- 59:06Large on your tools.
- 59:07Yeah.
- 59:08As far as your budget will allow you.
- 59:10And I've been on both ends of that spectrum.
- 59:12And as your budget allows you, large on your tools because they're what you're going to
- 59:15be spending most of the day using anyway.
- 59:18And the last thing you want is something you're going to be getting splinters on.
- 59:21So indeed.
- 59:22And then notable PyPI package, something you're like, oh, this thing's awesome.
- 59:26I just ran across X, Y, Z.
- 59:28I guess I have two.
- 59:30One would be, I think my favorite, and this has certainly gotten plenty of press, but I
- 59:34love it.
- 59:34It's the hypothesis testing library.
- 59:36I love hypothesis.
- 59:37Hypothesis is fantastic.
- 59:39It will revolutionize the way you write tests.
- 59:41It's not as scary as the documentation makes it look.
- 59:44If you click on what you can generate and how, start there.
- 59:47I go through the quick start guide and then go right to this.
- 59:49And it's so cool because you just write these strategies.
- 59:52I'm like, okay, this is what my data is kind of sort of supposed to look like.
- 59:56And then your test is going to try and break your code.
- 59:59I love hypothesis.
- 1:00:01It's going to throw all sorts of garbage at you until it finds a way to break your code.
- 1:00:04And then it's going to go, hey, here's the simplest thing I could find that blew up
- 1:00:07your code.
- 1:00:08Fix it.
- 1:00:08Lovely.
- 1:00:09Nice.
- 1:00:09So much fun.
- 1:00:10The other favorite package I have is one of my own actually branch detective.
- 1:00:15I built this at two jobs ago.
- 1:00:17And basically we had the problem where we had these diverging branches where we had no
- 1:00:23choice, choice, but to completely diverge them.
- 1:00:24And we were cherry picking back and forth.
- 1:00:26And there was always this, which commits are in this one that aren't in this one.
- 1:00:30When you're doing cherry picking, your hashes are complete garbage.
- 1:00:33So I built this tool that allows you to see what's in one branch.
- 1:00:37It's not in another branch, any branch arbitrarily.
- 1:00:40You can specify day range.
- 1:00:42It knows how to handle cherry picks.
- 1:00:43It knows how to handle merge revisions or filter them out.
- 1:00:46And you can even pass to the dash M flag and just get this nice markdown.
- 1:00:51If you're using conventional commits, we'll just spit out this markdown.
- 1:00:53Then you can then copy and paste into your PR and say, hey, here's, here's all.
- 1:00:57Here's the summaries of the 26 commits I'm putting in here.
- 1:01:00So I use this every single day and I've improved it for every project I'm on so far.
- 1:01:06And I just, I love it.
- 1:01:09It's my favorite.
- 1:01:09One of my favorite things I've ever built.
- 1:01:10So you kind of wrote set for Git, right?
- 1:01:13Like you can say, what's in the set?
- 1:01:14Subtract that set.
- 1:01:15Show me those things.
- 1:01:16Yeah.
- 1:01:16Speaking of data structures.
- 1:01:18Exactly.
- 1:01:18Like what's here that's not there.
- 1:01:19Yes.
- 1:01:20Yeah.
- 1:01:20Cool.
- 1:01:21All right, Jason.
- 1:01:21Well, thanks for being here.
- 1:01:23Final call to action.
- 1:01:24People will get their Python better.
- 1:01:26What do you tell them?
- 1:01:27Yes.
- 1:01:27So dead simple Python is available from wherever you buy books, but especially if you go to
- 1:01:33nostarch.com, you can get this comes in ebook or in physical book form.
- 1:01:38And Hey, you know what?
- 1:01:39I wrote it and I refer to it every single day because this is the book I wish I had had.
- 1:01:43And my favorite little feature of this is that I have a thing in the back with every single
- 1:01:49command for the debugger.
- 1:01:50Also, every single dunder method is also documented in here, which even the Python docs didn't
- 1:01:55do that.
- 1:01:55This is the entire core language bar nothing explained for the existing coder.
- 1:02:00So check this out.
- 1:02:01Awesome.
- 1:02:02Well, thanks for taking the time for being here and enjoy the chat.
- 1:02:05Thank you so much.
- 1:02:06It's been a pleasure.
- 1:02:07You bet.
- 1:02:08See you later.
- 1:02:09This has been another episode of Talk Python To Me.
- 1:02:12Thank you to our sponsors.
- 1:02:14Be sure to check out what they're offering.
- 1:02:15It really helps support the show.
- 1:02:17Take some stress out of your life.
- 1:02:19Get notified immediately about errors and performance issues in your web or mobile applications with
- 1:02:24Sentry.
- 1:02:25Just visit talkpython.fm/sentry and get started for free.
- 1:02:30And be sure to use the promo code talkpython, all one word.
- 1:02:33Want to level up your Python?
- 1:02:35We have one of the largest catalogs of Python video courses over at Talk Python.
- 1:02:39Our content ranges from true beginners to deeply advanced topics like memory and async.
- 1:02:44And best of all, there's not a subscription in sight.
- 1:02:47Check it out for yourself at training.talkpython.fm.
- 1:02:50Be sure to subscribe to the show.
- 1:02:51Open your favorite podcast app and search for Python.
- 1:02:54We should be right at the top.
- 1:02:55You can also find the iTunes feed at /itunes, the Google Play feed at /play,
- 1:03:01and the direct RSS feed at /rss on talkpython.fm.
- 1:03:05We're live streaming most of our recordings these days.
- 1:03:08If you want to be part of the show and have your comments featured on the air,
- 1:03:12be sure to subscribe to our YouTube channel at talkpython.fm/youtube.
- 1:03:16This is your host, Michael Kennedy.
- 1:03:18Thanks so much for listening.
- 1:03:19I really appreciate it.
- 1:03:20Now get out there and write some Python code.
- 1:03:22I'll see you next time.