Brought to you by Michael and Brian - take a Talk Python course or get Brian's pytest book


Transcript #330: Your data, validated 5x-50x faster, coming soon

Return to episode page view on github
Recorded on Tuesday, Apr 4, 2023.

00:00 Hello and welcome to Python Bytes, where we deliver Python news and headlines directly to your earbuds.

00:05 This is episode 330, recorded April 4th, 2023.

00:10 I'm Michael Kennedy.

00:11 And I'm Brian Okken.

00:12 And you can connect with us over on Fostadon.

00:15 If you're on Mastodon, find us there.

00:18 I'm at mkennedy at Fostadon.org.

00:20 Brian's Brian Okken over there.

00:22 And the show is at Python Bytes at Fostadon.org.

00:25 And if you're interested in the video version, check out pythonbytes.fm/live.

00:31 Click that.

00:31 Go over to the YouTube channel.

00:32 Subscribe.

00:33 Get notified.

00:33 You'll get a little pop-up when we start streaming live.

00:37 It's always fun to be part of it.

00:39 We encourage people to check the show out that way as well.

00:41 So, Brian, let's start off with something that has been almost exactly one year in the works.

00:47 I was just going to ask you about that.

00:50 So, was this about a year ago we talked about this, Pydantic?

00:55 I think I saw something on Twitter, of all places, from Samuel Colvin saying it was April 4th, 2022, that I started working on Pydantic version 2.

01:09 So, that sounds like to the day.

01:11 Okay.

01:11 Well, it's not completely here yet, but it's here enough to try.

01:16 So, I'm pretty excited about it.

01:18 So, Pydantic V2 version 2 pre-release.

01:22 So, people can, it's available now.

01:24 So, people can install it.

01:26 You have to do the, like the install --pre Pydantic.

01:32 And then you can say Pydantic greater than or equal to 2.0 A1, I guess, if you want to get the alpha one or better.

01:39 So, the big news is alpha one's available.

01:41 And it's pretty exciting.

01:44 There's a whole bunch of great stuff changes.

01:46 We've talked, I think we talked about it.

01:48 You talked about it on your show, I think also.

01:50 Yeah.

01:50 Yeah.

01:51 But the headlines here is, well, one, it's not complete yet.

01:57 This is the alpha.

01:58 We're not even debated yet.

01:59 So, if you see, if you try it out and you see something, they've got a GitHub, created a GitHub issue.

02:05 And they want to have people use the bug V2 label to create issues around the version 2.

02:13 Because they want to hop on those right away.

02:15 Anyway, so, the big change was, one of the big changes was to move a lot of Pydantic and the rules and everything into a different module called Pydantic Core.

02:28 That one's mostly written in Rust.

02:30 And so, it's like 5 to 50 times faster overall for performance.

02:37 So, that's pretty exciting.

02:39 I mean, this is, when you're using Pydantic, it's hitting for every interaction, right?

02:44 So, as fast as possible is great.

02:46 And I do like the idea of the, like, the separating the Rust part out into a different module, a different package, Pydantic Core, so that they can have, kind of maintain it and have safety and maintenance around that a little separate.

03:01 I think that makes sense.

03:03 Yeah.

03:03 And people used to have to create their derived classes and put a lot of their customization and their, you know, what's called root level validators and things like that.

03:13 Where it's like, I want to validate the whole class, not just a certain field.

03:16 Or, you know, if this is set, then that has to be set that way.

03:18 Like, a lot of those things had to be done in an OOP way.

03:22 And I think with the Pydantic Core, you have more direct access to, like, a layer below.

03:27 So, it's not just faster, which is fantastic, but it also opens up, like, more ways to interact with Pydantic, which is cool.

03:34 Yeah.

03:34 Yeah.

03:34 So, they've got a lot of stuff working already.

03:37 They want people to be able to experiment and try out their base model changes.

03:43 A lot of the same features for validation, but there's new method names, and it is a change.

03:50 Data classes, serialization, strict mode, different schemas.

03:54 Lots of changes for V2.

03:56 So, I'd like people to try it out.

03:58 There is a lot of stuff still under construction, mostly documentation.

04:05 And some of the base settings have changed from there were base settings, and now they're going to be in Pydantic settings.

04:12 That's not quite ready.

04:13 So, there's still some work to do, even in the migration guide.

04:18 So, they've gotten a start on the migration guide, but it's not there.

