Latest / Talk Python To Me / #141: Python tricks
Transcript
- 0:00How many Python developers do you know that learned Python quickly, but then plateaued
- 0:04pretty quickly as well? Maybe this is someone you worked with, or maybe it's even you.
- 0:09Python's clean and simple syntax can mean it's easy to learn, but hard to master. After all,
- 0:14if you learned it in a week, what else is there to this language? How much more do you need to
- 0:18dig into it? Well, plenty. And Dan Bader is here to share his very popular Python tricks with us.
- 0:25You'll learn to look deeper for more than just the how, but the why and when of many of Python's
- 0:31more subtle features. This is Talk Python To Me, episode 141, recorded December 4th, 2017.
- 0:51Welcome to Talk Python To Me, a weekly podcast on Python, the language, the libraries, the ecosystem,
- 0:57and the personalities. This is your host, Michael Kennedy. Follow me on Twitter, where I'm at
- 1:02mkennedy. Keep up with the show and listen to past episodes at talkpython.fm, and follow the show on
- 1:08Twitter via at Talk Python. This episode is brought to you by Linode and Rollbar. Thank them both for
- 1:14supporting the show by checking out what they have to offer during their segments. Talk Python To Me is
- 1:19partially supported by our training courses. Python's async and parallel programming support is highly
- 1:25underrated. Have you shied away from the amazing new async and await keywords because you've heard
- 1:30it's way too complicated or that it's just not worth the effort? With the right workloads, a hundred times
- 1:35speed up is totally possible with minor changes to your code. But you do need to understand the internals,
- 1:41and that's why our course, Async Techniques and Examples in Python, show you how to write async code
- 1:47successfully as well as how it works. Get started with async and await today with our course at
- 1:53talkpython.fm/async. Hey, everyone. Before we get to the conversation with Dan about his Python
- 1:59tricks, I want to talk about Python bytes for a second. I know many of you who listen to Talk Python
- 2:05also listen to my other podcast, Python bytes, but many of you don't, and it's been a while since I've
- 2:11talked about it on the show. So if you're looking for almost the opposite of Talk Python, instead of
- 2:17long form, deep conversations on one topic, you want to just catch up on the news, think newsletter and audio
- 2:23forum for the Python space, head over to
- 2:25pythonbytes.fm and subscribe to the short
- 2:28news focused 15 minute weekly podcast
- 2:31I do with Brian Okken. I hope you guys check it out and I hope you're enjoying both
- 2:35of the podcasts. Now let's chat with Dan. Dan, welcome back
- 2:39to Talk Python. Hey, thanks, Mike. Thanks for having
- 2:41me on the show again. Yeah, I love to talk with you
- 2:43about technology and programming, so I'm really
- 2:45excited to do it once again.
- 2:47You were previously on the show
- 2:49for our panel with
- 2:51Anthony and Ronald about
- 2:53Contributed Open Source, and that was really a well-received
- 2:55episode. That was 132. Cool, yeah.
- 2:57That was a really fun experience, and
- 2:59I really like the panel format. You know, I
- 3:01listen to your show, and those are
- 3:03some of my favorite episodes when you bring on a bunch of
- 3:05people, and it's just, you know, the amount of information
- 3:07you can soak up so quickly. It's, yeah, it's just great.
- 3:10Awesome, thanks. The next one that I have scheduled,
- 3:13I don't know when it's actually going to air, like
- 3:15four or five episodes out, is
- 3:17a catch-up for
- 3:19Python and machine learning at the Large Hadron
- 3:21Collider and in particle physics,
- 3:23so that should be really fun. That sounds intense.
- 3:25It's going to be intense. That means lots of research
- 3:27for me, so that'll be awesome. All right,
- 3:29so let's talk about
- 3:31not contributing to Open Source so much.
- 3:33Let's talk about making our
- 3:35Python better with Python
- 3:37tricks. So that's what we're going to talk about this
- 3:39week. You know, I know you talked a little
- 3:41bit about it in 132, episode
- 3:43132, but it was the panel format,
- 3:45so you didn't get to go into it too much.
- 3:47So let's talk just quickly, kind of like
- 3:49how you got into programming and what you
- 3:51do day-to-day before we get into your tricks.
- 3:52Sounds good, yeah. So, well, I guess how I got
- 3:55into programming was when I
- 3:57finally managed to convince my parents to buy
- 3:59a used, old, dingy
- 4:01Commodore 64 for me.
- 4:03And, you know, that was the end of the
- 4:05home computing era, I guess, and those machines
- 4:07all booted into some kind of
- 4:09interpreter directly, so you wouldn't, you know,
- 4:11you don't get, like, a graphical desktop
- 4:13or anything. You just get a basic, the
- 4:15programming language.
- 4:16Yeah, that was crazy because you would,
- 4:17like, you couldn't just interact with the
- 4:19computer. You had to, like,
- 4:21converse with it to get it to do things,
- 4:23right? It was interesting.
- 4:24Exactly. It was, like, a glorified
- 4:26calculator and you just
- 4:28turned it on and you booted
- 4:29instantly and then you were just
- 4:31sitting there like, ah, okay, I'm
- 4:33getting this blue screen.
- 4:34What am I going to do now?
- 4:36And it really forced you to
- 4:38pick up some basic programming skills
- 4:41to do anything. You know, if you wanted
- 4:42to play a game, well, tough luck.
- 4:44You had to actually figure out, like,
- 4:46how to load the game from disk and
- 4:48then run it or, you know, actually
- 4:50type it in.
- 4:50Yeah, it was, like, a load command and
- 4:52stuff to actually do it, right?
- 4:53Exactly. Yeah, the load 8,1 or
- 4:56something. It's like the drive ID.
- 4:57And I think I'm kind of fortunate that
- 5:00I caught the tail end of that because
- 5:02it was already, like, the PC era and,
- 5:04like, Windows 3.11 or something.
- 5:07But because my parents were really,
- 5:09really against computers and technology
- 5:12in some way, the only thing I managed
- 5:14to convince him was to get this, you
- 5:15know, used old Commodore 64.
- 5:16And I got it from this guy and it came
- 5:18with, like, stacks of disks and
- 5:20notes this guy took.
- 5:22And so it was just a treasure trove for
- 5:25me.
- 5:25And, you know, it hooked up directly to
- 5:27your TV screen and it was just, yeah,
- 5:29just this amazing, like, miracle
- 5:31machine.
- 5:32And that's how I got into programming.
- 5:34Nice. And then how'd you get over to
- 5:35Python?
- 5:36Oh, yeah, that's a long,
- 5:36that's a long jump, a big leap there.
- 5:39Yeah.
- 5:39Right.
- 5:40I think that the basic programming and
- 5:43basic on the Commodore 64, I think it
- 5:44just kindled some, some kind of passion
- 5:46in me.
- 5:47And eventually I went on and got
- 5:49bachelor's and master's degree in
- 5:50computer science and just, you know,
- 5:52wanted to become a professional
- 5:54programmer.
- 5:54At some point in my university days, I
- 5:57think it was on a ski trip with some
- 5:59friends and one person, he brought a
- 6:02book, a Python programming book, one of
- 6:05these, like, learn Python in 48 hours.
- 6:07One of those that never really work out
- 6:08that way. And this was the first time
- 6:10I really.
- 6:11Was that the Sam's Learn Python on a
- 6:13Ski Vacation or Your Money Back
- 6:16Guaranteed book?
- 6:17Yeah, I should ask for my money back.
- 6:19Remember the Sam's book?
- 6:20It's awesome. So you had the book and
- 6:22you had some, like, kind of downtime
- 6:23after you're skiing to chill and flip
- 6:26through it?
- 6:26Yeah, it was a very geeky skiing trip.
- 6:28But this is when, the first time when
- 6:30I saw the actual language, you know,
- 6:31just learned a little bit more about
- 6:32it because my friend, he actually
- 6:34couldn't, hadn't used Python either,
- 6:36but it just seemed like an
- 6:37interesting new language or, you know,
- 6:39I guess it wasn't new at the time, but
- 6:40for us it was new.
- 6:41And so I just really fell in love with
- 6:44the way the language looked, you know,
- 6:46just seemed like the perfect blend of,
- 6:47well, this looks really appealing, you
- 6:50know, like almost like pseudo code and
- 6:51like in some way, like really poetic
- 6:53and nice, but also really, really
- 6:55powerful where it's not just your
- 6:58basic or basic programming language, but
- 7:00it's actually something where you can
- 7:02take it to places and you can build
- 7:04full-blown applications.
- 7:06And so, yeah, that's how it started.
- 7:07That's awesome.
- 7:07There's plenty of simple
- 7:09programming languages, but they often
- 7:11have like a ceiling where it's like,
- 7:13okay, you would build this and this
- 7:14with it, but you wouldn't really build
- 7:15anything bigger than that, right?
- 7:17Where it's pretty sweet that Python
- 7:19generally doesn't fall into that category.
- 7:21Yeah, totally.
- 7:21And I think it was you, you called it
- 7:23a full-spectrum language at some
- 7:25point or like on that interview that
- 7:27we did on my blog.
- 7:28And this is, I love this, you know, I
- 7:30bring this up all the time when
- 7:31people ask me about Python because
- 7:32yeah, it is really a full-spectrum
- 7:35language where someone can dive in and
- 7:36they can learn the basics of Python and
- 7:38know enough to be dangerous.
- 7:40And then you have giant, very, very
- 7:43complex systems built in this language
- 7:45and it can kind of scale that whole
- 7:47gamut.
- 7:47And I think that's just really cool.
- 7:49Yeah, it's amazing.
- 7:50What I really like about that spectrum is
- 7:52a lot of languages make you take the
- 7:55stuff you need for the advanced type of
- 7:58large applications.
- 7:59you got to use that syntax and that
- 8:01structure and those design patterns
- 8:02right from the start, like Java or C
- 8:05sharp static main void inside of a
- 8:07program class.
- 8:08You're like, whoa, I just want to like
- 8:09do a few things.
- 8:09Like what's all this about, right?
- 8:10Whereas Python, you like as you need the
- 8:12features, you layer them in, but they're
- 8:14not required until you need them, which
- 8:16is, I think, a part of the magic.
- 8:17That's a huge part of the appeal of
- 8:18Python that I see, you know, when people
- 8:20use Python or learn about Python, but
- 8:22it's also kind of the dirty little
- 8:24secret where it's very easy to kind of
- 8:26get stuck at that beginner level.
- 8:28And you're like, well, okay, so how
- 8:30would I actually go to the point where
- 8:31I could build, I don't know, something
- 8:33like the Instagram backend or even, you
- 8:36know, think about it.
- 8:37Yeah, absolutely.
- 8:37And that is one of the curses of its
- 8:40sort of success or its features is that
- 8:42it's a lot of people quickly get into
- 8:44it and they just get comfortable and
- 8:45they're just like, well, I just sort of,
- 8:47I knew C or I knew JavaScript.
- 8:48So now I know Python.
- 8:50I just don't do semicolons or curly
- 8:52braces, right?
- 8:53That's okay.
- 8:54And it works.
- 8:55But, you know, I think our topic today
- 8:57is really about understanding like where
- 9:01people get stuck and then going, okay,
- 9:03these are actually how you should go
- 9:05farther and do it correctly or
- 9:07Pythonically or whatever, right?
- 9:08Yeah, pretty, pretty much.
- 9:10I mean, this is, I feel like this is the
- 9:12biggest challenge in taking the next
- 9:14step with Python.
- 9:15Like how, you know, what does it even
- 9:16mean to write Pythonic code and how,
- 9:19how do you get there?
- 9:19because it's such an opaque concept and
- 9:22yeah, and whatever can be done there,
- 9:24I think it's going to make people's
- 9:25lives easier and it's going to make
- 9:26them more successful as developers.
- 9:28Yeah, absolutely.
- 9:28So let's talk about your book, Python
- 9:31Tricks.
- 9:32It's a buffet of awesome Python features,
- 9:35which is really nice.
- 9:36And you released it a little while ago
- 9:39in a digital only version on your site,
- 9:42debater.org, right?
- 9:43Right URL?
- 9:44That's right.
- 9:45Yeah.
- 9:45Yeah.
- 9:45And you also released it on Amazon and
- 9:49it kind of went a little bit crazy on
- 9:50Amazon.
- 9:51Well done, man.
- 9:52Thank you.
- 9:52How did it do?
- 9:53Like right now, if I'm looking here,
- 9:54it's number 21 and all the books in
- 9:58Python, like that's amazing for such a
- 10:00new book, but it was much higher, right?
- 10:03Yeah.
- 10:03So crazy enough, it actually hit the
- 10:06number one spot in Python programming
- 10:08and in programming languages in general.
- 10:12At some point it was even, it was book,
- 10:14it ranked number 250 in Amazon sales
- 10:18rank across all books on Amazon.
- 10:20And, you know, I actually, I took a
- 10:22screenshot of that and this is like.
- 10:24Printed out, put it on the wall in frame.
- 10:27This was like visible proof to my in-laws
- 10:29that I was just, you know, I was not,
- 10:31not unemployed, not just unemployed and
- 10:33addicted to the internet.
- 10:35It's like, here, I'm an author on the
- 10:38internet.
- 10:39This is what I've been doing in my
- 10:41office.
- 10:41I promise you I have a job.
- 10:43Yeah, it was a cool experience.
- 10:44That's really cool.
- 10:45Congratulations.
- 10:46Thank you.
- 10:46Yeah.
- 10:47Yeah.
- 10:47And so it's, it's still going strong.
- 10:49Like it's number 29 in web programming
- 10:51and stuff.
- 10:52So yeah, right now.
- 10:53Very cool.
- 10:54So we'll definitely link to your book
- 10:55from the show.
- 10:56So maybe give us the quick elevator
- 10:58pitch.
- 10:59I mean, we've kind of been building up
- 11:01to it, but what's the, what's the
- 11:02idea behind the book and what are you
- 11:03talking about in there?
- 11:04Well, like you said, the, the
- 11:05subtitle for a book is a buffet of
- 11:07awesome Python features.
- 11:08And so basically the book is a
- 11:10collection of actionable tips that
- 11:13will help you write clean,
- 11:15professional and developer style,
- 11:18quote unquote, Python code.
- 11:20And, you know, the feeling that I
- 11:22want to create for my readers is not
- 11:24like they're sitting through an
- 11:26advanced computer science lecture
- 11:27where I'm throwing a bunch of
- 11:28jargon at you, but more like we're
- 11:30on the same development team.
- 11:32You know, we're sitting down
- 11:33together, you know, maybe know the
- 11:34basics of Python.
- 11:35And I want to help you get up to
- 11:37speed as quickly as possible and
- 11:38help you grasp some of the
- 11:39intermediate and more advanced
- 11:41Python topics that often seem
- 11:43weird and opaque, you know, things
- 11:45like decorators, first class
- 11:46functions.
- 11:47What's the difference between an
- 11:48iterator and a generator and
- 11:50generator expressions and list
- 11:51comprehensions.
- 11:52And just some of the really useful
- 11:54things in Python that border on the
- 11:57arcane, but they're really not.
- 11:59If you, right.
- 12:01If you approach them from the right
- 12:03perspective.
- 12:03Sure.
- 12:03It's a lot of these things that,
- 12:05you know, I feel like Python is, you
- 12:07can learn the language really quick,
- 12:08but it takes a long time to really
- 12:10master it.
- 12:11And part of that mastery is like
- 12:12discovering these little trade-offs
- 12:14like I could use a tuple, but oh, by
- 12:17the way, did you know there's a name
- 12:18tuple?
- 12:19And did you know in Python three, six,
- 12:20there's a new version of a name
- 12:21tuple that is more flexible, but
- 12:23similar in performance and like, oh,
- 12:25wait, there's a new name tuple.
- 12:27What is that?
- 12:27Right.
- 12:28You don't really easily fall into
- 12:30that discovery or things like, yeah,
- 12:32you can use list as a queue, but it's
- 12:34like a thousand times slower than if
- 12:37you use this other proper thing for
- 12:39queuing.
- 12:39Oh, but don't use that for multi-thread
- 12:41and use this other one for, for
- 12:42parallelism and things like that.
- 12:44Right.
- 12:44Those types of things are somewhat
- 12:46arcane, but they're, they're not
- 12:48easily discoverable.
- 12:49I don't feel.
- 12:50Exactly.
- 12:51you know, where it's a lot of
- 12:52times like when someone has some
- 12:54experience with other programming
- 12:55languages and they're switching to
- 12:56Python, it can be really hard to
- 12:58understand how these different
- 12:59concepts map to Python, because I
- 13:01guess Python uses a very like human
- 13:04friendly naming style for a lot of
- 13:06things, you know, like you just
- 13:07mentioned the queue and list
- 13:08example, but if you're coming from,
- 13:11let's say a Java background, how
- 13:13does a list map to what I know from,
- 13:15from the Java world, you know, what,
- 13:17what kind of time complexity
- 13:19guarantees does it make and, and
- 13:20kind of, you know, touching on some
- 13:22of these things as well.
- 13:23Like how can you take some existing
- 13:25algorithm and actually bring it over
- 13:26to Python and to make sure, you
- 13:28know, it doesn't just look pretty,
- 13:29but it actually also really works
- 13:31and is fast enough.
- 13:32Yeah, absolutely.
- 13:33And one of the, you know, one of
- 13:34the things even more than like list
- 13:36versus say array list in Java is
- 13:38array, right?
- 13:40Like there's not really like the
- 13:42traditional C style array in Python
- 13:44in the built-in language sense.
- 13:46There's the, you know, array dot
- 13:48array class, but there's not the like,
- 13:50square bracket means something
- 13:52different than list, right?
- 13:54Like those are actually the same
- 13:55thing.
- 13:55And that just gets confusing, but I
- 13:57think we should probably just start
- 13:58going through some of your individual
- 14:00tricks that you thought are
- 14:01particularly like noteworthy in at
- 14:04least a radio format is.
- 14:06That'll be interesting.
- 14:07Yeah.
- 14:08It's so we'll try to not talk too
- 14:09much about code, but, you know,
- 14:11cover some of the ideas behind it.
- 14:13All right.
- 14:13So actually, you know, the first one
- 14:15you said, let's start with something
- 14:16simple.
- 14:16And I totally agree that it's a
- 14:18good place to start.
- 14:19You talk about comma placement for
- 14:21dictionaries and lists and just like
- 14:23structuring those for basically for both
- 14:28avoiding errors and making them source
- 14:29code control friendly.
- 14:31What's the story there?
- 14:32It started with sort of this being a
- 14:34pet peeve of mine.
- 14:34Like if you use lists or any of the other
- 14:38built-in collection classes like a
- 14:40dictionary or set, and you're trying to
- 14:42define a constant in your code, like,
- 14:45let's say, you know, a list of names
- 14:47or a set of some other objects.
- 14:50If those get too long and they don't
- 14:52fit into a line, like usually, you
- 14:54know, according to pep8, we're spacing
- 14:55them out and we're kind of spreading
- 14:57them out across line breaks.
- 14:58Now, when you do that, this usually
- 15:01looks pretty good.
- 15:02But then when you add an element to
- 15:04the end of the list, you've got to
- 15:06make sure you have your trailing
- 15:08comma right in that list, because if
- 15:11there's no trailing comma, then and
- 15:13you're working with strings, for
- 15:14example, cpython, the interpreter or
- 15:17the parser is actually going to mash
- 15:18two consecutive strings together into
- 15:20one.
- 15:21So you're going to end up with a
- 15:22completely different value.
- 15:23And it's not a it's not a interpreter
- 15:25error or parser error or syntax error.
- 15:27But this you actually get the wrong
- 15:29output.
- 15:29And so this if that makes sense so far.
- 15:33So if the first if you had a list of
- 15:35four for names, name one, two, three,
- 15:39and four, and you forget the comma
- 15:40because you added a new line, but you
- 15:41forgot the comma, it's what's in the
- 15:43list is name one, name two, name three
- 15:45and four as one string, like as if you
- 15:47would put a plus there.
- 15:48And that drives me crazy about Python.
- 15:50Like, I understand, OK, there's a few
- 15:52conveniences, but the ability to
- 15:55introduce bugs by having two string
- 15:57variables next to each other and
- 15:59forgetting a comma or something is
- 16:01really frustrating.
- 16:02It is totally frustrating.
- 16:05Yeah.
- 16:05And so this is something that I saw come
- 16:08up in code reviews.
- 16:09you know, as I was, we were at the
- 16:11time like onboarding two new developers
- 16:14and this actually came up in the code
- 16:16review and we're like, what's going on?
- 16:17Like, I hate this language, right?
- 16:19Like it was one of these things where
- 16:20you run into it and you're like, well,
- 16:21why did this just happen?
- 16:22And it's like a really nasty error or
- 16:25bug that you can introduce into your
- 16:26programs.
- 16:27And so basically we came up with this
- 16:29rule of like just ending every single
- 16:31item with a comma because you can in
- 16:32Python, you can actually have a
- 16:35trailing comma, even in the last and
- 16:37the final item in one of these
- 16:40collection constants.
- 16:41Right.
- 16:41The dictionary, the list, whatever,
- 16:43right.
- 16:43It can always have that comma there.
- 16:45The other benefit is if you don't put
- 16:48the trailing comma and you're going to
- 16:50create new items in that list and you
- 16:53look at a diff in source control, it
- 16:55looks like the previous item has
- 16:57changed as well because that line
- 16:58changed with a comma, whereas it only
- 17:00changed so you could extend it.
- 17:02But if you always put the comma
- 17:03there, like literally the lines that
- 17:04changed are the lines that changed.
- 17:06Yeah, that's right.
- 17:06You can keep your your diffs, your
- 17:09get diffs nice and clean.
- 17:10And I mean, you know, I don't know,
- 17:12like this probably makes us sound like
- 17:13super nitpicky.
- 17:14You know, if you're like, oh, you're
- 17:15you're like the senior developer
- 17:17reviewing someone else's code, you're
- 17:19pointing out stuff like that.
- 17:20But honestly, I think a lot of times
- 17:22it's a little things, right?
- 17:23Like if you see like it's a little
- 17:24bit hard to talk about this on, I
- 17:26guess, on a podcast where we can show
- 17:28people code examples.
- 17:28But if you see this in front of you,
- 17:31usually people go like, oh, yeah,
- 17:32that makes sense.
- 17:32I'm just going to start doing that
- 17:33because you can just remove that
- 17:36class of errors or that potential
- 17:38for errors there.
- 17:39You can just remove that completely
- 17:40just by making one tiny, tiny,
- 17:42small change to your coding habits
- 17:44or how you write your code.
- 17:46It's super simple to do and
- 17:47understand how it works is great.
- 17:48The other place that that's really
- 17:50helpful is for code generation.
- 17:52So imagine you've got like a cookie
- 17:54cutter template and you're cookie.
- 17:56you enter some values and that's
- 17:58powered by Jinja, too.
- 17:59So you put some very like give it a
- 18:02list and it'll generate like it could
- 18:03generate a bunch of code that's going
- 18:04to generate your starter project.
- 18:06And because you can trail everything
- 18:08with a comma, you don't need some weird
- 18:09if case to like filter out the last
- 18:11comma and just throw that puppy in
- 18:12there and it's all good.
- 18:13Oh, that's nice.
- 18:14Yeah, I see.
- 18:15That would make sense.
- 18:15Yeah.
- 18:17This portion of Talk Python To Me is
- 18:19brought to you by Linode.
- 18:20Are you looking for bulletproof
- 18:22hosting that's fast, simple
- 18:23and incredibly affordable?
- 18:25Look past that bookstore and check
- 18:26out Linode at talkpython.fm
- 18:29slash Linode, L-I-N-O-D-E.
- 18:31Plans start at just $5 a month for
- 18:33a dedicated server with a gig of RAM.
- 18:35They have 10 data centers across the
- 18:37globe.
- 18:37So no matter where you are,
- 18:38there's a data center near you.
- 18:40Whether you want to run your Python
- 18:41web app, host a private Git server
- 18:43or a file server, you'll get native
- 18:45SSDs on all machines, a newly
- 18:47upgraded 200 gigabit network and
- 18:4924-7 friendly support, even on
- 18:51holidays and a seven day money
- 18:53back guarantee.
- 18:53Want a dedicated server for free
- 18:55for the next four months?
- 18:57Use the coupon code Python17 at
- 18:59talkpython.fm/Linode.
- 19:01Let's move on to sort of a more
- 19:05protective, defensive way of coding
- 19:08by covering our assets with asserts.
- 19:11Yeah, the assert statement.
- 19:14Yeah.
- 19:15So you talked about the assert
- 19:16statement, which I don't see being used
- 19:18all that much in the code that I run
- 19:20across.
- 19:20Maybe it's just what I've been looking
- 19:22at, but I don't see it that often.
- 19:23Yeah.
- 19:23And I think that's a shame.
- 19:25I don't know.
- 19:25Like I always felt the assert statement
- 19:27in Python was really, really useful.
- 19:29I feel like it's underutilized.
- 19:31It can really help people write while
- 19:34making their code easier to debug.
- 19:36First and foremost, you know, it's
- 19:38like a defensive programming technique.
- 19:41And basically what an assert statement does,
- 19:43it's like a shorthand for testing a condition.
- 19:47And then if the condition is false, it will
- 19:50automatically raise an exception.
- 19:51It will just throw an assertion error.
- 19:54So you can use assert statements in your code
- 19:56just to quickly test conditions.
- 19:58If you don't want to write, you know, like a
- 20:00longer if statement and really, you know, maybe
- 20:03define a custom exception or anything like
- 20:04that.
- 20:04You just want to make sure that, for example, a
- 20:06product price, it shouldn't be negative or
- 20:08something like that, where you don't really
- 20:10expect this to happen, but you kind of want
- 20:12to make sure that it can never actually really
- 20:14happen in production because crazy things
- 20:16sometimes happen in production.
- 20:17Then this is just a great little tool or little
- 20:21feature in Python that you can use.
- 20:23Yeah. And I really like the little examples
- 20:25that you give.
- 20:25So like when you introduce assertions, you talk
- 20:28about like, let's imagine you have a store, say
- 20:30for buying online classes or something, and you
- 20:32want to set up a discount coupon type of
- 20:34mechanism.
- 20:34And so you might assert that after applying the
- 20:37discount, the price is no less than zero and no
- 20:40more than the actual maximum price.
- 20:43So you don't get like a negative, negative coupon
- 20:45or something.
- 20:46Charge them more than if they didn't enter it.
- 20:48The worst coupon in the world.
- 20:50Please don't put that in.
- 20:53Yeah. So that's, those are all these little
- 20:55examples that you have in the book are really
- 20:56great. And so like asserting after you apply the
- 20:59discount code that the price is zero to the full
- 21:02price is pretty straightforward.
- 21:04So you do talk about some really interesting things
- 21:07in here. One is that the idea behind assertions is
- 21:11not the same as proper error handling.
- 21:14And that these are really meant to handle
- 21:16conditions that are like bugs, not just invalid
- 21:20input.
- 21:20That's right. Yeah. You want to basically use an
- 21:23assertion. If you want to inform yourself or fellow
- 21:26developers about an unrecoverable error in your
- 21:29program. So I like to think of them more as a
- 21:32debugging aid rather than just, you know, a glorifier,
- 21:35like a fancy or weird way to throw an exception.
- 21:38So this is really, you know, your app is probably still
- 21:41going to come down crashing, but at least you will know
- 21:45exactly what cost your application to crash.
- 21:48So it's usually going to make it easier to have your
- 21:51application, to have it fail fast, right?
- 21:54When it hits that, that condition that it should not, or
- 21:56when it goes into a state that it should not be in rather than
- 22:00having that carry on.
- 22:01And then at a later time, you know, cause trouble further down
- 22:03the road. And so using an assert statement or using multiple
- 22:06assert statements, that's a great way to just have these little
- 22:09checkpoints in your code that you can just sprinkle around these
- 22:13little self checks and they can really save you a ton of time
- 22:17because they give you all the context you need.
- 22:19They make it easier for you to have that context to debug your
- 22:24programs because of course, nothing is a silver bullet here.
- 22:26So yeah, sure.
- 22:27But I do think it's good to have, like you said, that context,
- 22:30because a lot of times maybe the website would start crashing
- 22:34because Stripe is rejecting the price that you're trying to
- 22:37charge when the price is negative.
- 22:39And you're like, what is going on here?
- 22:40Why is this happening?
- 22:41Right.
- 22:42Whereas if you had an assertion that said, well, like when we
- 22:45applied the discount code, the price went negative.
- 22:46Like, oh, I see.
- 22:48I know what's going on here.
- 22:49So it's really nice.
- 22:52A final warning that you had in here is you should not use
- 22:58assertions for regular checks, right?
- 23:01Like if you ask a person to enter a number between zero and
- 23:0410 and they enter 11, you shouldn't like assert that that's
- 23:07between zero to 10, right?
- 23:08Because they can be turned off the dunder debug setting that you
- 23:14talk about.
- 23:15And also they're, they don't, don't tell you anything about it
- 23:19from a perspective of the exception, right?
- 23:21It's always an assertion error, not like a negative number custom
- 23:25exception thing you made or something.
- 23:26Yeah.
- 23:26You can add a message to the assertion that will also be part of
- 23:30the exception stack trace.
- 23:32But, but yeah, like you said, it's, it's pretty dangerous to use
- 23:36assertions for actually handling conditions or checking for
- 23:40conditions that you expect to happen as part of the normal program
- 23:43flow.
- 23:43So for example, if you do your user validation with an assert
- 23:47statement, you know, if you go assert user is admin or something
- 23:51like that, well, that's going to work great.
- 23:53But as soon as these assertions are turned off and they can be
- 23:56globally disabled at the interpreter level, then all of a sudden, you know,
- 24:01everyone can log into your app and do all kinds of crazy things because
- 24:04all of a sudden everyone is an admin.
- 24:05And so you don't want to do that.
- 24:07Yeah, that's for sure.
- 24:08Yeah.
- 24:09You would basically be able to pass a flag that turns off all air
- 24:12handling and preconditioned checking.
- 24:14That might be bad.
- 24:16Do you know why they are statements and not functions?
- 24:19I remember reading about that.
- 24:21I hope I didn't actually mention this in the book and now I forgot about it.
- 24:24I don't think you did.
- 24:25It seems to me like maybe one of the reasons is if you do turn them
- 24:29off, they can like literally vanish from the code and not just become
- 24:32operations that don't do anything.
- 24:34So maybe they're easier to like factor out.
- 24:36I actually have no idea, but it drives me crazy.
- 24:39Like there's like weird conditions about the tuples versus parameters and
- 24:43all sorts of funkiness because they're not just like actual calls,
- 24:48function calls.
- 24:48That's right.
- 24:49It's very easy to write assertion statements that never fail because you
- 24:53would somehow intuitively think this feels more like a function call,
- 24:58sort of like the print statement versus the print function.
- 25:01But if you write a syntax or if you use this like a function and you put
- 25:07parentheses around it, Python can actually interpret it as this as a tuple.
- 25:11So it's like the assert statement plus a tuple expression.
- 25:17This will always be so this will also be true or truthy.
- 25:21And so, yeah, you can you can shoot yourself in the foot that way.
- 25:25But I think this is now actually a warning in Python 3.
- 25:29Yeah.
- 25:29Yeah.
- 25:30You do get a warning in at least 3.6.
- 25:32I can tell you that for sure.
- 25:33So that's good.
- 25:33Yeah.
- 25:34Yeah.
- 25:34But I'd rather have just the language, actually, you know,
- 25:36just not let that be possible in the first place.
- 25:40All right.
- 25:41So let's move on to the next one.
- 25:42The shocking truth about string formatting.
- 25:45It's not very Zen-like, is it?
- 25:47Yeah.
- 25:47No, that's true.
- 25:49I mean, this is one of the realizations I had when the format strings or F
- 25:53strings landed in Python 3.6.
- 25:56I was like, hold on a minute.
- 25:57Like, there's like four different ways to do string formatting in Python now.
- 26:01And variations on each four of those.
- 26:03So which one should I actually use?
- 26:07And it seemed to go against the Zen of Python as well.
- 26:11And, you know, I guess it's one of these things where, well, languages, they evolve over time
- 26:16and we can't just throw out, you know, all of the legacy code that we have.
- 26:20So it's like a trade-off that we need to make or people need to make.
- 26:23But still, I just thought it was interesting.
- 26:25And yeah, so let's chat about that.
- 26:28Yeah, for sure.
- 26:29So we've got the old style formatting, the like string with a percent, a format character.
- 26:36So percent S, percent D, and so on, percent F.
- 26:39Then mod the values or a tuple of the values to be inserted.
- 26:44And that's kind of more or less not the way these days, right?
- 26:49Yeah.
- 26:50I mean, it's called old style formatting for a reason, I guess.
- 26:53But yeah, so we have a newer way of doing string formatting.
- 26:59And it's called, it's referred to as new style string formatting using the string dot format function.
- 27:04But it's not actually the newest style, but it's the middle age.
- 27:08And the old style isn't actually...
- 27:11It's not deprecated, yeah.
- 27:12It's not going to go away, right?
- 27:13So I guess it's just a hint that we got from the CPython team that, you know, really you should be using...
- 27:21Maybe you shouldn't be using the old style of string formatting.
- 27:22On the other hand, it is kind of nice to use.
- 27:25Yeah.
- 27:26And it's, you know, the new style is still in Python 2.7.
- 27:30So new style would be like, hello, curly, curly, all that in quotes, dot format name or something like that.
- 27:36And that's pretty standard.
- 27:38That's really nice.
- 27:39Takes a lot of sort of its own formatting mini language to convert the values like numbers to digit grouping and things like that.
- 27:47And then we've got the f-strings, which are the new hotness in 3.6, right?
- 27:52Yeah, the f-strings are just awesome.
- 27:55They were added in Python 3.6.
- 27:56And I think it's a really, really great feature.
- 27:58The f-strings are also called formatted string literals.
- 28:01And people also refer to this as string interpolation.
- 28:04So really what this means is it's basically a new type of string.
- 28:08So you just put a lowercase f in front of your string.
- 28:12And then you can use curly braces to just have a Python expressions right in your string.
- 28:20And when Python evaluates that string, it's actually going to evaluate those expressions, convert them to a string, and then merge them with the surrounding pieces or parts of that string.
- 28:30So this is a really, really nice and concise way to define strings in Python that are dynamic.
- 28:37And it does the same job than all of the other formatting tools that we talked about or formatting styles.
- 28:42But I find this to be very readable and just very, very nice to work with.
- 28:46Yeah, I really like it too.
- 28:48The more I use it, the more I really want to use it everywhere.
- 28:50It's great.
- 28:51And so you would say F quote and then have a regular text and then curly curly.
- 28:57Then, like you said, you put these expressions and they don't.
- 29:00It looks very similar to the dictionary version or the keyword value version of the old format or the new old format or middle-aged format.
- 29:09But you can do things that are unexpected.
- 29:10Like you could actually call functions inside there.
- 29:14You can add up things.
- 29:16You can, what I didn't realize is you can actually put format descriptors like some variable colon hash X for hexadecimal representation.
- 29:27And that's pretty cool.
- 29:28It's pretty neat.
- 29:29And I think it's also nice and readable because you just have all of the context you want.
- 29:35Like one of the challenges I've felt with, you know, at least old style string formatting format is that a lot of times you have to jump back and forth.
- 29:43And then, you know, if you have a complicated format string, you have to actually count out like, okay, this percent S, a placeholder, it refers to this other variable.
- 29:52And you just have to do all of that, that mental gymnastics to figure out what's actually going on.
- 29:57And with f-strings, well, you see it right in front of you and it's kind of nice.
- 30:02Yeah.
- 30:03It eliminates a whole class of bugs.
- 30:04Like it's totally possible to have too few arguments in like a format expression or too many arguments or get them in the wrong order.
- 30:13But when the actual variable goes in the spot, like that's what's in the spot, right?
- 30:18There's no more debate in it.
- 30:19Yeah.
- 30:19I guess the one thing that is kind of nice sometimes is in the string, you have to repeat something that's like sort of an expression that got evaluated.
- 30:27Like that might be in the middle.
- 30:28And so I'm going to propose some nomenclature here and we'll see if the world will take it.
- 30:33I don't know.
- 30:34So let's call the old style.
- 30:37Old style is finer.
- 30:38It could be legacy style formatting.
- 30:40I think that's even a better word for it.
- 30:42The new style, what should we call that?
- 30:45What do you think?
- 30:45The traditional string formatting style.
- 30:49And then the interpolation, we could call that the new style.
- 30:52The new style.
- 30:54Yeah.
- 30:54Let's do that.
- 30:55Let's go and submit a pull request on the Python, CPython docs.
- 31:00Exactly.
- 31:01Let's do a PEP to change the naming.
- 31:02Exactly.
- 31:04Exactly.
- 31:05And so really quick.
- 31:06The last one is one that I really hadn't played with, which is it's sort of the little Bobby tables equivalent of string.
- 31:13So it's something called string templates.
- 31:16And it's really good for when you're like taking an untrusted input and jamming it into a string format.
- 31:21Yeah, this is great when you're dealing with user input.
- 31:24You know, when you have something else that comes in externally where users can actually define those templates.
- 31:30Because the problem is if you just feed that into one of the other formatting techniques or methods, people can actually execute code in your interpreter environment.
- 31:40So there's a way to reach out and look at global variables.
- 31:47I mean, I'm in Ronaker's blog where he demonstrated that technique.
- 31:51And basically you can go in and there's a way to reach out from a format string and to reach into the Dunder globals dictionary and just to look up any variable in the global variable environment.
- 32:06So, you know, if you have your secret API key in there and this is a web app, then, well, bye bye API key.
- 32:12It's just going to, you know, people can leak it out.
- 32:15What's my name?
- 32:16My name is global square bracket AWS secret key.
- 32:19Oh, look, there it is.
- 32:21Yeah, that's bad.
- 32:22Yeah.
- 32:22Okay.
- 32:23Yeah.
- 32:23So you can use this template thing.
- 32:24And so the template strings, they prevent that.
- 32:26Yeah, nice.
- 32:27They're kind of like parametrize queries in SQL expressions.
- 32:30Like they don't really get evaluated.
- 32:32They just get put in there as raw strings, but they kind of signal that this part is input and it doesn't run.
- 32:38And this, it just goes here as text.
- 32:41This portion of Talk Python To Me has been brought to you by Rollbar.
- 32:45One of the frustrating things about being a developer is dealing with errors.
- 32:49Relying on users to report errors, digging through log files, trying to debug issues, or getting millions of alerts just flooding your inbox and ruining your day.
- 32:58With Rollbar's full stack error monitoring, you get the context, insight, and control you need to find and fix bugs faster.
- 33:05Adding Rollbar to your Python app is as easy as pip install Rollbar.
- 33:08You can start tracking production errors and deployments in eight minutes or less.
- 33:13Are you considering self-hosting tools for security or compliance reasons?
- 33:17Then you should really check out Rollbar's compliant SaaS option.
- 33:21Get advanced security features and meet compliance without the hassle of self-hosting, including HIPAA, ISO 27001, Privacy Shield, and more.
- 33:30They'd love to give you a demo.
- 33:31Give Rollbar a try today.
- 33:33Go to talkpython.fm/Rollbar and check them out.
- 33:37So the next one that you talked about is exceptions.
- 33:41And there's all sorts of stuff around exceptions.
- 33:44I mean, there's the whole embracing the look before you leap, rather disregarding the look before you leap style from C and embracing that it's easier to ask for forgiveness than permission.
- 33:53And just really using exceptions as, like, for exceptional cases, right?
- 34:00But then there's also making sense of the consuming the APIs that have been really embracing that format.
- 34:07So Crete, your recommendation is to define your own exception classes to make your code more readable, self-documenting, and so on, right?
- 34:15Under some circumstances, I think that makes a lot of sense.
- 34:17I feel like a lot of times you're totally fine if you just, let's say, raise a value error or a key error.
- 34:23If it's just perfectly clear, you know, if you're working with a small function and this is not some perfectly polished library that you're releasing to the world, you know, just go ahead and raise a value error.
- 34:33Don't bother with defining your own exception hierarchy.
- 34:36But I think as soon as you have a larger piece of code that you want to share, either across multiple applications or just, you know, kind of have it nicely served in a well-defined package where it's its own package, its own module, then I think it makes sense.
- 34:51What do you think about this?
- 34:52What if you have your rule as if it has a setup dpy, it has its own exception hierarchy?
- 34:57I think that makes a lot of sense.
- 34:58Yeah.
- 34:59I think that's a package.
- 35:00That's a good idea.
- 35:00People consume it externally.
- 35:02Maybe it should be more formalized in this way.
- 35:05Yeah.
- 35:05Yeah.
- 35:06At the very least then.
- 35:07And also if you're working in a bigger application, you know, where you have a lot of code, like I don't do this in any little script that I write, but if you have a larger body of code, I feel like these are the things that really make it easier for you also to work with your code in the future, because it's just nicer or it's easier for you to see, you know, what went wrong and where.
- 35:27Essentially, a value error can come from many, many places.
- 35:31But if it's a password validator exception that you defined, then, well, you know, this has something to do with password validation or, you know, the next person working on your code knows that it has something to do with that.
- 35:42Another thing that I think is really nice.
- 35:43Another thing that I think is really nice that you talked about is actually having a base exception that defines, like, these are all the errors that come out of this particular package.
- 35:53So that makes me crazy.
- 35:55Sometimes I'll be like handling errors out of different packages.
- 35:58I'm like, what is this thing throwing?
- 36:00Is it throwing one of its own errors?
- 36:01Is it throwing like a system socket error?
- 36:04Like, what is this?
- 36:06And how do I find a way to catch it?
- 36:07You know what I mean?
- 36:08It'd be really nice to go, okay, I'm working with the SQLAlchemy library.
- 36:13So I'm going to catch, I'm going to start by catching SQLAlchemy errors and like dealing with that.
- 36:18And then I'll start to add on special cases of like, oh yeah, this is when the network is down.
- 36:22This is when the database doesn't exist or, you know, whatever.
- 36:25Yeah, exactly.
- 36:26And some people start using value error or something like that, like a built-in exception for that.
- 36:31And then just put that information in the exception message.
- 36:35And I mean, for debugging purposes, I think that's okay because, well, you're just going to look at the stack trace and you know, sort of know what's what happened.
- 36:43But you really don't have the granularity when you're catching these exceptions, right?
- 36:49When you have a try except block and like actually exception is part of your structuring the control flow of your program.
- 36:56Then that becomes very dangerous because then, well, you kind of have to parse out that string.
- 37:01And I don't know, I don't like that.
- 37:03Yeah.
- 37:03Well, and also if you're trying to say, well, I'm trying to deal with the case when there's a value error, but is it from this library or that library?
- 37:11Because that means really different things.
- 37:13One is the network is down.
- 37:14The other is you don't have permission.
- 37:16And I don't really know what to do here, right?
- 37:18So you can totally help that.
- 37:20And, you know, another thing that I'm not sure people think very much about, but I think would be really helpful is like discovering the solution to some common problems.
- 37:30So if it's a generic standard library error and you go search for that on like Google on Stack Overflow, I use Rollbar and Rollbar has this sort of mechanism where you go into the error reporting for your web app.
- 37:43And it says here are the common solutions or issues that are found associated with this error.
- 37:48But if it's super general, it's like, well, nothing, right?
- 37:50But if it's pretty specific errors, like you could probably get help more easily on lots of levels that way.
- 37:56I really like your thinking there.
- 37:58It's like this whole meta game of programming, right?
- 38:01Where you're not just fending on your own, like you're fighting the machine there.
- 38:05But a lot of times it's just great if you can punch something into Google and just at least find some other people who have the same problem.
- 38:11And yeah, I think it really increases the Google ability of your exceptions if you do that.
- 38:18Yeah, absolutely.
- 38:19Totally agree.
- 38:20All right.
- 38:21So let's talk about naming a little bit.
- 38:23I know that you've talked about this on your blog and some articles.
- 38:26I've talked about this, Brian Okken on Python Bytes.
- 38:29There's a special meaning for underscore and double underscores and prefix underscores and suffix underscores in Python.
- 38:37And maybe we should just touch on those a little bit.
- 38:39There's like five or so that you cover in here, right?
- 38:43So many underscores.
- 38:44Yeah, Python does love the underscore.
- 38:46I'm getting undersource from all these underscores.
- 38:48So the first thing that we can start off with is like a semi-private variable, underscore var.
- 38:55Yeah.
- 38:56So the underscore var is just a naming convention.
- 38:59And I like that you called it semi-private because a lot of times when people come from, let's say, a Java or C# background,
- 39:06they assume that private really means private in this context.
- 39:09Like you cannot, you know, reach in and modify this variable.
- 39:12But in Python, if you do underscore var, it's a naming convention.
- 39:17You know, it's just something that the community agrees on.
- 39:19It's like a hint to the programmer, but it's generally not enforced.
- 39:21So that's something you got to keep in mind.
- 39:24Like we're all consenting adults or whatever Guido said about that.
- 39:28Exactly.
- 39:28I've heard him describe it that way as well.
- 39:31But then there's the, yeah, but I still don't trust you.
- 39:35So I'll put another underscore in front of it.
- 39:37Yeah.
- 39:37And that's where it gets interesting because that enables or that kicks, that makes the name mangling kick in.
- 39:45So if you do a double underscore in front of your variable name, the Python interpreter will actually mangle that name and prepend the name of the surrounding class to that variable.
- 39:58So this sort of kind of makes it like a private variable, but not really, because if you do the mangling yourself, you know, if you just do that string concatenation in your head or in your program, you can still reach in and modify that variable.
- 40:12So it's really more a mechanism for preventing naming collisions, but not, you know, not a way to implement private variables.
- 40:19Not true private.
- 40:20I mean, it's not private in the sense that it's private, but there's still really not that much private in all the other languages.
- 40:27So if you think of say C#, it has a private protected internal, all these different mechanisms and scoping rules.
- 40:34But then on the other hand, you have reflection and you could just go ask for it for the private variables.
- 40:39Java has introspection C++ they have private, but you know, how many bytes offset is that?
- 40:45Okay.
- 40:45We just go to the front of the object and like read that form over.
- 40:49And we, I mean, like in all of these languages, there's a backdoor to these privates.
- 40:54Right.
- 40:55And so I feel like this is really no more or less than that.
- 41:00It's just maybe slightly easier if you know the convention, but it certainly screams to you.
- 41:06Don't do this.
- 41:07Yeah.
- 41:08It's like you're, you're digging a moat around your, around your fortress.
- 41:12And, you know, of course people can still overcome it and destroy your beautiful programming fortress,
- 41:18but you're just making it a little bit harder for people to hurt themselves.
- 41:21So yeah.
- 41:22I mean, the whole point is like people shouldn't mess with the internal implementation.
- 41:26They shouldn't depend on the internal implementation.
- 41:28And if they do, they're going to suffer.
- 41:31Not you probably unless they fire out, file a bug and you got to deal with it.
- 41:34Yeah.
- 41:34Yeah.
- 41:34Yeah.
- 41:34You would hope so.
- 41:35Right.
- 41:36That it's not you as the maintainer who gets the flack for that.
- 41:40Yeah, exactly.
- 41:40But I do think like that's kind of far enough.
- 41:43I use that every now and then.
- 41:45I also use it for when I want clean autocomplete.
- 41:49If there's some library that I'm like, I want to give this to somebody.
- 41:53I want them to say thing dot and the list of the stuff I want them to work with really clearly.
- 41:58I'll put that either the underscore, the double underscore, not so much to make people stay away,
- 42:03but to like make the API more discoverable in the tooling.
- 42:07Oh, that's nice.
- 42:07That's a good idea.
- 42:08Yeah.
- 42:09And the double underscore, like it takes it entirely out of the list.
- 42:11So I'm kind of like, ah, you know, if it's not going to bother me too much, I might do that.
- 42:14But that's one of my motivations.
- 42:17I did want to ask you with the single underscore, that means like internal private.
- 42:23But do you think that means protected or truly private, like only in the class or classes and
- 42:28its derivatives?
- 42:29That'll come down to the individual developers and their teams agreement.
- 42:34Personally, I use it more like a protected variable, I think, where it's like, yeah, you
- 42:41know, if it makes sense to reach in and modify this.
- 42:44And we're part of the same, you know, if it's related code, very, very closely related code,
- 42:50then I feel like it's okay.
- 42:52But it's definitely one of these things where it's it's a it's a slippery slope, right?
- 42:55Or it's like, oh, yeah, it'll be fine.
- 42:57And it's like one of these famous last word things.
- 43:01It's this convention.
- 43:02And it's not super explicit, right?
- 43:04Like, yeah.
- 43:04But I think it's, I think it's good to know about it.
- 43:08So just really quickly, maybe touch on the other underscores.
- 43:11There's var underscore the suffix.
- 43:13Yeah, so that's mostly just used to get around.
- 43:16So, you know, you can use a reserved word, like a Python keyword, like class, you can use
- 43:23that as a variable name in your programs.
- 43:24But a lot of times, it actually makes sense to have a variable name, name class, if you're
- 43:29passing in some kind of class to a function.
- 43:31Yeah, the place where I've seen it is in Beautiful Soup.
- 43:34You can do a find and specify a class keyword.
- 43:37And then you say the name of the class you're looking for in the HTML.
- 43:40But you can't say class, right?
- 43:42So they have class underscore equals as the keyword argument.
- 43:45Yeah, there you go.
- 43:46And then people sometimes do, they just kind of mangle the name themselves.
- 43:50So they maybe call it class with a K or with a Z.
- 43:53Yeah, with a Z.
- 43:54I actually went and dug through PEP 8.
- 43:58And so what people recommend or what like the official recommendation is to just end
- 44:03your variable name with an underscore.
- 44:06And that disambiguates it and make sure it doesn't conflict or there's no conflict with
- 44:11the keyword or the built-in reserved word.
- 44:13And I kind of like that because I feel like, yeah, it's pretty clear, pretty easy to remember.
- 44:16And we have more underscores that way.
- 44:19Isn't that wonderful?
- 44:20Yeah, we have more.
- 44:21All these languages have ways of dealing with them and none of them are really amazing.
- 44:26This seems just totally, totally decent.
- 44:28And then we have the Dunder methods, which is really the Python data model magic methods,
- 44:34which I know you don't like that term, but.
- 44:36Do you like that?
- 44:36I was going to ask you, do you like the word magic methods?
- 44:39To me, they feel like operators.
- 44:41I don't know.
- 44:42I don't really like the term either because magic seems like they're inaccessible and
- 44:47they're like these internal black box things.
- 44:49They're not really right.
- 44:51And then the final thing around underscores, because Python loves underscores, is to use
- 44:56it as the, I don't care about this variable.
- 44:58Don't lint it.
- 45:00Don't make me name it.
- 45:01I don't care about it.
- 45:03Yeah.
- 45:03It almost has like a Haskell kind of feel to it where you're like, oh yeah, I'm so cool.
- 45:08I'm ignoring all these variables and I'm deconstructing tuples and whatnot.
- 45:13Yes, exactly.
- 45:13Yeah.
- 45:14So if you want to like loop over something like 10 times, you could say for n in range
- 45:22one to 11 or zero to 10 or whatever.
- 45:24But if you don't care about that n, you know, you might get warnings like that variable is
- 45:29not used and whatnot.
- 45:30And if you want to be explicit and say, I don't care about the loop variable.
- 45:33I just want to do this 10 times.
- 45:34Just put an underscore instead of n and everything's good.
- 45:36Same thing for like parameters.
- 45:38Yeah.
- 45:38And methods.
- 45:39Yeah.
- 45:39The only thing you got to be careful about is when you do that in a Python REPL session
- 45:43where the underscore, like a single standalone underscore is usually represents the result
- 45:49of the last expression that the interpreter evaluated, which is kind of nice if you're using,
- 45:54you know, if you're working with the REPL, but if you redefine it, that can cause all kinds
- 46:00of trouble.
- 46:00So, you know, that's kind of the one caveat with using the single underscore.
- 46:05That's for sure.
- 46:06And it's nice because if you type an expression like, oh, geez, I would love to use that as
- 46:09a variable.
- 46:10You can say variable name equals underscore and then roll on from there.
- 46:13That's nice in the REPL.
- 46:15Yeah.
- 46:15All right.
- 46:16So let's move on to data structures.
- 46:18And this is like a pretty significant chapter that you talk about.
- 46:22And I kind of hinted at this at the opening.
- 46:25Like you can come in and find the really basic structures really easily, but there's actually
- 46:29a rich set of data structures that are special made for certain circumstances and situations.
- 46:36So let's dig into those.
- 46:37Maybe, yeah, start with the dictionaries because that's a pretty central thing in Python.
- 46:42Yeah.
- 46:42It's like the central data structure in definitely, I think, in Python and in, I feel like probably
- 46:47in programming in general, it's like just a super versatile tool.
- 46:51Yeah.
- 46:51And it's not just something you use in Python, right?
- 46:53Like the variables and attributes in a class are defined by those.
- 46:59You know, the stuff in module, like global variables, all sorts of stuff, right?
- 47:02Like it's the structure that stores, that holds the types of Python together, I guess.
- 47:07Like the stuff the universe is really made off.
- 47:10It's the dark matter of Python.
- 47:14I like that.
- 47:15Yeah.
- 47:16And it's, I mean, it's everywhere, you know, but on the other hand, when we started, you
- 47:21know, the last place I worked at, we were interviewing pretty aggressively and by aggressively mean,
- 47:25you know, interviewing a lot of people and trying to hire Python developers.
- 47:29And so one of the things that came up over and over again in those interviews that was that
- 47:34people were really struggling with when you're talking about like basic, like hash table or
- 47:38hash map questions, people were like, yeah, I'm not sure if that's available in Python.
- 47:43And I don't really know how this concept would work in Python, but at the same time, they knew
- 47:49about dictionaries.
- 47:50So that was just, I don't know.
- 47:52I mean, obviously, you know, I understand where this is coming from and this is not necessarily
- 47:56like a terrible sign.
- 47:57But on the other hand, I was curious or just confused.
- 48:00Like, why, why was this happening?
- 48:01I mean, you know, like, why, why is there this disconnect?
- 48:04Because in Java, well, if you want like a mapping structure or a dictionary, you know that you're
- 48:10dealing with some kind of map or a hash map, right?
- 48:13Like, you know, what kind of concrete data structure you instantiate in Python is just like, yeah,
- 48:17it's a dictionary.
- 48:17It does its job.
- 48:18I don't really care how it works.
- 48:19And that's good.
- 48:20But it also means you're kind of, you're disconnected from a lot of the specific implementation details
- 48:25as a developer.
- 48:26Yeah, that's for sure.
- 48:27And until recently, dictionaries and still under some circumstances, they're unordered,
- 48:33which can cause all sorts of issues.
- 48:35If you read a file in, make some changes, save it back out in Python, you know, and you had
- 48:40that file in source control, it could look like 95% change where you just change like something
- 48:45from true to false or something silly like that.
- 48:46Right.
- 48:47And so there's actually, depending on the situations that you're in, there's all these
- 48:52other types of dictionaries that are pretty awesome.
- 48:54So we have ordered dict.
- 48:55I've seen the people that do data science and like really chain together generator expressions
- 49:01and list comprehensions and make massive use of default dict.
- 49:04There's chain map, which I don't think I had heard of chain map before.
- 49:08Maybe I heard of it and forgot about it.
- 49:09There's even mapping proxy type.
- 49:13So maybe take us through like when you might want to use those other things, like when dict
- 49:17or curly curly is not enough.
- 49:18I was also really, really amazed or, you know, really enjoyed learning more about chain map.
- 49:24So chain map is really cool.
- 49:25Basically, you can chain together multiple dictionaries and treat them like a single dictionary, like
- 49:31a single mapping.
- 49:32So you can group several dictionaries into one.
- 49:35And then when you do a lookup, actually the chain map is going to go and it's going to
- 49:39search all of these underlying dictionaries one by one until it finds a match.
- 49:43Yeah, that's really cool.
- 49:44To me, it felt like what I had previously done, if I wanted this behaviors, I would create a
- 49:50new dictionary and I would merge all of them into it.
- 49:53Then I would ask that question.
- 49:54But this lets you basically create the same behavior without actually building that other
- 49:59dictionary.
- 50:00And potentially you could create the chain map and then one of the underlying dictionaries
- 50:04could change.
- 50:05And then the answer coming back from the chain map would adapt to that, which is pretty awesome,
- 50:08right?
- 50:09That's right.
- 50:09And you don't incur that memory overhead because, you know, it's just like an abstract way to
- 50:15merge all these dictionaries together.
- 50:16And it's just the magic is in this search algorithm.
- 50:19But on the other hand, of course, you know, it can potentially be slower because the search
- 50:24actually needs to happen.
- 50:25So depending on, you know, if you change together thousands of dictionaries, maybe that's not
- 50:28a good idea.
- 50:29But if you, if you only have a couple of them and maybe you're combining a bunch of
- 50:33configuration dictionaries, then this could be really handy and you can just make sure,
- 50:37you know, okay, we have like a default setting dictionary with default settings, and then we
- 50:42can override them with more specific user settings.
- 50:45Right.
- 50:46Put the user settings and stuff at the beginning of the chain map.
- 50:49Yeah.
- 50:49That's nice.
- 50:51Yeah.
- 50:51The place I see this happening all the time on the web is because you've got to go, you've
- 50:54got a dictionary for like the route URL data.
- 50:57You've got one for the post, you've got one for the query string, you've got one for or else,
- 51:01somewhere else, headers, all sorts of stuff.
- 51:04And you can like combine them together into one place and ask like, did they give me this
- 51:08data somehow?
- 51:09I don't really care where it came from.
- 51:10Did they tell me their username or something?
- 51:12Right.
- 51:13Yeah.
- 51:13Where's the API key?
- 51:14Yeah, exactly.
- 51:14And then where's that print statement I'm looking for?
- 51:16All right.
- 51:18So another major class of data structures that I've found to be pretty interesting that you
- 51:23covered were arrays.
- 51:24So, you know, sort of the most common one of this is the list, but there's more than that,
- 51:29right?
- 51:29Oh, yeah.
- 51:29Yeah.
- 51:30And it's really similar to, you know, what we just discussed with the dictionaries where,
- 51:34well, if you're looking for an array, it's typically people will just tell you,
- 51:38well, use a Python list.
- 51:39It does everything you want.
- 51:41And, you know, behind the scenes, Python lists are actually dynamic arrays.
- 51:44So mutable dynamic arrays, they will grow and shrink with the data you put in.
- 51:48But there's actually other options as well.
- 51:51You know, you can, in a lot of ways, you can view a tuple as an immutable array, which means
- 51:57you can't change it.
- 51:58Right.
- 51:58You index into it.
- 51:59It has a length.
- 52:00You can say, give me the third, give me the fifth one, but it's totally static.
- 52:03Even the individual elements can't be changed, right?
- 52:06Exactly.
- 52:06Yeah.
- 52:06And I mean, the only problem you run into if you have like, you know, a nested data structure.
- 52:11So if you have a dictionary in your tuple, you can't put something else at that position in the
- 52:17tuple and replace the dictionary, but you could still reach in and modify the dictionary because
- 52:20the dictionary itself is not immutable.
- 52:22It's, you can modify it.
- 52:23And so sometimes, you know, that causes trouble for people.
- 52:26But in general, you know, yeah, you can absolutely treat a tuple like as an immutable array.
- 52:31Let's go back just for a second.
- 52:33Like one thing we kind of moved on from without talking much about it is mapping proxy type
- 52:37in the dictionary space.
- 52:38And so if you really wanted like this truly immutable tuple, you want to put a dictionary
- 52:43there, you could wrap it in this mapping proxy type, right?
- 52:46Oh yeah.
- 52:47That's a perfect segue.
- 52:48Yeah, exactly.
- 52:49So you, you could use this mapping proxy type class to make, take any standard dictionary and
- 52:57then to provide a read only view into that dictionary.
- 53:00So you can still access all of the variables, all the values, all the keys, but you cannot
- 53:05reach in and modify the dictionary.
- 53:07I mean, sure.
- 53:08You could just kind of pry that mapping proxy apart and then reach in and modify it.
- 53:14And this is probably not a good idea, but.
- 53:15That goes back to where the, like, you, you know, you shouldn't be doing it.
- 53:19And if you, if you mess it up, it's kind of your fault, not the APIs.
- 53:23It's like, yeah, you could install a kernel driver and patch the memory and.
- 53:27I rooted my machine and then it crashed.
- 53:29Oh, this thing is a crappy OS.
- 53:30Yeah.
- 53:32So that would, that, I mean, you could combine that for like a greater functional programming,
- 53:36immutable pass by value type of behavior, right?
- 53:39Yeah, you could do that.
- 53:40I mean, personally, I'm always thinking like, okay, at what point do we reach the level where
- 53:45we're really fighting against what the language wants us to do, but it is entirely possible,
- 53:51you know, to do what we just talked about, you know, you have a tuple and then instead
- 53:55of putting like dictionaries in there, I mean, you'd probably just go with a frozen dict class
- 53:59for that, but you could totally also wrap it in this mapping proxy type thing.
- 54:03And that way you really enforce that immutability as best as we can.
- 54:07And I think that's so nice about Python.
- 54:10The frozen dict is a little more obvious, but yeah, that is super nice.
- 54:12One of the things that traditional, let's say C, C++ arrays to a lesser degree, like
- 54:20some category of say C# arrays have is they have the same type in them and then they're
- 54:27tightly packed.
- 54:28And the tightly packed thing that I'm really thinking about, right?
- 54:30So if I say list and Python and I put number one, number two, number three in there, that's
- 54:35really a tightly packed set of pointers that points out to three things that are not anywhere
- 54:40near each other.
- 54:40Potentially.
- 54:41I mean, those are probably like pre-allocated and put next to you.
- 54:45Like I know one to 20255 or pre-allocated, but you know, in general, things in the arrays
- 54:49are not nearby each other, which means you're following a lot of pointers and there's a lot of GC type
- 54:56behavior, but there are structures that if you really want homogeneous types, tightly
- 55:01packed, you can get them, right?
- 55:03You can also get that in Python because like you said, with your standard list, there's really
- 55:07no guarantees.
- 55:09If your list of a couple of elements, it could actually be much larger than just those like
- 55:14five or six elements because it's pre-allocated to a bigger size and kind of shrinking and growing
- 55:18dynamically.
- 55:19And so if you want absolute control over how your data is laid out in memory, then you
- 55:24could use something like the array class from the array module that's in the Python standard
- 55:30library.
- 55:30And with this, you can actually say, Hey, I want an array off C style floats or ints, and
- 55:38you can just put in your values there and they will be laid out in memory exactly the way you
- 55:44request it.
- 55:45But it won't accept types that don't fit the declared data type, right?
- 55:48So if you say it takes floats, it won't take a string, right?
- 55:51It enforces that as well as the packing.
- 55:53Yeah, that's right.
- 55:53So you get some type safety there.
- 55:55So it's going to make sure that you're not putting in, I don't know, something crazy needs
- 55:59to be consistent.
- 56:00Let's skip ahead a little bit.
- 56:01We're actually, I thought we would get through more of these.
- 56:03I think we're going to run out of time pretty quick.
- 56:05But there's some stuff I really want to cover while we're here, because I think it's, it's
- 56:09quite interesting.
- 56:09You talk about stacks and you talk about queues.
- 56:13And I think this list that we were just talking about, it can be many things to many people.
- 56:19It can act as a queue.
- 56:20You can take the thing off the front.
- 56:22It has a pop to take things off the end.
- 56:25You can append that's like a synonym for push sort of.
- 56:28But there's also other queues.
- 56:30And there's also some really terrible performance issues if you try to use, say, like a list for
- 56:36a queue.
- 56:37So let's maybe touch on those things while we're on this topic.
- 56:39Yeah, for sure.
- 56:40So I mean, the list, again, it's such a versatile data structure in Python, right?
- 56:45And it's, I feel like they made exactly the right performance trade-offs when they implemented
- 56:51how list is actually implemented in the CPython interpreter.
- 56:54But on the other hand, it leads to those situations where you're like, oh yeah, you know, I can push
- 56:58and pop from a list.
- 56:59So I can totally make a stack or I can totally use it to make a queue.
- 57:03And I mean, you can, but the problem is, for example, if you use a list to make a queue,
- 57:10you can run into a situation where you'll just have terribly slow performance.
- 57:16Because if you enqueue an element in a list and you added, like you insert it at the front,
- 57:22this will actually shifting all of the other elements by one.
- 57:26So you're looking at a linear time operation just to insert one element.
- 57:30And that's really, really bad for a queue implementation.
- 57:33That's super bad.
- 57:34Yeah.
- 57:34If you're doing like the first in, first out behavior, right?
- 57:37So append, that's fine.
- 57:39It goes on the end.
- 57:39But every time you take an element off the front, it has to entirely copy every element.
- 57:44If there's a million items in your queue, that's a heavyweight DQ operation right there.
- 57:49So ironically, you should use this thing called DQ to fix it.
- 57:53Yeah.
- 57:54So there's this great, great little class in the collections module.
- 57:57And the collections module is just so awesome.
- 58:00If you're listening to this, for anyone who's listening to this show and they haven't heard
- 58:04or played with the collections module, check out the collections module.
- 58:07There's, it's just a treasure trove.
- 58:08And it's also fun reading, you know, just, just seeing what's available in there and the
- 58:12reasons for why it exists.
- 58:14And, but yeah, the collections module has this thing called a DQ or deck.
- 58:20I think some people pronounce it.
- 58:21And this is actually implemented as a linked list.
- 58:25So you can append and remove from both ends really, really fast because, you know, it's no,
- 58:32there's no shuffling around.
- 58:34It's just, okay, cut off the head or add something to the end.
- 58:37It's a single pointer operation.
- 58:38It's super, super fast.
- 58:39That's right.
- 58:40And the DE at the beginning is not the operation of removing the item from the Q, but that's
- 58:45the double ended Q, DEQ, right?
- 58:48Yeah, that's right.
- 58:49It's fast from, from the front and fast from the back as well.
- 58:52Right.
- 58:53And we have things like Q.Q and multiprocessing.Q and some other ones, special cases one.
- 58:59So I think it's really interesting to think about these because a lot of them, you know,
- 59:03like we said, you don't really discover them on your own.
- 59:06You have to kind of study them and seek them out.
- 59:08And you're like, oh, that's, I thought just Python was slow.
- 59:11List turns out to just be a really bad Q.
- 59:13So maybe I'll do something different, right?
- 59:17Yeah.
- 59:17So I think these are all really great, but I don't want to give people the impression that
- 59:20your book is all about just data structures, right?
- 59:23There's actually, you know, just one chapter.
- 59:25It just happens to be a super rich chapter.
- 59:27So maybe like we have some other stuff that was on deck that we're not probably going
- 59:31to be able to talk about in terms of time, but maybe just run us super quick through like
- 59:34what else you wanted to highlight.
- 59:35When I wrote this book, really the angle it's written from is that I don't want to create
- 59:40another technical reference, but I want to walk people through some actionable stuff and
- 59:47explain things step-by-step with examples that make sense.
- 59:52So I'm also covering kind of the classic features in Python that are often difficult to wrap your
- 59:58head around.
- 59:59And, you know, I don't know how much more time we have here, but for example, I think if
- 1:00:05people understand decorators and like really understand how they work internally, that is
- 1:00:11a huge leap from a beginner level understanding of Python, because you need to understand, you
- 1:00:17know, a couple of rather advanced features, like you need to understand, you know, what
- 1:00:22first class functions are that all functions can be treated are real full blown objects.
- 1:00:26And, you know, you can not just call them, but also look like a hand them around to other
- 1:00:30functions.
- 1:00:30Right.
- 1:00:31And closures, the concept of like variable capture and closures is front and center there.
- 1:00:35And that is all super important stuff.
- 1:00:38And just so by, you know, focusing on under really understanding how this feature works,
- 1:00:43you can actually learn a lot about Python and you can pick up a lot of other features and
- 1:00:50abilities that are going to help you in programming Python in general.
- 1:00:54And so a big part of the book is, you know, I really spent a lot of time on this and it's,
- 1:00:59it's been iterated over.
- 1:01:01I tested this with people and just walking them through understanding how decorators,
- 1:01:06for example, work, or, you know, walking them through iterators and then going from there
- 1:01:10to generators and then later generate expressions and really just explaining the logical connection
- 1:01:15between all of those and kind of structuring it away in a way where it's not just like,
- 1:01:19oh yeah, this is how it is, but kind of approaching the topic step by step.
- 1:01:23I think those are really, really nice things.
- 1:01:25You certainly building a simple decorator, like come up with some concept where it makes sense.
- 1:01:31And go through the motions of creating it will really, like you said, expose you to a lot.
- 1:01:35Some other things you talk about, you talk about the sushi operator, and I'm not going to give any
- 1:01:40more details.
- 1:01:40People have to check out your book to learn about that Python has a sushi operator.
- 1:01:44Design patterns like emulating a switch statement with dictionary dispatch, all sorts of stuff like
- 1:01:51that.
- 1:01:51So really just tons and tons of these little tricks.
- 1:01:53And you said the, it's like a buffet, right?
- 1:01:55So you don't have to take it in order.
- 1:01:57Yeah, that's right.
- 1:01:57So most of these chunks in this book or the sections in this book, you can actually,
- 1:02:02you know, jump around and just kind of look for things that interest you.
- 1:02:05They're usually pretty short and they all have a key takeaway section at the end.
- 1:02:10So you can quickly review this.
- 1:02:11And for example, I heard feedback on the data structures chapter where it's, you know,
- 1:02:16it's a great way to prepare yourself for a Python interview and just kind of, you know,
- 1:02:19get updated or get a refresher on how these standard data structures map to Python.
- 1:02:24I feel like people kind of, they take the easy way out and they ask these algorithm questions
- 1:02:29and these data structure questions in interviews.
- 1:02:32And yes, it's important, but I don't necessarily feel like if I don't know the most efficient
- 1:02:38particular queue for this type of operation, that doesn't necessarily mean I couldn't write
- 1:02:42an awesome web app that has, you know, a hundred thousand users a month and has no real performance
- 1:02:47pressure on it.
- 1:02:48Right.
- 1:02:48I agree with you.
- 1:02:49I mean, unfortunately, some people will really bust out the trick questions where it's like,
- 1:02:54ha ha, you didn't know about this like special case.
- 1:02:56That means you're a fool and we're never going to hire you.
- 1:02:58But the reason I don't like those is I feel like those tend to advantage the people that
- 1:03:03have CS degrees, but not necessarily better programming skills than people who are self-taught.
- 1:03:08So I really like that your book kind of backfields that information for people.
- 1:03:11Thanks.
- 1:03:12I mean, that was one of the goals, right?
- 1:03:13Where it's like, yeah, a lot of this stuff, you don't need to spend like four years in
- 1:03:18university just to understand what a hash table is.
- 1:03:22Right.
- 1:03:22And it's, I don't like it when people create that impression.
- 1:03:25And so, you know, I feel like if you have a good practical understanding or practical programming
- 1:03:32skill, then it will be in most cases, like pretty easy for someone to fill in those gaps in
- 1:03:38our knowledge and, you know, to satisfy the people who ask the more computer science motivated
- 1:03:43questions, I guess.
- 1:03:44Yeah, absolutely.
- 1:03:44All right.
- 1:03:45Well, let's leave it there for the book because we are definitely going to run out of time.
- 1:03:48So I do want to talk about a few other projects that you have going.
- 1:03:51First of all, you have this thing called Pythonista Cafe.
- 1:03:54Did you open like some kind of like coffee shop or something?
- 1:03:59That's my lifelong dream, you know, open a coffee shop, write books.
- 1:04:03Answer a Python question before you're permitted in.
- 1:04:05Yes.
- 1:04:05So Pythonista Cafe, it all started when occasionally I read Reddit or Hacker News and it can be great.
- 1:04:12And it can also be like people have some weird personas on these forums sometimes, you know,
- 1:04:18and people kind of lash out at each other and get really worked up.
- 1:04:23And I remember this one time, like somebody made a post on Reddit, one of the Python forums
- 1:04:27on Reddit and said, hey, I'm this 14 year old guy.
- 1:04:30I really love Python.
- 1:04:31I made these tutorial videos on my YouTube channel about, you know, building a Flask app.
- 1:04:35What do you think about this?
- 1:04:36And like the first person to reply is like, oh, you're teaching it all wrong and you should
- 1:04:41have never created these videos and you'll basically, you'll never be a good programmer
- 1:04:45and blah, blah, blah.
- 1:04:45Who do you think you are?
- 1:04:46He's probably like in the top, like 5% of all like 14 year old programmers.
- 1:04:50I mean, come on.
- 1:04:51And that is nuts, right?
- 1:04:52Like, how can you, how can you, I was just almost like sitting in front of a computer
- 1:04:57shaking and getting angry at this person.
- 1:04:59And it just seems so crazy.
- 1:05:01But the problem is, I mean, this is, this is real, right?
- 1:05:04Like this happens.
- 1:05:04I mean, most of the time it's a great forum.
- 1:05:06It's a great community, but some people, they are just crazy when it, the knee jerk reactions
- 1:05:13they get.
- 1:05:13And I really liked that.
- 1:05:15And so I reached out to a bunch of people and said, hey, so here's an idea.
- 1:05:19We're going to do a Python forum, an online forum where people can discuss Python, you
- 1:05:24know, you can bring their code and get feedback on their code.
- 1:05:26And we're going to talk about all things Python, but we're going to do it in private.
- 1:05:31So it won't be a public forum and there will be an application process to get in.
- 1:05:35And I launched that at the beginning of this year.
- 1:05:39So 2017, I don't know when the show is going to come out, but so far it's been a really,
- 1:05:44really cool experience.
- 1:05:45We have about 120 people in the forum right now.
- 1:05:48And it is, I don't like the word safe space, but in a way it is a safe space where you can
- 1:05:52just go and post some code and you're going to get like really, really positive and encouraging,
- 1:05:57helpful comments.
- 1:05:58And there's no danger of someone going in and be like, oh, you're the worst like 14
- 1:06:02year old programmer in the world.
- 1:06:04And you should go back to where you came from.
- 1:06:06That's Pythonista Cafe in a nutshell.
- 1:06:08Nice.
- 1:06:09That's awesome.
- 1:06:10So yeah, you've got that going.
- 1:06:11Did you do a book or videos or something for Sublime for Python developers?
- 1:06:15Yeah, I also did that.
- 1:06:16So I have a couple of courses that I offer on my website.
- 1:06:20And one of them is also on Talk Python training.
- 1:06:22And so one of them is a complete course on setting up Sublime Text for Python development.
- 1:06:28So like an end-to-end, step-by-step course.
- 1:06:31And that consists of a couple of modules and, you know, like workbooks and video lessons on
- 1:06:35how to get a really, really awesome and productive Python environment with the Sublime Text editor,
- 1:06:40because that's my favorite editor.
- 1:06:42I know you really enjoy PyCharm and I think that's a great option too.
- 1:06:45But for me, Sublime Text has just been, it's just been a great setup.
- 1:06:49And you can script it with Python, which is nice too.
- 1:06:51It's definitely a very Pythonic place to be.
- 1:06:54That's awesome.
- 1:06:54So yeah, that's cool.
- 1:06:56If people want to learn more about like getting the most out of Sublime, they can check that out.
- 1:06:59You've got like the pip dependencies course available both on your website and Talk Python training,
- 1:07:04which I thought that was really awesome.
- 1:07:06I mean, people are like, well, I know pip.
- 1:07:08Like now this is like really, really the right way to sort of manage dependencies in your app
- 1:07:13or other packages that you're using.
- 1:07:15It's really great.
- 1:07:16So I definitely enjoyed checking that out as well.
- 1:07:19All right.
- 1:07:19So I think you've given us a spoiler for this.
- 1:07:21The last two questions here, at least the first one and the last two.
- 1:07:24Favorite editor?
- 1:07:25No, it's Sublime.
- 1:07:26Bye, John.
- 1:07:26Yeah, that's awesome.
- 1:07:28Yeah, I think I like Sublime as well.
- 1:07:30I definitely use it some of the time.
- 1:07:31Very cool.
- 1:07:32And notable PyPI package this time around?
- 1:07:35Yeah.
- 1:07:35So this time around, it'll be Django because Django 2.0 just came out.
- 1:07:40That's big news.
- 1:07:40What's the biggest piece of news about Django 2.0?
- 1:07:42I think the new URL routing syntax is pretty cool.
- 1:07:46It looks really nice.
- 1:07:47And for me, Django, you know, it just represents so well what Python stands for.
- 1:07:52It's really, you know, this is a piece of software that you can depend on.
- 1:07:56It's mature.
- 1:07:57And it's for when you actually want to get work done and not be worried about having
- 1:08:01to rewrite your whole stack in six or eight months down the road.
- 1:08:04And I feel like, you know, the Django team is so awesome.
- 1:08:07And it's a huge achievement about what they've done over the last couple of years.
- 1:08:11And yeah, so, you know, my pick is Django.
- 1:08:13Awesome.
- 1:08:14New 2.0.
- 1:08:15Yeah, the shiny Django.
- 1:08:16And the other major news around there is that they've dropped legacy Python support.
- 1:08:21No more Python 2 in Django 2.
- 1:08:23Oh, yeah.
- 1:08:23Yeah, ironically, given the numbers there.
- 1:08:25I think that's a super positive move.
- 1:08:29Just the fact that Django switched their default behavior in their documentation from Python
- 1:08:352 to Python 3 made a huge dent in the consumption of Python 3 on PyPI.
- 1:08:39And now I imagine this is just going to take it up a notch.
- 1:08:42That's great.
- 1:08:43Yeah, totally.
- 1:08:44I mean, I saw that.
- 1:08:45Like, I've been, you know, I have a couple of videos on YouTube on should I be using Python
- 1:08:492 or Python 3?
- 1:08:50And just a couple of months ago, you know, people were like, oh, yeah, this is a valid question.
- 1:08:55And now it's like every time I tweet this video, someone is like, there's no question
- 1:08:59about it.
- 1:08:59Like Python 3, like, don't go with Python 2.
- 1:09:02So that's great news, I think.
- 1:09:03I totally agree.
- 1:09:04I feel like the last six months or so it really has become like, why would you even ask this
- 1:09:08question?
- 1:09:08Like, this is sort of a deprecated video.
- 1:09:10You should just take it down.
- 1:09:11But that's really positive for the whole community.
- 1:09:14Awesome.
- 1:09:14All right.
- 1:09:15So final call to action.
- 1:09:16People can get your book.
- 1:09:17They can check out Python East Cafe.
- 1:09:19There's all sorts of stuff they can do to learn more about this, right?
- 1:09:21If you're listening to the show and you found interesting what Michael and I talked about,
- 1:09:25and you want to learn more and kind of go from beginner to intermediate Python skills and
- 1:09:29beyond without complex jargon and other crazy stuff, then you should download the free sample
- 1:09:36chapter for my book.
- 1:09:37You can just go to pythontricksbook.com or also just go to Amazon and search for Python
- 1:09:42tricks.
- 1:09:43And the book also comes with a free bonus toolkit where people can get more than two
- 1:09:48hours worth of video material to help reinforce some of the main lessons in the book.
- 1:09:53So yeah, go check it out at pythontricksbook.com.
- 1:09:56Awesome.
- 1:09:56All right, Dan, thanks for being here.
- 1:09:57It's fun, as always, to chat with you.
- 1:09:59Hey, thanks for having me on the show again.
- 1:10:00It was great.
- 1:10:01You bet.
- 1:10:01Bye.
- 1:10:03This has been another episode of Talk Python To Me.
- 1:10:06Today's guest has been Dan Bader, and this episode has been brought to you by Linode and
- 1:10:10Rollbar.
- 1:10:11Linode is bulletproof hosting for whatever you're building with Python.
- 1:10:15Get your four months free at talkpython.fm/linode.
- 1:10:19Just use the code Python17.
- 1:10:21Rollbar takes the pain out of errors.
- 1:10:24They give you the context and insight you need to quickly locate and fix errors that might have
- 1:10:30gone unnoticed until your users complain, of course.
- 1:10:32As Talk Python To Me listeners, track a ridiculous number of errors for free at
- 1:10:37rollbar.com slash talkpythontome.
- 1:10:39Are you or a colleague trying to learn Python?
- 1:10:42Have you tried books and videos that just left you bored by covering topics point by point?
- 1:10:46Well, check out my online course, Python Jumpstart by Building 10 Apps at
- 1:10:50talkpython.fm/course to experience a more engaging way to learn Python.
- 1:10:55And if you're looking for something a little more advanced,
- 1:10:58try my Write Pythonic Code course at talkpython.fm/pythonic.
- 1:11:02Be sure to subscribe to the show.
- 1:11:05Open your favorite podcatcher and search for Python.
- 1:11:07We should be right at the top.
- 1:11:09You can also find the iTunes feed at /itunes,
- 1:11:12Google Play feed at /play,
- 1:11:14and direct RSS feed at /rss on talkpython.fm.
- 1:11:18This is your host, Michael Kennedy.
- 1:11:20Thanks so much for listening.
- 1:11:21I really appreciate it.
- 1:11:22Now get out there and write some Python code.
- 1:11:24See you soon.
- 1:11:24I'll see you next time.