04:21 As you see on, like, some of the links, there's changes to data classes, changes to base model.

04:27 Some of the stuff's already there, but it's still under construction.

04:31 So, pretty exciting.

04:33 I'm definitely excited, and the 5 to 50 times faster, that's no joke.

04:39 There's, you might think, okay, well, what are you doing?

04:41 I'm, like, parsing, like, a settings file, or I got a single JSON document, whatever.

04:44 All of FastAPI is deeply, not all, but much of FastAPI is deeply based on exchanging rich data with Pydantic, right?

04:54 So, your API layer could get much faster, right?

04:58 And you can also use this, people maybe don't realize it.

05:02 You can use this with other frameworks as well.

05:04 You could use it with Flask.

05:05 You could use it with Pyramid.

05:07 You know, FastAPI is cool, because you can put just, there's a argument of type Pydantic model, and it automatically fills it all in.

05:14 But all you've got to do is just take, here's the post dictionary, and feed it to a Pydantic model.

05:18 Like, just inside the function is the first line, and you're in the same place.

05:22 So, you can use this across all these areas.

05:24 Then, for example, Pythonbytes.fm is powered by Beanie, which is Pydantic plus MongoDB plus Async, which is awesome.

05:31 But every single database record comes back, goes through Pydantic.

05:36 And if you're using something like Beanie or SQL model, in FastAPI, your data layer goes through Pydantic, and your web layer goes through, there's like multi-layered Pydantic operations on every interaction.

05:48 And so, making that part five to 50 times faster is just huge, right?

05:53 That's a really big surface area to make a lot faster.

05:56 I got speedups as well to talk about later in the show, but it's not that much area.

06:00 That's awesome.

06:01 One of the things you bring up, which is interesting, is that a lot of people, I mean, there's tons of people that use Pydantic just by itself with their own code.

06:10 But the people that mostly touch it through FastAPI or Beanie or something, they may have to wait until those projects bring on the changes then,

06:21 unless those projects have branches for B2, which who knows?

06:24 I, yeah, yeah, I hope so.

06:26 And maybe there's a way, you know, I think the breaking changes in, say, base model, for example, are, I think they're kind of deprecated already.

06:35 And 1.10.7, just they're still there, right?

06:40 But I think they become breaking changes here.

06:42 If you're also, if you're doing model validation, a lot of the function names gets changed.

06:50 Yeah, like things like from RRM go away.

06:53 There's a bunch of little, I don't think they're big changes that are going to be a huge problem for people, but they are incompatibilities, as you point out.

06:59 Both Roman Wright and Sebastian Ramirez seem to be really on top of their projects, respectively, Beanie and FastAPI.

07:06 So I feel like by the time this becomes fully released, they'll be there.

07:11 Yeah. And what's one of the reasons why I covered is to try to like promote everybody.

07:14 Nudge, nudge. Hey, exactly.

07:15 Nudge, nudge.

07:16 Exactly.

07:18 Please.

07:19 So, yes, indeed.

07:21 Yeah, it would be awesome that when this comes out for just, boom, V2 is out, like you just update all the packages that depend upon it and you can adopt it right away.

07:30 That would be great.

07:30 But to be fair, like let's say I'm working on a side project that's using FastAPI or Beanie or something and I don't have it in production yet.

07:39 I'd be like, yeah, let's use V2 right away.

07:42 But if I've got a production system, I don't want to switch right away.

07:46 So I get that there's projects at different levels and group projects like FastAPI and Beanie have to keep that in mind.

07:54 So, yeah.

07:55 All right.

07:56 All right.

07:56 Well, this is exciting to me.

07:58 I know this has been coming for a long time.

08:00 So excellent work.

08:01 What you got for us?

08:02 Well, that's a quick real time follow up.

08:05 I interviewed Samuel Colvin.

08:07 He loves to do stuff on the fourth of months, apparently, on August 4th as well.

08:12 Last year called PyTanic V2, The Plan.

08:15 So people can check that out if they're interested.

08:17 But let's talk about something really small, okay?

08:20 Okay.

08:21 From a friend of the show, Miguel Grinberg.

08:23 And he created this thing called MicroDot.

08:26 MicroDot, it's very small.

08:28 It's bigger even than like the semi-dot or the regular dot.

08:33 Very small.

08:33 No, so what this is.

08:36 Yeah.

08:37 So back to web frameworks.

08:39 This is the impossibly small web framework for Python and MicroPython.

08:43 So it's, I believe it's reason for existence really is to basically bring something like Flask

08:51 to MicroPython and CircuitPython, which is cool.

08:54 However, it also runs under standard CPython, which opens up some interesting possibilities as well.

09:00 Cool.

09:00 So if we go down here.

09:03 So if you're familiar with the Flask API, you should be real familiar with MicroDot.

09:10 So app equals, instead of Flask, you say MicroDot.

09:13 Got a function.

09:13 It says, I want to do an app.route on it.

09:17 Or I don't know if he supports direct verb, HTTP verbs there, like app.get, app.post, like Flask

09:24 adopted recently, but app.route for sure.

09:26 And then one of the differences is you have to pass a request object into the functions

09:31 there.

09:32 Whereas Flask has this thread local ambient variation of this thing.

09:37 So you'll get like a 500 error if you try to, you know, just run this directly without adding

09:43 that request.

09:43 So it's easy to overlook.

09:44 But other than that, other than the fact that there's a request parameter to the views, basically

09:50 the same thing.

09:51 Okay.

09:52 So yeah, that's pretty interesting.

09:53 Now, there's a bunch of compromises that are made here because MicroPython doesn't support

10:00 Jinja.

10:00 It doesn't support Flask, all of these different things.

10:03 All right.

10:03 So there is a template language, but it's not Jinja, right?

10:09 So there's a bit of a migration if you were going to take this on, right?

10:13 So you can run it under CPython, but you can also run it under MicroPython.

10:18 There's the HTTP methods.

10:20 I think you see the old style.

10:21 No, no, there's an app.get.

10:24 You can do the old style where you pass the method names like get and post, or you can just

10:28 do an app.get.

10:29 But yeah, again, if you're familiar with Flask, the way you do routes, the way you pass data

10:34 into the functions, all those things are absolutely the same, which is pretty cool.

10:39 One thing you can do is you can return JSON responses, and you can even just return a dictionary,

10:45 which I don't think you can do in Flask.

10:46 Maybe you can, but I think you have to JSONify it.

10:50 I think you have to say Flask.jsonify.

10:51 So this is kind of an upgrade, I would say.

10:53 Okay.

10:53 So if you have a little tiny thing, like a little tiny thing like this, Brian.

10:59 Okay.

11:00 All right, here.

11:01 However big is that, you know, like not about the size of my hand, like the half the way

11:05 across the pole of my hand.

11:06 I got one of these little tiny MicroPython, CircuitPython things.

11:09 You can now put APIs on here, and you can put even really interesting things like it has support

11:15 for concurrency.

11:18 So Flask doesn't support directly having async and await, I don't believe.

11:22 Not fully, anyway.

11:23 You got to switch over to Quart to do that.

11:24 I think they partially support it, but not full async and await.

11:28 But you can use the MicroPython asyncio extension here and get APIs running with full async and

11:35 await concurrency support doing JSON or other things.

11:39 Maybe with Pydantic.

11:40 Not sure.

11:40 That's pretty cool.

11:41 Because a lot of the, I mean, that's just an easy, like throwing a REST API or some

11:46 sort of API on something to throw back JSON to, that would be really cool.

11:52 And you don't need, I mean, for applications like that, you don't need a lot of templating

11:55 around.

11:56 Yeah.

11:57 And so let's see, go to the core extensions here.

12:00 You can see there's actually a bunch of cool core extensions.

12:02 So got the async and await support.

12:04 So all you got to do is just, where I'm going to scroll, just write async def endpoint,

12:10 right?

12:11 Boom.

12:11 Off it goes.

12:12 Yeah.

12:12 Because you really want to async that hello world.

12:14 Yeah.

12:15 For hello world, not so much.

12:16 But if you're talking to, you know, files that's similar database or something.

12:19 Yeah.

12:19 You can use, what is that?

12:22 MicroTemplate.

12:23 It says U-Template, but I bet it's a micro.

12:25 I think it's pronounced template.

12:27 Template.

12:29 Yeah.

12:29 U-Template.

12:30 It's a lightweight and memory efficient template for Python, which it looks very, very

12:38 Jinja-like as well.

12:40 So that's a pretty straightforward thing, but it's not identical, right?

12:43 What else we got?

12:44 Ooh, this is Jinja, isn't it?

12:45 Oh, can you?

12:47 Oh, you.

12:49 But no, hold on.

12:51 It's CPython only.

12:52 Oh, okay.

12:53 Okay.

12:53 Got it.

12:54 So, because it's not supported in MicroPython.

12:56 Got it.

12:57 But if you're doing it on CPython, you can add those templates.

13:01 Yes.

13:01 This is actually why it's pretty interesting to me.

13:03 So you can do TLS, HTTPS support, right?

13:07 Which is pretty cool.

13:08 You have web sockets.

13:10 You have asynchronous web sockets.

13:11 You have cores, cross-origin, resource sharing settings.

13:15 And you can even deploy it.

13:17 So it comes with its own little web server, which you can run on your MicroPython daily,

13:23 right?

13:24 But if you were going to put this into a big data center on a huge rack of servers,

13:29 that might not be the best choice.

13:30 So you can run it on MicroWisgi, which is awesome.

13:33 You can run it on GUnicorn.

13:35 You can even run it on GUnicorn with UVicorn workers to get the awesome LibUV high-performance

13:45 async support, right?

13:46 So you can deploy it onto kind of the top-tier Python stuff, put Nginx in front of it and all

13:52 that.

13:52 It's pretty cool, right?

13:53 Yeah.

13:54 Now, one of the ways you run web apps on servers, especially in Python, because the threading

14:00 support is not as friction-free, I guess, you know, like the GIL and all that, is you'll

14:06 farm it out into multiple workers, right?

14:08 So I can't remember what we have for Python by it.

14:11 It's not too many.

14:12 So I can't remember what we have for Python.

14:14 So I can't remember what we have for Python.

14:16 So I can't remember what we have for Python.

14:18 So I can't remember what we have for Python.

14:20 So I can't remember what we have for Python.

14:22 It'll automatically send the request over to another one, right?

14:25 Now, usually there's maybe a couple of limits, but one of the limits you really want to consider

14:30 is I don't want to run the machine out of memory.

14:32 So I think the ones we use like 150 megs per worker process.

14:36 So at some point, they only create so many, and then the server's like, okay, I've had it.

14:41 But with this little thing that runs on MicroPython, you could scale the heck out of it.

14:45 You could have a ton of worker processes under MicroWSGI.

14:48 Oh, yeah.

14:49 Or under UVicorn, right?

14:52 And I actually did a little super simple test.

14:56 I wrote the Flask equivalent of Hello World, literally exactly the same code with the changes

15:01 I talked about.

15:02 And then the MicroPython version, sorry, the micro.version, and that one, both running on

15:09 CPython, nine megs for this framework, 25 megs for the Flask framework.

15:14 So maybe you can have twice as much processing horizontal scale with this thing, but deployed

15:22 to real servers.

15:22 So there might actually be some interesting advantages to having this really tight framework.

15:28 Like I want to run it in a bunch of Docker containers that are using the smallest amount of

15:32 memory.

15:32 I want to farm it out and say, no, I'll have 30 worker processes on the server, not five.

15:37 I don't know.

15:37 We'll see.

15:37 Yeah.

15:38 Yeah.

15:39 So Panful is pretty psyched about the memory saving out there as well.

15:43 So I think it's good.

15:44 Yeah.

15:45 And I mean, for lots of jobs where you don't need, if you don't need the power, don't use

15:49 the power.

15:49 So.

15:50 Yeah.

15:50 Yeah.

15:51 Pretty neat.

15:51 So well done, Miguel.

15:53 And yeah.

15:54 Now, Brian, let me take just a moment and tell everyone about our sponsor.

15:59 This episode of Python Bytes is brought to you by Influx Data, the makers of InfluxDB.

16:04 InfluxDB is a database purpose built for handling time series data at a massive scale for real

16:11 time analytics.

16:11 Developers can ingest, store, and analyze all types of time series data, metrics, events,

16:17 traces in a single platform.

16:20 So dear listener, let me ask you a question.

16:22 How would boundless cardinality and lightning fast SQL queries impact the way you develop

16:27 real time applications?

16:28 InfluxDB processes large time series data sets and provides low latency SQL queries, making

16:34 it a go-to choice for developers building real time applications and seeking crucial insights.

16:39 Optimized for developer efficiency, InfluxDB helps you create IoT, analytics, and cloud applications

16:46 using timestamped data rapidly and at scale.

16:49 It's designed to ingest billions of data points in real time with boundless cardinality.

16:54 InfluxDB streamlines building once and deploying across various products and environments from the

17:01 edge, on-premise, and to the cloud.

17:03 Try it for free at pythonbytes.fm/influxDB.

17:08 The links in your podcast show notes.

17:10 Thanks to Influx Data for supporting the show.

17:12 Over to you.

17:14 What's your next one?

17:15 I want to talk about GitHub Actions a bit.

17:17 So a lot of my workflows have moved over to GitHub Actions.

17:20 And so there's actually three projects that I wanted to talk about that I thought were neat.

17:26 And they're all kind of in the GitHub Action genre.

17:29 Whatcha got?

17:30 Whatcha got?

17:31 Whatcha got first?

17:32 Whatcha?

17:35 Watch GHA.

17:37 It comes from Ned Batchelder.

17:40 This is a, a, a, just a simple tool to, to watch your GitHub Action progress from a command line.

17:47 So, pretty, I think it's a command line thing.

17:50 Looks like command line.

17:51 and so it looks, so you can, it has like a little progress bar thing, progress dots that go green.

17:58 And then they start out gray and then they go white and green and stuff.

18:01 to see the, the different things you got like, you're running three, seven on Ubuntu.

18:06 By Pia.

18:08 So if you've got a big matrix, that's doing a lot of stuff, it's kind of hard to keep up with what, what all's going on.

18:14 So, this is kind of neat to watch just a little tool from Ned.

18:18 Thanks, Ned.

18:18 one of the other things I, I, I was thinking about, so I just, my talk at PyCascades was talking about, that you can share, you can just, you can share packages without actually ever building it.

18:33 because pip install will build your wheel for you if, if it's not built already, but you probably, you probably should test that.

18:40 and one of the ways you can test some of that building is with, with Enix, build and inspect Python package.

18:48 so this is a GitHub action that, will, it does a lot of stuff, but it does, it does a build to make sure the build works.

18:57 it also does, has a linter to, to lint the wheel contents.

19:02 it also uploads the, wheel and the source distribution as GitHub action artifacts.

19:08 So it actually does generate the wheel for you, as an artifact, which is kind of neat.

19:13 the, it also, one of the things that's, what's one of the things that's always a mystery to me is like the makes, making sure I have everything that I want in the S dist, the source distribution.

19:24 And, this, this will, lint that.

19:27 And also, well, I guess it doesn't lint the contents of the S dist, but it does print them out.

19:32 So, print does a tree of the S dist and the wheel in the output so that you, you don't have to download it to check it.

19:39 You can just look at it in your GitHub output.

19:41 Make sure all the files and resources you might need to send out come along.

19:45 Yeah.

19:45 and this, yeah.

19:48 I, I, I had recently made a change to a package and it took out the tests and I had somebody say, Oh, look, we, we want the tests back in.

19:57 So it's kind of nice.

19:58 so, let's, I guess that's, that's it with that.

20:03 It's kind of a neat, GitHub action thing, that you just put it, put it in.

20:08 It's one of those actions.

20:09 So you just like specify it and it just works.

20:11 It's nice.

20:12 the, the third thing I wanted to bring up was, py test, GitHub actions, annotate failures.

20:20 so this is a, one of the, just a nice extra thing that I hadn't heard about before.

20:26 py test it's under the py test dev, umbrella, but it, it is a, it's a pip install sort of thing.

20:33 And what it does is it makes sure that the, the, all the, the proper stuff gets output, so that you can have nice annotated failure.

20:42 Your asserts, if there's failures, it's annotated nicely and github actions.

20:46 That's it.

20:47 Just some fun GitHub action stuff.

20:49 Yeah.

20:50 Once you really start getting into CI, CD, it's, it's fun.

20:54 And you're just like, oh, and now that it's automated, we could do this.

20:56 So we could do that.

20:57 Yeah.

20:58 And then, but when they automate all the things and then when things go wrong, you're like, oh God, then we have to pull it down and check it again.

21:04 But having, so having some of these debug stuff and things up, up in the cloud, it's good.

21:10 Yeah.

21:10 Very handy.

21:10 Excellent.

21:12 All right.

21:12 Well, I have a PEP for us to discuss.

21:15 Okay.

21:16 709 inlined comprehensions.

21:19 Now, this is a debate that I seem to have only on YouTube.

21:24 if I'll do, like, I've done some videos about list comprehensions or other sort of design patterns.

21:31 You might involve comprehensions and people are like, oh, Michael, you said that a for loop is different than a list comprehension.

21:38 But look, it says for thing in collection.

21:41 And so they're the same.

21:42 And so I just don't know.

21:43 You just don't know what you're talking about.

21:45 Like, you know what?

21:46 Let's disassemble it.

21:47 Let's see what it does.

21:48 Is it the same disassembly?

21:49 No, it's completely different disassembly.

21:52 That means the implementation of list comprehensions are different.

21:54 I don't care if the word for appears in both of them.

21:56 They're not the same thing.

21:58 This PEP brings them, kind of tries to take the both, the best of both worlds, though.

22:04 And it says there are some things we do to make comprehensions work, but look like they're just right there in the same function or in line, even if you don't have a function.

22:14 But in fact, there's kind of this thing behind the scenes that's happening where we create a nested function that you never see, but the interpreter creates and then calls it.

22:23 And that's the interpreter.

22:25 OK.

22:25 The comprehension, rather.

22:27 So this PEP by Carl Meyer is basically saying we could get really good performance increases if we just change that implementation a little.

22:39 And the reason it's created as a nested function and not just some inline code is what if you have a variable X in your regular function and then you have X as a loop variable or as the item variable in your comprehension or things like that, right?

22:55 You want them to still be isolated.

22:56 So that's basically the idea here.

22:59 It says comprehensions are currently compiled as nested functions, which provides isolation of the comprehension's iteration variable, but is inefficient at runtime.

23:08 So PEP 709 proposes to inline list dictionary and set comprehensions into the code where they are defined and provide the expected isolation by looking at all the variables, creating a copy of them, running this in place.

23:24 And then if there was a variable for that loop variable, just put the old value back, right?

23:29 Kind of push and pop them there.

23:30 And the benefits here are up to two times as fast as comprehensions are today.

23:37 So then they said, this is translating to an 11% speed up in one sample benchmark derived from real world code that makes heavy use of comprehensions in the context of doing actual work.

23:48 That's pretty cool, right?

23:49 Yeah.

23:49 I believe comprehensions were in general slightly faster than four loops that would just do something and put it in a list.

23:56 So making it two times faster still is even better.

23:59 So if this gets adopted, it's in draft form right now.

24:02 I can go back to my YouTube comments and have even further nuanced discussions about like, here's yet again, how they are not the same thing, but they look similar.

24:11 So yeah.

24:14 I never would have thought that I should reuse a variable in a comprehension though.

24:20 I don't do that, but I guess.

24:22 No, I think it's like if, let's say you've got two list comprehensions, you know, X squared for X in first set.

24:31 Then X, two X plus one for X in other set.

24:36 Those are two separate list comprehensions.

24:38 You don't want like one of those variables too.

24:41 You want to keep them.

24:41 They want to be like, okay, this X is only for this comprehension.

24:45 That's what it's like.

24:46 So if you have an embedded comprehension, you might use X in both places.

24:50 Right.

24:50 Or if you have an X, X and Y equals something, and then you generate a comprehension, you say X in there.

24:55 Like there's a couple of, there's some weird.

24:56 Yeah.

24:57 Yeah.

24:57 I mean, I guess I was just thinking my own style.

25:00 The second one I would never do.

25:02 If I had, if I was already using X, I probably wouldn't use X in the comprehension.

25:06 But I'll often use I or X in a comprehension in embedded ones and don't even think about it.

25:13 So yeah.

25:14 Interesting.

25:14 Cool.

25:15 Yeah.

25:15 David Poole says, I'm sure there's good reasons for it, but I wonder why comprehensions don't use name mingling strategies for their var names.

25:22 Everyone's got to be named underscore underscore X.

25:25 That is a good question.

25:28 That reminds me of a joke.

25:29 Well, so what it's doing now is it basically says, we're going to create a function.

25:37 And so that variable is basically a local variable, that function, which has no influence on it.

25:42 Were you going to actually tell us the joke?

25:44 No, it's going to wait.

25:47 Okay.

25:47 All right.

25:48 Or should we do it now?

25:50 Oh, just, I think it was Ned Batchelor actually that mentioned that Dunder, we often talk about Dunder init instead of double underscore init.

26:01 But it's really underscore underscore init underscore underscore.

26:05 So it's really quunder.

26:06 Quunder.

26:08 And so I responded to him and said, I don't think so.

26:12 I think it's Dunder and Net Dunder is what it should be.

26:16 But that would be Redunderant.

26:18 It's pretty bad.

26:21 That's pretty much on par with the joke we got at the end.

26:23 So I'm preparing for people.

26:25 All right.

26:25 So basically the way to understand this, you can't look at the code and tell, right?

26:30 Which is why people incorrectly try to correct me on YouTube.

26:34 So you look at the code and it looks like, oh, it's just a for list and we took out the line breaks and put brackets.

26:40 So it's the same thing.

26:41 So if you look at it now, you can see if you create a function that creates a list comprehension, you'll see it creates what's called a code object of type list comprehension.

26:52 Then it calls make function.

26:54 Then it loads the list and then it does a bunch of stuff on it.

26:58 And then it actually, you can see there's like this sub function that gets disassembled and it says, we're going to build a list, load fast, iterate it.

27:04 So let's look at list append.

27:05 And what's really interesting, this is the part that differs from for loops.

27:08 There's a byte code called list underscore append.

27:12 If you do this with a for loop where you have a list and you call append, it loads the function append and then calls append on the operands.

27:19 But it's not in the runtime in a for loop.

27:22 In a comprehension, there's a special byte code that runs and that's like the primary difference.

27:26 So the benefit is list append is a byte code operation, not a function call.

27:34 But the drawback is there's this object created, there's a stack frame created, there's a function call over to this comprehension call.

27:40 There's an issue with all that stuff.

27:43 So the new one just says, what we're going to do is we're going to create a new op code called load fast and clear, which is like, I'm going to load the variable X.

27:52 And if there was one of those before, we're going to hang on to that just in case, you know, so we can put it back to avoid that.

28:00 And then it calls build list.

28:01 And like you can notice, there's no, there's no function call.

28:05 So no stack frame, no extra function call.

28:07 There's no list comprehension object, all those things.

28:10 And so this is the new byte code operation that manages that variable isolation.

28:16 And then you just do it directly, which saves you a bunch.

28:19 We talked about the 2x speed there.

28:21 So that's the, that's the, the pep.

28:23 People check it out.

28:24 See what you think.

28:25 That's really interesting, Michael, but you had me at it's faster.

28:28 I know exactly.

28:29 I just want people to kind of know what's, what's happening and why it might be faster and so on.

28:35 So pretty neat.

28:37 You can see it does have the, the only possible, I guess, concern or the reason they say, why is this even a pep?

28:45 Why is this not just, Hey, I made it faster.

28:46 Like, why do we need to discuss this?

28:48 Yeah.

28:49 Like you said, right?

28:49 Like it's faster.

28:50 We're done.

28:50 Let's go.

28:51 Is there are user observable changes?

28:54 If the user doesn't like themselves, basically.

28:58 For example, why would a user return locals as the item you want put into a list during a list comprehension?

29:07 Well, if you did do that, you would see that it's not the same as before.

29:11 I have no, no idea why you would ever try to do that, but that would technically would be a breaking change.

29:17 The other one was slightly more valid, perhaps is that if there's an exception inside the list comprehension, because it used to be in a separate function call.

29:27 It was, there's, it would show up in the actual traceback call stack, right?

29:33 But now you are not in another function.

29:35 It is on a line in the OG function.

29:38 So you, you don't have that.

29:41 Basically it's missing from there.

29:43 So you would have a slightly different traceback exception.

29:46 Well, traceback, the exception would be the same, but the traceback call stack listing would be different.

29:51 That potentially affects somebody, but not a lot.

29:55 I don't know.

29:56 It's a 2X, it's a trade-off.

29:57 I would totally think it's worth taking.

29:59 Yeah, but I, I see why that's a, it's a observable interface or an observable behavior change.

30:06 So yeah.

30:07 Yeah.

30:07 Yeah.

30:07 Although I learned from Brett Cannon just a couple of weeks ago that locals often has weird stuff in it.

30:14 If you look at locals a lot, sometimes there's stuff in there that you don't recognize.

30:18 Interesting.

30:19 Yeah.

30:20 The locals one seems like, you know what?

30:22 Don't do that.

30:24 But the traceback one, I can see, okay, we're always looking for this.

30:27 And like, if I get an error, I try to like, look at the traceback to figure out what to tell people.

30:32 Or I don't know.

30:33 Theoretically could have, it still seems unlikely.

30:36 I feel like you shouldn't depend upon what's listed there, but I'm sure somebody does somewhere.

30:41 Well, like IDE makers and things like that.

30:43 Yeah.

30:44 Yeah.

30:44 Cool.

30:45 All right.

30:45 Well, that's it for all of our items.

30:48 You got any extras?

30:49 I don't.

30:50 I don't.

30:51 Do you?

30:51 I thought I didn't have any extras, but I think by the time, I'm going to try to predict the future a little bit because I have some control over it.

30:58 So I do have an extra.

31:00 I'm going to be releasing either today or tomorrow.

31:03 And by the time this podcast comes out, this is going to be released.

31:06 But if you're watching it live, it's not yet released.

31:09 So I'm going to be releasing a new course, Python Web Apps That Fly with CDNs.

31:14 It's just over a three-hour course that's all about taking CDNs and applying them to Flask web apps and also hosting video content and large files and how do you geo-replicate that.

31:26 We use a lot of these techniques in Python Bytes to make the website faster as well as to deliver terabytes of MP3s to people.

31:36 So check that out.

31:38 I will put a link in the show notes.

31:40 Again, if you're listening live, this is not out yet, but it will be out by the time the MP3 hits your podcast player.

31:46 So if you have the audio-only version, go check it out.

31:48 Link's in the show notes.

31:49 Nice.

31:49 Yeah, I think that's a really, really cool course.

31:51 I think there's so much people can get out of it in terms of like it's really easy.

31:57 You know, 30 minutes and you're like, oh, our app is so much faster and we can use smaller servers.

32:01 That's really great.

32:02 Well, three hours plus 30 minutes.

32:05 Yes.

32:06 Well, once you know the thing, it's probably 30 minutes to apply it to your app.

32:09 That's what I mean.

32:10 Yeah.

32:10 David Poole says, the traceback one could be worked around if the debug compiled used the old function style method,

32:18 like aggressive optimizations and GCC with inline functions.

32:22 Okay.

32:22 Possibly.

32:23 Interesting.

32:23 You would have to have people buy into that, but right.

32:27 I mean, I'm sure, Brian, you're very well aware of the debug versus release builds and optimization levels and all that stuff in C, right?

32:36 No.

32:37 I mean, yes, but I don't use them.

32:38 No, you don't use that.

32:40 Well, I don't, I personally don't like to, I don't like to test in something my user isn't going to see.

32:46 So I always test in optimized released.

32:49 Got it.

32:50 Yeah.

32:50 Yeah.

32:50 But it can make a big, it can make a big difference.

32:52 But in the Python world, we don't really discuss that so much.

32:56 Right.

32:56 Yeah.

32:57 And except for the, the, the comment, the one thing to be aware of that I would, while we're, while we're on it, we probably haven't mentioned this lately is asserts are awesome in your test code, but they're not that great in your, it actually, they're, they're pretty great in your function code also, but just don't depend on, on it.

33:14 Because assert lines could be completely removed if you have the optimization on.

33:19 So.

33:19 Absolutely.

33:20 Yeah.

33:21 All right.

33:21 You ready for a joke?

33:22 Yes.

33:23 You fan of movies?

33:24 Like watching, watching movies and stuff?

33:25 Yeah.

33:26 I just went to a great movie.

33:27 Yeah.

33:27 So.

33:28 Nice.

33:28 Well, as a software person, especially if you do a lot with Linux or macOS, you might not be able to watch the movies too much.

33:34 This one says, I can't watch movies on my computer.

33:37 All it does is bash scripts.

33:38 Bash on the scripts of the movie.

33:41 Oh.

33:41 Not plot.

33:42 Or, or run shelf scripts.

33:45 I'm not sure which.

33:46 Okay.

33:47 That's funny.

33:48 I think.

33:48 Sort of.

33:49 Yeah.

33:49 Somewhat.

33:52 I have an incredible one that I want to put up, but it's like only video that has music and no, no spoken word.

34:01 So I don't think it fits for this format, but I'll, you know what?

34:03 I'll throw it in.

34:04 I'll throw it in.

34:04 People also check out the movie.

34:06 It's about releasing stuff to production.

34:07 So it's, it's pretty epic.

34:09 I'll put it in the list.

34:10 But the one we won't play is something that's like 30 seconds long that has nothing but music.

34:15 Cool.

34:17 Nice.

34:17 All right.

34:18 Is that all we got?

34:19 That's all we got.

34:20 It's a wrap.

34:21 It's a wrap.

34:22 Yeah.

34:22 Thanks as always.

34:23 Thank you.

34:24 See you later.

Back to show page