Latest / Talk Python To Me / #24: Fluent Python
Transcript
- 0:00Are you fluent in Python, or do you speak the language with an accent?
- 0:03Python's ease of learning can also lead to non-Pythonic patterns for even experienced developers.
- 0:08Luciano Romalo is here to give us a deeper understanding of this language we love.
- 0:12It's episode number 24, recorded Thursday, August 6, 2015.
- 0:17I'm a developer in many senses of the word, because I make these applications, but I also use these verbs to make this music.
- 0:28I construct it line by line, just like when I'm coding another software design.
- 0:33In both cases, it's about design patterns. Anyone can get the job done, it's the execution that matters.
- 0:39I have many interests, sometimes conflict.
- 0:42Welcome to Talk Python To Me, a weekly podcast on Python.
- 0:46The language, the libraries, the ecosystem, and the personalities.
- 0:49This is your host, Michael Kennedy. Follow me on Twitter where I'm @mkennedy.
- 0:53Keep up with the show and listen to past episodes at talkpython.fm.
- 0:57And follow the show on Twitter via at Talk Python.
- 1:00This episode is brought to you by Hired and CodeChip.
- 1:03Thank them for supporting the show on Twitter via at Hired underscore HQ and at CodeChip.
- 1:09Before we get to the conversation with Luciano, he generously agreed to give away an electronic copy of his book, Fluent Python.
- 1:15All you have to do to be eligible to win is to be a friend of the show.
- 1:19Visit talkpython.fm, choose friend of the show in the nav bar, and sign up.
- 1:24Now, let me introduce Luciano.
- 1:25Luciano Romalo is the author of Fluent Python.
- 1:28Romalo was a web developer before the Netscape IPO in 1995 and switched from Perl to Java and finally to Python in 1998.
- 1:36Since then, he's worked on some of the largest news portals in Brazil using Python.
- 1:40He's taught Python web development in the Brazilian media, banking, and government sectors.
- 1:45He's spoken multiple times at OSCON, PyCon, Python Brazil, FISL, and Rupy.
- 1:50Romalo is a fellow at the Python Software Foundation and a co-founder at the Garoa Hacker Club, the first hackerspace in Brazil.
- 1:58Finally, he's the managing partner at python.pro.br at Python Training Company.
- 2:03Luciano, welcome to the show.
- 2:06Thanks, Mike.
- 2:06Thanks for having me.
- 2:07Yeah, I'm really excited to talk to you about your new book, Fluent Python.
- 2:11Yeah, I'm excited about it too.
- 2:13I bet.
- 2:14You've been working on it for a while.
- 2:16Before we get into talking about your book, which is really an amazing book, I'll have more to say on that in a minute.
- 2:21Thanks.
- 2:22You're welcome.
- 2:23Let's talk about how you got started in the programming and Python at all.
- 2:27You know so much.
- 2:29I can tell from your book.
- 2:30But at some point, you had to start at the beginning, like we all do, right?
- 2:34Where did you get started?
- 2:36Okay.
- 2:36Now, I'm 51 years old.
- 2:39So when I was a teenager in Brazil, there were no microcomputers.
- 2:44But I got a hand on a programmable calculator, a TA-58.
- 2:51And I learned to program by programming it.
- 2:54And actually, my first interesting program was a port of a game from the HP-25 calculator to the TI-59.
- 3:02It was the lunar landing game that was famous.
- 3:06Oh, that was a great game.
- 3:07Yeah.
- 3:08So that was my first significant program.
- 3:12And then I was an exchange student in 81 in the US.
- 3:16I lived in Little Harrisburg, Illinois, population 10,000.
- 3:21And there I got – there were – in the library, in the school, there were a few Apple II computers that had just arrived when I arrived.
- 3:31And nobody knew how to use them.
- 3:33The teachers who were supposed to teach about them hadn't yet taken the course to do it.
- 3:40So they were basically free for anyone to do whatever they wanted.
- 3:43And I learned – I taught myself basic programming in them.
- 3:48Anyway, so that's a long time.
- 3:50And then I went through lots of languages after basic, Pascal.
- 3:54And Pascal was one of the most important ones for me.
- 3:58And then Delphi.
- 4:00And then Visual Basic.
- 4:02And I studied a little bit of small talk.
- 4:07And then when the web started here in Brazil, again, we had a kind of a different situation because there was – all telecom companies in Brazil were state-owned at that time.
- 4:19Later they were privatized, but they were state-owned in 94.
- 4:24And so I was hired by a large publishing company here to start developing their strategy for online.
- 4:30And I learned – I taught myself pro programming because everybody did everything on the web that was automated using Perl at the time.
- 4:37Right?
- 4:38Right.
- 4:39And I loved Perl.
- 4:41I thought it was awesome.
- 4:42But I also found it – well, I'm saying I just said I love it.
- 4:48But I loved it, but I also hated it at the same time because it was very powerful and quick.
- 4:53But it was also full of traps.
- 4:56And it was difficult to read afterwards and so on.
- 5:01Then I tried Java and I probably was one of the first people in Brazil to do server-side Java because when Java was first released, Sun was trying to market it as a tool for client-side programming, right, with the applets and so on.
- 5:18It took a while for them to realize that server-side was where Java was going to thrive.
- 5:24But I didn't enjoy the verbosity of Java.
- 5:27I liked the fact that it was really object-oriented, which Perl was not at the time.
- 5:32But it was too verbose.
- 5:34And then after a while, I went back to Perl again.
- 5:37And Perl had acquired object-oriented features in around Perl 5.
- 5:43And then something happened.
- 5:45As the Perl community was discussing how to do things in a dynamic language, but also in an object-oriented way,
- 5:54they kept repeating the sentence.
- 5:58Here's how they do it in Python.
- 6:00In Python, this is like that.
- 6:03And that's how I first heard of Python.
- 6:05It was discussions on the Perl mailing list about how to do things in an object-oriented way,
- 6:13but in a language that was a scripting language, in a dynamic language.
- 6:16So after reading mentions of Python a few times, I decided to go study it.
- 6:20And then I read the tutorial, which was shorter at the time than it is now.
- 6:25And I just fell in love immediately because for me, it had the best features of Python, of Java and Perl.
- 6:35It was really object-oriented with an object-oriented standard library like Java and had exceptions and other things that I liked about Java,
- 6:47but was not verbose.
- 6:49So it was, you know, very concise and to the point and agile like Perl.
- 6:57And so it was for me the combination of the best features of both languages.
- 7:02And then I basically never looked back.
- 7:04That's a great story.
- 7:06I think we've all had those sorts of feelings about some of our first programming languages.
- 7:12We love them because we really got into programming, but, you know, they're not necessarily the best.
- 7:17So you have those words as well, right?
- 7:19Exactly.
- 7:20But Python's where you've sort of settled into.
- 7:24I can tell you've been doing it for a long time from your book.
- 7:26Yeah.
- 7:28So I started doing it in 98.
- 7:30That was when I found the tutorial that I mentioned.
- 7:33And then I immediately pitched a Python solution to a client at the time for an online news site.
- 7:43And then I discovered Zope, which was just then released as open source.
- 7:51And I started using it like a couple weeks after it was released as open source.
- 7:58And then a couple months later, we actually launched in Brazil a new site that was based on Zope.
- 8:05And it was one of the first big Zope cases around the world.
- 8:09Oh, that's excellent.
- 8:11Yeah.
- 8:11And so I owe a lot to Zope.
- 8:13I don't use Zope anymore these days, but I owe a lot to it because it was because of Zope that I was able to deliver the kinds of products that the clients were looking for me to deliver, like content management systems.
- 8:29And so I was able to get paid for writing Python pretty early, at least in the context of Brazil, since 98.
- 8:39That's a great feeling, right?
- 8:40Yeah.
- 8:41You find this thing you love and, wow, people pay me to do it.
- 8:43Exactly.
- 8:44Exactly.
- 8:45It's a good time to be a programmer indeed.
- 8:47So let's talk a little bit about your book.
- 8:49The title is Fluent Python.
- 8:51And it's coming out under, being published by O'Reilly.
- 8:54And it's coming out, I think, September 4th is what Amazon.com tells me anyway.
- 8:58Is that right?
- 9:00Yeah.
- 9:00To be honest, I don't know exactly when the print book is going to come out because different pages that I look at give different dates.
- 9:06I've seen August.
- 9:07I've seen September.
- 9:08And I've seen October all in the last two weeks.
- 9:11So, yeah.
- 9:14But soon, right?
- 9:15Pretty soon.
- 9:16Just about the same time that this podcast comes out, I think, should be the time that the book is released.
- 9:20Wow.
- 9:21That's excellent.
- 9:22But let me say, the e-book is now complete in the final form.
- 9:29So people who buy the e-book or who have bought it previously can now download the final version of, you know, first edition, first revision.
- 9:40And it's also available from Amazon.com for the Kindle in the final form.
- 9:47So we are only waiting for the print book at this time.
- 9:50Right.
- 9:51Okay.
- 9:51Excellent.
- 9:52Yeah.
- 9:52I think having the electronic book works really well.
- 9:55I was reading it on my Kindle and it, on my Kindle, paper white, and it was really nice.
- 9:59You know, the code samples came through really well.
- 10:02Like sometimes that doesn't work so well.
- 10:04Wow.
- 10:04That's cool.
- 10:05Nice to know.
- 10:05Yeah.
- 10:06Yeah.
- 10:06It came out really, really well.
- 10:07And, you know, so I've only had the book for a few days.
- 10:11So I've only been able to read maybe the first five chapters.
- 10:14But my impression of the first five chapters, if it keeps going like this, I'm sure it does, is this book is a masterpiece.
- 10:22It is really, really good.
- 10:25Well, thank you very much.
- 10:26I think it's going to go down as a classic book in the Python community.
- 10:29Really.
- 10:30Thank you very much.
- 10:31Yeah.
- 10:31I'm not just saying that because you're on the show.
- 10:33But I was reading it and it's a little hard for me to put in words, I think, for people to really understand.
- 10:41But it seemed like everything that you covered, and we'll talk a bit more about that.
- 10:46But it seemed like everything you covered, I'm kind of like, I mostly know this.
- 10:49Oh, wait.
- 10:50There's this really cool piece of information or motivation that I was never really aware of.
- 10:57And just underneath the surface of the knowledge that I do have.
- 11:02Right.
- 11:02And that was like all the time happening to me while I was reading your book.
- 11:06And so I think it's a great contribution.
- 11:08Thank you.
- 11:09Yeah.
- 11:10So let me read a quick quote right from the beginning to give people a sense of, I think, what you're going for with this book.
- 11:17So in the introduction or one of the first chapters, you say, Python is an easy-to-learn, powerful programming language.
- 11:24Those are the first words of the official Python tutorial.
- 11:26That's true, but there's a catch.
- 11:28Because the language is so easy to learn and put to use, many practicing Python programmers leverage only a fraction of its powerful features.
- 11:35Does that kind of sum up the problem you're trying to solve with this book?
- 11:40Exactly.
- 11:41And that's why I called it Fluent Python.
- 11:42You know, as somebody who is fluent in two languages, in Portuguese and English, and that knows how difficult it is to become fluent in a language,
- 11:52because it's easy to become fluent in the language in your native language, right?
- 11:56You grow with it, and that's basically people don't even notice.
- 12:01But getting fluent in another language is hard.
- 12:05And so I've been teaching Python to lots of people.
- 12:11When I had a company, whenever we hired people, we never could hire people that knew Python because nobody knew Python at the time.
- 12:18So we hired people who knew Java and then taught them Python.
- 12:22But I also worked as a Python instructor, and that's what I do now these days.
- 12:28So I realized that this thing that I say over there, that the language is easy to use, it's practical.
- 12:36So people start solving real problems real fast.
- 12:40But that means that sometimes they are already using it for several years and don't know why some things are the way they are.
- 12:47And another thing that I say right after that in the second paragraph is that when you're learning a new language,
- 12:57you look for things that are familiar to you.
- 13:01Like, for instance, maybe this language, probably this language has a way to use a regular expression.
- 13:08Let's see how that works.
- 13:09But if there is something in the language that is not matched by something that you know from another language,
- 13:17like, for instance, the concept of generators that many languages don't have,
- 13:22then you're unlikely to go look for it, right?
- 13:25Because you don't even know the thing exists.
- 13:27Another simpler example than generators is like tuple unpacking.
- 13:32It's just a nice synthetic sugar.
- 13:35But if you've never seen that in a language, you won't look for it.
- 13:39And maybe you're going to spend years working with Python without ever using it.
- 13:43But it's a really nice thing to use because it makes code more readable, even safer in some ways.
- 13:49Python tuple unpacking has this characteristic because it actually forces the interpreter to count the number of items.
- 13:59So if there's one more or one less, an error will be raised.
- 14:03So it's a very nice feature, but it's something that people who come from other languages may never use because they won't think about it.
- 14:10Right.
- 14:10If you came from something like C#, for example, that doesn't have this concept of tuple unpacking,
- 14:15and so you just wouldn't even, you might not ever find it.
- 14:19And that kind of gets at the broader issue or concept of Pythonic versus non-Pythonic code, right?
- 14:29Yes.
- 14:29Yes.
- 14:30It's pretty easy to come over to Python and learn it really quickly so that you can do the same thing that you do.
- 14:37But it's not always the best, is it?
- 14:39Some of your mental models don't really carry over so well.
- 14:42Exactly.
- 14:43Yeah.
- 14:43Yeah.
- 14:44It's very, it's, of course, it's a very elusive thing to define what is Pythonic.
- 14:51I actually mentioned it several times in the book and tried to address it afterwards.
- 14:58But basically, I just send people links to other people that I respect a lot in the Python community who have attempted to define it directly.
- 15:11Right.
- 15:11But it's like, it's about a good definition which doesn't really explain what is Pythonic is to say it's idiomatic Python, right?
- 15:20Right.
- 15:21But okay, so what is idiomatic Python?
- 15:23Right?
- 15:24It's hard to define.
- 15:27Yeah.
- 15:28I think your best bet is to show a bunch of people a bunch of examples, say, all of these things are Pythonic.
- 15:34All of these things are not Pythonic because they carry over concepts that work well in other languages but are not the proper way to do them here.
- 15:44And once you get enough experience of going, bad example, good example, then you kind of get this sense, like almost a smell for like, yeah, it seems like this is right.
- 15:54It seems like that's not right.
- 15:55But it's very hard to be very concrete.
- 15:57Exactly.
- 15:58Yeah.
- 15:59Yeah.
- 15:59I tried to do that in the book.
- 16:01And actually, a decision that I made early on was to be as Pythonic as I could in the program examples, even when being Pythonic might not have provided the clearest source code from something that's not familiar with the language.
- 16:20You know?
- 16:21Like, for instance, right in the first example in the book, I use named tuple, right?
- 16:29Yep.
- 16:29And then I give it just a couple sentences to explain.
- 16:34It's probably not enough if the person has never seen it, but it's actually something useful that I start using all over the book.
- 16:41And if that picks up the person's curiosity, then the person can go and look for it.
- 16:48So this is what I decided.
- 16:50Like, for instance, before I actually have a chapter that formally discusses generated expressions, I use them in the book whenever it was natural to do so.
- 16:59Even before I actually cover it.
- 17:02Because of what I just said.
- 17:03You know, I wanted to write idiomatic Python whenever I could, even if that made the...
- 17:09Because the thing is, when you're speaking to a native speaker of English, for example, the person won't dumb down his or her speech for you, right?
- 17:21That's right.
- 17:22And that's cool.
- 17:23Sometimes maybe you don't understand everything, but you're catching, you know, there's something new about the way this person is expressing herself.
- 17:33And then you're going to try and find out what that means.
- 17:37That's, you know, if you always talk to people that spoke English, easy English, you would only learn easy English and not real English, fluent English.
- 17:49You wouldn't get that chance to grow into a fluent speaker, kind of like you said.
- 17:53So your example with the name tuple kind of hits on the point that I was talking about.
- 17:58Like, I've used name tuples.
- 18:00I'm familiar with them.
- 18:01I think they're great.
- 18:02But, you know, you go a little bit deeper and you're like, hey, it's really cool to use a name tuple because the actual dictionary that stores the attributes is stored in the class object, which is a singleton in the runtime.
- 18:16Not in the individual instances.
- 18:17So storage is actually cheaper than regular custom classes.
- 18:21Yes.
- 18:21So if all you want to have is just a bunch of pieces of data that have names in an object, but not functionality, maybe you should use these name tuples.
- 18:31This episode is brought to you by Hired.
- 18:44Hired is a two-sided, curated marketplace that connects the world's knowledge workers to the best opportunities.
- 18:50Each offer you receive has salary and equity presented right up front, and you can view the offers to accept or reject them before you even talk to the company.
- 18:59Typically, candidates receive five or more offers in just the first week, and there are no obligations.
- 19:05Ever.
- 19:05Sounds pretty awesome, doesn't it?
- 19:07Well, did I mention there's a signing bonus?
- 19:10Everyone who accepts a job from Hired gets a $2,000 signing bonus, and as Talk Python listeners, it gets way sweeter.
- 19:18Use the link Hired.com slash Talk Python To Me, and Hired will double the signing bonus to $4,000.
- 19:25Opportunities knocking.
- 19:27Visit Hired.com slash Talk Python To Me and answer the call.
- 19:31Exactly.
- 19:41Yeah.
- 19:42And another thing.
- 19:43They are, because they are inexpensive, you can use it.
- 19:47And they are inexpensive and they are completely compatible with tuples, right?
- 19:52You can unpack them, you can iterate over the items and so on.
- 19:55So, for instance, whenever you need to return more than one value from a function, I recommend you return a name tuple.
- 20:03Because it will make, for instance, understanding what the function does easier, because there's going to be the definition of this name tuple.
- 20:11If the person is looking at the result of the function in the console or in the debugger, the person is going to actually see the names of the parts of the tuple that the function is returning.
- 20:24So, this is a recommendation.
- 20:26Like, for instance, if you're going to return more than one value from a function, usually people do it with a tuple, and that's fine.
- 20:34But with the name tuple is even better.
- 20:36And it's cheap, like you said, because of the way it's implemented.
- 20:40It doesn't require more memory.
- 20:44Yeah.
- 20:45You don't have to store all the keys, copies of the keys for every instance, right?
- 20:49Like you would with regular classes or objects.
- 20:52Yeah.
- 20:53And, of course, when those come back, if you're unsure what's coming back from that method, you could print it out and you get a nice listing of actually not just what's in there, but the meaning of them by the name.
- 21:02Exactly.
- 21:03Exactly.
- 21:03Or if you're in a debugger like PyCharm, you could probably hover over it and actually see it pop up in the watch window.
- 21:10Yeah, it's excellent.
- 21:11Yeah.
- 21:11Yeah.
- 21:12So, one thing that you said you used in your book is something called Doctest to check the code in the console listings.
- 21:18Can you talk about that a little bit?
- 21:20Yeah.
- 21:20So, that's something that I think was created by the people in the ZOOP community.
- 21:28And it's not...
- 21:32So, in the ZOOP community, this was always very big.
- 21:35So, the main idea of Doctest is to make sure that whatever code snippets appear in documentation, particularly listings of console sessions demonstrating stuff, that those actually match the behavior of the program at that time.
- 21:56So, this was the starting point for Doctest.
- 22:01In the ZOOP community, which is still active, there's a lot of development going on still in ZOOP.
- 22:07There's a lot of also files that are testing files that are not strictly documentation that are written in the Doctest format.
- 22:18Because it's really easy to...
- 22:19Because it's really easy to...
- 22:20I find it excellent to...
- 22:22When I have to design a new API.
- 22:24Because I...
- 22:27It's sort of a BDD thing, you know?
- 22:30You start typing those fake console sessions where you're thinking how it would feel to interact with this API.
- 22:41And then the result would look like that and so on.
- 22:44And then you...
- 22:46It's really good to do TDD.
- 22:49Because you write a couple of stanzas of Doctest and then you implement the code that makes it pass and so on and on and on.
- 22:59You know, the whole idea of trying to envision what your API looks like from the outside and then implement it generates much cleaner APIs in the end.
- 23:10And this is, you know, one flavor of that, right?
- 23:12That's cool.
- 23:12Yeah.
- 23:14One thing, though, I have to say about Doctest is that after a while you'll realize there are some limitations to it.
- 23:20And even recently, like last week, I was...
- 23:24I had to come up with a difficult solution to an algorithmic problem.
- 23:29And I started implementing some tests using Doctests.
- 23:34But then, for instance, Doctest doesn't let you easily run just one part of the test.
- 23:40It runs everything from top to down.
- 23:43And sometimes when you're debugging, it's useful to be able to run just a specific part.
- 23:48Anyway, the unit testing frameworks, not only unit tests, but py tests, which is my favorite, are much more powerful than Doctests.
- 23:59And they can actually also run Doctests.
- 24:02So there is a way to integrate them.
- 24:04So you can have...
- 24:05I think the best idea these days is to really use Doctests just for the documentation.
- 24:13And just the straightforward cases, not corner cases, not weird outcomes, not testing whether an exception is raised or not.
- 24:23Because although all of that can be done in Doctests, in the end, it becomes difficult to maintain the tests.
- 24:30Right.
- 24:30Maybe it's better done in pytest, something like that.
- 24:32Exactly.
- 24:33Yeah.
- 24:33I use Py.Test.
- 24:35I prefer it much better than unit tests because I think unit tests has a very strong Java flavor of forcing you to do everything inside classes.
- 24:44Mm-hmm.
- 24:44And I much prefer the syntax that pytest introduced and the nodes also supports.
- 24:50But I prefer Py.Test of having tests as functions.
- 24:54And you can create classes if you want, but you can also have module level things.
- 24:58I prefer the syntax.
- 25:01Yeah, cool.
- 25:03So one of the very first things that you talk about in your book is the Python data model.
- 25:08And you talk about it's sort of the foundation for making your own classes fit this Pythonic programming model.
- 25:17And you have a quote in there.
- 25:18It's something like the – you know, talking about this data model or just learning about it is the tip of the iceberg.
- 25:25So the iceberg is called the Python data model, and it describes the API that you can use to make your own objects play well with the most idiomatic language features.
- 25:33Yes.
- 25:34Mm-hmm.
- 25:35Do you think people are – people who are sort of come to Python from other languages, you know, ones that add functionality to their classes very much by overriding virtual methods and those kinds of things sort of miss out on some of the Python data model, like under-under init or, you know, the iter or str methods, things like that?
- 25:57Yeah, for instance, this is something that happened to me many years ago when I developed the French deck, the card deck example that I use in that chapter.
- 26:08So many years ago when I had the idea of implementing that, so I was – the idea was to show how easy it was to create a new class that behaved like a sequence, right?
- 26:25So you implement dunder lan and you implement dunder get item and basically you are done.
- 26:31But then something really interesting happened because I was implementing – the idea was to implement a card deck, right?
- 26:37So what is an obvious thing you want to do with a card deck is to shuffle it, right?
- 26:41Right.
- 26:42So the first time that I created this example, I actually had a method called shuffle, which was not a special method.
- 26:50It was not dunder shuffle.
- 26:51It was just shuffle, right?
- 26:53So the idea was – so you have this card deck and you can look at the cards and you can call the shuffle method and see the cards shuffle.
- 27:00Okay.
- 27:01But then a few weeks after that, I realized that when I looked back at the example given a class, I said, wait, wait a minute.
- 27:14There is already a shuffle function in the random module that shuffles any mutable sequence that you give it.
- 27:27So it actually doesn't make sense to implement the shuffle.
- 27:31If the whole point of my exercise was to show how easy it was to create a sequence, even if the sequence represented a card deck, a very specific domain, specific thing, it didn't make sense for me to implement my own shuffle method.
- 27:47Because there was already a shuffle method and it is designed to work with mutable sequences.
- 27:54Right.
- 27:55Right.
- 27:55And it's already tested, probably optimized, all that kind of stuff, right?
- 27:58Exactly.
- 27:59So then that was an insight for me of an advantage of being Pythonic.
- 28:05It's the fact that when you are Pythonic, your objects suddenly become more compatible with things that are already out there in the standard library and in other packages that you can download from PyPI.
- 28:19Yeah, I think this is a lesson that people need to learn.
- 28:24Because, for instance, I don't know about C#.
- 28:29I studied Java a lot for a while.
- 28:31And even today, sometimes I spend some time every year I try to sit down and read something about Java.
- 28:39Because I get a lot of clients in my course that studied Java before and it's useful to make analogies.
- 28:47But, for instance, a big advantage of Python over Java is the fact that we have these operators that are more generic.
- 28:55Like, for instance, the square bracket operator, which is the operator to get an item or to set an item, right?
- 29:03And they don't have that in Java.
- 29:07That operator in Java is a magical thing that only works with the primitive arrays.
- 29:14Anything else, you have to use some other, you know, you have to use method syntax.
- 29:20Also, these days I'm studying Go, the Go language, and I really like it very much.
- 29:27But one of the things that I most dislike about it is the fact that it follows this Java philosophy of having some built-in data structures that are magical.
- 29:38And you cannot create your own data structures that emulate them precisely.
- 29:43Like, for instance, in Go, there's a way to...
- 29:48You can iterate over some primitive types, like maps and arrays and slices.
- 29:55But you cannot create your own type that is iterable using the for-range construct of the language.
- 30:02Right.
- 30:03That's too bad.
- 30:04Unlike the for-in-loop in Python, which is totally accessible.
- 30:08Yeah, so I think Java, I think Python is actually better than most languages in this regard, of enabling you to create structures that behave exactly like the most fundamental data structures in the language.
- 30:22And the way to accomplish that is through the data model.
- 30:25So, I think it's very nice to have that documented and exposed, and I wanted to give it more visibility.
- 30:33So, that's why I started the first chapter with that.
- 30:35Even if in the following chapters, my strategy then is to say, okay, but let's look at what is already implemented in Python.
- 30:44Because I didn't want people to go out and reimplementing new sequence types when maybe they don't even leverage everything that the built-in sequence types can offer.
- 30:55Right?
- 30:56Right.
- 30:56I think that's a good point.
- 30:57People coming from other languages, they don't necessarily know what's available.
- 31:01So, they'll find themselves reimplementing stuff.
- 31:03A really simple example would be I would like to randomly get an item out of a list.
- 31:09So, I might import the random module, get a random index based on the length, and then go index it out.
- 31:14Or you could just do random.choice.
- 31:16Oh, I didn't know random.choice exists.
- 31:18Exactly.
- 31:19Yeah.
- 31:19Yeah.
- 31:20So, one thing that you talk about around the Python data model is the len method.
- 31:25Mm-hmm.
- 31:26Yeah.
- 31:27And, you know, being such an object-oriented language, I always felt that it should be, you know, collection.len or length or something like that, maybe even as a property.
- 31:38And this sort of special function on the outside of the classes seemed a little bit odd.
- 31:43But you had a good conversation around that in there.
- 31:46You want to elaborate on that?
- 31:47Yeah.
- 31:48Sure.
- 31:48So, that is also something that bothered me for a while.
- 31:52Of course, you get used to it because it's really easy to get used to it and it's really easy to type.
- 31:58It's actually one of the arguments that Guido uses to defend that decision is that it's easier to type than having a dot.
- 32:05You know, if it was a method call, you would have to write, like, you know, x.len open close paren.
- 32:14Right?
- 32:15So, there's one dot extra there.
- 32:19And it's also something.
- 32:21And here's another thing that's funny.
- 32:23Because I think it's of the human nature.
- 32:28When people come to learn a new language, sometimes they are not pleased.
- 32:33Sometimes their boss told them to learn it.
- 32:35Right?
- 32:37And then, some people try to resist by pointing out, you know, things that they don't like or things that they think are inconsistent.
- 32:46And bugs, although bugs are extremely difficult for a beginner to find in Python.
- 32:52I've never seen a beginner find a bug in Python.
- 32:55Every time a beginner thinks…
- 32:56I've seen them write bugs.
- 32:57Yeah, yeah.
- 32:58Sure.
- 32:58Yeah.
- 33:00But most of the time, in my life, every time I've seen a beginner complain about a Python bug in a mailing list, it was invariably because the person didn't understand what exactly was going on.
- 33:11That's right.
- 33:11It was not a real bug.
- 33:12But anyway, here's about inconsistencies.
- 33:15So, they say, ah, this is inconsistent because it should be spelled as a method.
- 33:21Okay.
- 33:22Now, let's think about how this problem is solved in Java.
- 33:26In Java, there's actually an inconsistency because there is the… in the array type, there is like a… a property, as it were.
- 33:37It's actually a field, like a public field that you read.
- 33:41So, you don't… you just write myarray.length without parentheses, and that's how you get the length of the array.
- 33:50Because Java is just basically giving you the value that's… it's internally stored in a structure that represents the array.
- 33:58So, that's very cheap, and that's how they do it.
- 34:01And it's important to be cheap because it's a very common operation.
- 34:05Then, for other types that don't have this structure, there's a method called length.
- 34:12But also, in the Java API, there are other classes that could perfectly have methods named length, but have methods named size.
- 34:21Size or count or something, yeah.
- 34:23Yeah.
- 34:24So, there's at least three different ways that are common in Java code to do the same thing.
- 34:32And in Python, we have only one way.
- 34:34It's the length of the thing, called as a function.
- 34:37And so, first of all, it's more consistent.
- 34:41The second thing is, it's pragmatic.
- 34:44Because the way it works is, if you say length of X, and X is a built-in type, this built-in type, if the built-in type has many elements,
- 34:56there's actually a struct, I forgot the name, but it's spelled out in the book,
- 35:01there's actually a C struct that represents the type, and it has a field that has the count of the numbers, of the number of items.
- 35:08So, that's the same thing that Java does, except that it doesn't expose a special field, but uses the length function.
- 35:15So, the length function, the implementation of the length function in the Python interpreter does that.
- 35:20It just goes to look at, if there is a built-in type, and it's a multi-valued built-in type,
- 35:26then return the value of this field in the struct.
- 35:29So, that's very cheap, and it's actually faster than doing the resolution of an attribute, right?
- 35:36So, because if you had to write X dot length, this dot requires some advanced machinery in Python,
- 35:43because of the dynamic nature of Python.
- 35:45The price we pay for all this dynamic behavior is that the dot operator is kind of expensive.
- 35:52Right.
- 35:52Basically, a dictionary lookup, and then the function calls themselves are also kind of expensive.
- 35:57Exactly.
- 35:58Yeah, and it's more expensive than a dictionary lookup, because there's all that machinery that I explain later in the book.
- 36:08Basically, in the last part of the book, the metaprogramming part, is where I explain actually how descriptors work.
- 36:16And descriptors, the infrastructure below properties, but also below another thing that's really fundamental that everybody uses in Python,
- 36:27which are methods.
- 36:28Because what makes a method behave as a method, as an instance method, as a bound method, or as an unbound method,
- 36:38has to do with the way the descriptor mechanism resolves the dot operator.
- 36:45So, it's an expensive thing.
- 36:47So, by making it be written as len paren X, close paren, the interpreter can resolve this much faster.
- 36:58And then, if the structure is not a built-in structure, then the interpreter falls back to looking up,
- 37:06and then there's the cost of the attribute lookup, but it will look up the under len method.
- 37:15So, I think this is actually very elegant, because in the end, the user interface is always the same.
- 37:21It's always len of X.
- 37:22But it allows, but it gives us, creators of APIs, a way to provide a consistent interface.
- 37:30An interface that's consistent with the built-in method, with the built-in types,
- 37:35while at the same time ensuring that the built-in types have the fastest possible performance for doing this operation,
- 37:42which is crucial, right?
- 37:45It's often something done in loops, right?
- 37:47And you want to optimize everything that's done in a loop.
- 37:51So, getting the lane of something is important to be fast.
- 37:54This episode is brought to you by Codeship.
- 38:11Codeship has launched organizations, create teams, set permissions for specific team members,
- 38:16and improve collaboration in your continuous delivery workflow.
- 38:19Maintain centralized control over your organization's projects and teams with Codeship's new organizations plan.
- 38:25And, as Talk Python listeners, you can save 20% off any premium plan for the next three months.
- 38:31Just use the code TALKPYTHON, all caps, no spaces.
- 38:35Check them out at CodeChip.com and tell them thanks for supporting the show on Twitter where they're at, CodeChip.
- 38:45Yeah, that's excellent.
- 38:46And you talked a lot about the internals, which I think can be kind of tricky to discover in Python.
- 38:52The previous show that I recorded but is not yet released is with Philip Guao, all about this university class he did and then put online on the internals of Cpython.
- 39:05I think he did a 10-hour series on like walking through Cpython.
- 39:08And I just –
- 39:10I want to watch that.
- 39:11Yeah, I just want to recommend to listeners like if that kind of stuff that we were just talking about sounds a little like, I'm not really sure where this is going.
- 39:18Like watch Philip's videos, right?
- 39:20Maybe I'll put them in the links here because it'll all of a sudden click together and be like, oh, I see, exactly.
- 39:25And you talk a little bit about the same kinds of things that he does by importing the disassembly module, the dis module, and disassembling functions and actually looking at what it is you're talking about to see what's really happening.
- 39:37Yeah.
- 39:38The thing is I'm not – to be honest, I'm not an authority on the internals of Python.
- 39:46But sometimes I was curious about some things and I went to look for it.
- 39:50But I also want to tell your listeners that the book is not about that kind of thing.
- 39:56It has some moments that has these kinds of look into, you know, under the covers.
- 40:03But most of the time it's not like that.
- 40:05It's something that I enjoy, but I also found it challenging to read the source code for Cpython.
- 40:15So I would love to see Python developing – here's a wish that I have.
- 40:21I would love to see some time in the future, perhaps, a Python interpreter written in Go.
- 40:29Because Go is much more readable than C.
- 40:32It's simpler also.
- 40:34The syntax is simpler.
- 40:35But it has the problem of concurrency resolved in a very good way.
- 40:41And it would be awesome to see a Python interpreter written in Go.
- 40:45It would make it also much easier to understand what's going on.
- 40:48Yeah.
- 40:49How about something – a future Python that the runtime parts are easier to read because it's Go.
- 40:56And it doesn't have the global interpreter lock.
- 40:58Exactly.
- 40:59Maybe because you can somehow resolve that with simpler threading models in Go.
- 41:03Yeah.
- 41:04It's important to – since you mentioned the global interpreter lock, you know, the JITON and the Iron Python implementations of Python don't have it.
- 41:14Because they already can count on an underlying implementation of objects and threads that is thread safe.
- 41:21So it's something that happens with CPython and also with PyPI.
- 41:28But Iron Python and JITON don't have the global interpreter lock.
- 41:32Oh, that's interesting.
- 41:33Yeah.
- 41:34So basically there's two cases of this is a possible thing.
- 41:38It's just we're not there yet, right?
- 41:40Yeah.
- 41:41No.
- 41:41Not at all.
- 41:42Yeah.
- 41:45Let's move on maybe to like the array section and the sequences.
- 41:48You did some really interesting things there, a lot of which was kind of familiar to me like list comprehensions and generator expressions.
- 41:58Those are –
- 41:59Yeah.
- 41:59You know, if you've been doing Python for a while, it'll make a lot of sense.
- 42:02But then you also have like little gems in there.
- 42:04Like one of them that really stuck with me is said basically if you implement the iter method, you can automatically do iteration.
- 42:14But if the collection you have doesn't have a dunder contains, then any sort of is check will actually be a sequential scan.
- 42:23An in check, not is.
- 42:26Yeah.
- 42:26Oh, sorry.
- 42:27Yes, of course.
- 42:27Yes, an in check.
- 42:28So if you say object in sequence and it does – it implements iteration but not the contains, all of a sudden – it can still answer the question, but it's a huge performance problem potentially.
- 42:39Yeah.
- 42:40Yeah.
- 42:41And that's because if you look on – right at the second page of chapter two, there's a diagram of the – of the mutable sequence and sequence ABCs and then container iterable and sized.
- 42:56And so those ABCs pretty much define the standard API for mutable sequences.
- 43:02And in the sequence ABC, contains is a concrete method.
- 43:08So even though that's an abstract class, the only abstract method in it is dunder get item.
- 43:14But dunder contains is already implemented.
- 43:17And the way it's implemented is by just using iteration.
- 43:21Yeah.
- 43:22This is one thing that I – that – one thing that makes me really like Python is the fact that as a teacher, I found it easier to explain over the years.
- 43:33Although it has also grown more features that take a lot of time to explain.
- 43:38But for a certain specific set of features, for instance, if you're talking about sequences, these days it's easier to talk about sequences, I think, than it was before.
- 43:51Because although there's more content, you have to talk about comprehensions and so on.
- 43:55The basic concept of a sequence was not very well defined earlier in the language because there were no ABCs.
- 44:02In the documentation, you read about, oh, so this expects a sequence and that's – but there was no strict definition of what a sequence was.
- 44:11After a while –
- 44:12It's like a vague concept of, well, a sequence is kind of these – yeah, what does that actually mean precisely?
- 44:17Exactly.
- 44:17Yeah, exactly.
- 44:19So people who were comfortable with the language intuitively knew what a sequence was.
- 44:24People who knew about the implementation of the language in C knew exactly what a sequence was, but in a way that was very complicated to convey.
- 44:33And now with the introduction with the ABCs, it's easier because you can say, no, so here is what a sequence is.
- 44:40A sequence has to have a way of – it's a container, an iterable-sized container.
- 44:46So it has to be a way to – there's got to be a way to get the lane, to iterate over it and to determine whether something is or is not in it.
- 44:55And then it has to have a – so there's a definition that people can look up in the documentation.
- 45:03And in the book, I drew some UML diagrams, which I also think it's – they make it easier to see the relationship between those ABCs because in the documentation, there is no diagram.
- 45:15So it's all –
- 45:16Yeah, those pictures are really nice.
- 45:18I like them.
- 45:19Yeah.
- 45:20Yeah.
- 45:20So one thing I thought was really helpful, and I think probably I learned a decent amount from it, was when you're talking about this whole thing on data structures, you have this part that's called – that you entitled, you know, when a list is not the answer.
- 45:37Uh-huh.
- 45:37Yeah.
- 45:37So, like, you can almost always use a list for everything, and it does have a, you know, effectively a pop method and, you know, push is just append and things like that, right?
- 45:50So – but it's maybe not the best choice for that.
- 45:54So you talk about things like arrays.
- 45:57Yeah, exactly.
- 45:58And the DQ class or sequence for double-ended queues, and I thought that was really interesting.
- 46:05So maybe – do you want to just talk quickly about your message there?
- 46:09Yeah, so that was the idea, because it's something that I've always – not always, but I've already caught myself doing in the past, was to use a list when I should be using an array.
- 46:21An array is easy – it was really easy to use, and it's much more efficient in terms of memory, and also in terms of execution, because it's a packed data structure where one number is right after the other.
- 46:38Yeah, it might be worth just, like, expanding on that just a little bit, you know, because in normal Python, even a number, if I say x equals 7, that allocates under, you know, down in the runtime, like a full-on object, which has all sorts of additional information, and it's a pointer out to that thing.
- 46:58Exactly.
- 46:59Whereas array allocates, you know, literally four bytes for ints or whatever in a huge long array, right?
- 47:05Exactly.
- 47:06That's because – yeah, exactly.
- 47:07That's why when you create a – I think the first stumbling block to create an array is that you have to look up in the documentation, because when you create an array, you have to write this type code that tells the array what are the size of the numbers that are going to – the size and the type of the numbers that are going to be there, because it can also have an array of floats, right?
- 47:31So you have to look up in the documentation to remember what are the letters that you use to specify the numeric type that goes into the array.
- 47:40But then, when you do that, you have to look up in the array.
- 47:40But then, when you do that, you're all set.
- 47:42One thing that I try to do, like, for instance, in the discussion about arrays, there is that, and there's other places, is to compare – I wrote those tables with, like, all the methods and operations of a list and array.
- 47:58And so you can just briefly look and realize what the difference is.
- 48:03Oh, here's something that I can do with an array that I cannot do with a list or vice versa, right?
- 48:08So that's why I decided to add those tables, because there's a lot of functionality, and I wanted to pretty much graphically show people that they're almost the same.
- 48:20But there are some differences, and when you look at the tables, it's easy to tell really quickly what the differences are.
- 48:28Yeah, it was really helpful for me, so I appreciate that you added that.
- 48:32That's cool.
- 48:32Mm-hmm.
- 48:34So we're kind of coming up towards the end of the show.
- 48:36Maybe you want to touch on just a couple more points that you think are important parts of the book that maybe I haven't gotten to.
- 48:43Yeah.
- 48:44So, basically, the book is divided in six parts, right?
- 48:49We talked about the part one, which is just one chapter where I present the data model in a very introductory way.
- 48:58But the idea of the data model and demonstrate how useful it is with the card deck example, and then there's a vector class with operators.
- 49:07Anyway, and then the second part is data structures, the part that we were talking about.
- 49:13That part has a chapter.
- 49:16I think the longest chapters in the book are there.
- 49:20The one about all the sequence types, the one about dictionaries and sets, and the one about texts versus bytes.
- 49:27About the text versus bytes chapter, I was very happy because one of my favorite programming book authors, Bruce Echel, who wrote the famous Thinking in Java book.
- 49:39These days, he's doing some Scala writing, but also he loves Python, and he was reading my book, and he said that my chapter about text versus bytes, which covers Unicode, what Unicode means, and all the different ways of converting Unicode from and to bytes.
- 49:59He thought they were very useful for him.
- 50:02Anyway, so this is the data structures part of the book.
- 50:07Then part three is about functions as objects.
- 50:10I decided to introduce that before classes, also because I think it's a Pythonic way of thinking.
- 50:16We start by coding stuff in functions, and we usually only create classes when realized that just plain functions won't do.
- 50:26Right.
- 50:27Maybe some data needs to come along with those functions or something, right?
- 50:30Exactly, exactly.
- 50:32If you are trained in Java, you are trained to do everything in classes because that's the only way to do it.
- 50:39So that's why I decided in the book to give a total explanation of functions and design patterns using functions,
- 50:46how you can simplify some design patterns by getting rid of classes and just using functions.
- 50:53And then I wrap up with decorators and closures.
- 50:56And then part four is about object-oriented programming.
- 51:02It starts with an explanation of how references work and how the whole thing about mutability works.
- 51:10And then I give some examples of implementing what I call PyTonic objects,
- 51:16which are first a vector class, a 2D vector class, and then a two-dimensional vector class,
- 51:23and then a multi-dimensional vector class.
- 51:26And those examples are excuses to show the use of many special methods in practice.
- 51:33I thought your comment on how you don't like to call the Dunder methods magic methods,
- 51:42but special methods was interesting.
- 51:44You're like, there's nothing magic about these.
- 51:46This is how you do it.
- 51:47Exactly, exactly, exactly.
- 51:50And the thing is, when you call something magical, you're saying,
- 51:53okay, so this is not for mere mortals.
- 51:55But that's exactly the opposite.
- 51:57Like I was saying before about the criticism that I was criticizing Java and Go
- 52:06because of the fact that it has some data structures that are magical
- 52:11in the sense that you cannot really emulate them in your own code.
- 52:14And so, exactly.
- 52:18Anyway, so this is the part of the book where I explain how to do that.
- 52:22And also, there's some discussions about the use of interfaces.
- 52:28And something that's a first, and I'm very proud that Alex Martelli,
- 52:31who was one of the reviewers of the book, one of the tech reviewers of the book,
- 52:36wrote an essay introducing, after he introduced duck typing,
- 52:44at least the Wikipedia credits him with the creation of,
- 52:48or at least with the first use of the expression duck typing
- 52:52in the context of discussing programming languages.
- 52:56Alex Martelli now invented the concept of goose typing.
- 53:02Goose typing.
- 53:03Okay.
- 53:03What is goose typing?
- 53:06So, goose typing is a variation of duck typing where you have some type hints,
- 53:12this new feature of Python 3.5.
- 53:15But it doesn't require, actually, the new feature, the type hints.
- 53:19The idea is to use ABCs as a way to specify types.
- 53:27But here's the thing.
- 53:27Python uses ABCs in a way that's very influenced by its origins
- 53:35as a dynamic-typed language with duck typing.
- 53:39And the main reason for that is that it allows you to actually create a class
- 53:45that does not inherit from an ABC,
- 53:48but then you register it as a virtual subclass of that ABC.
- 53:53So, you're actually promising.
- 53:55I do implement that thing.
- 53:57Although I do not inherit from it, I do implement that.
- 54:00And you can actually do that after the fact, for instance.
- 54:04Maybe because it's a third-party library that implements something that you look at it
- 54:09and say, hey, this actually looks like a sequence,
- 54:12and it would be useful in my code to be able to ask if that's a sequence
- 54:17and to have the answer yes.
- 54:18So, in your own code, you can affirm that this other data structure
- 54:23that somebody else did is, in fact, a virtual subclass of a sequence.
- 54:28So, this is a facility that Python has with the way the ABCs were implemented.
- 54:34And there's this register method of ABCs
- 54:38that allow you to register some other type as a virtual subtype of this type.
- 54:43That's cool.
- 54:44Yeah.
- 54:45So, that's the idea of goose typing.
- 54:48It's a different kind of waterfall,
- 54:50but it's related to ducks in some way.
- 54:54Yes.
- 54:54Yeah.
- 54:55Nice.
- 54:57And then there is a chapter about inheritance
- 55:00where I discuss the multiple inheritance,
- 55:04which, again, is something that a lot of people don't have contact with
- 55:08because many OO languages don't implement that.
- 55:11And then there's the chapter about control flow.
- 55:14This was the hardest chapter for me to do.
- 55:18And there was a lot because, basically, because the AsyncIO was very new,
- 55:24still very new.
- 55:25Yeah.
- 55:26You have a lot of stuff on, basically, parallelism or async here.
- 55:30You've got concurrency with AsyncIO, concurrency with futures.
- 55:33What's the story with the futures part?
- 55:36Yeah.
- 55:36So, the thing is, there is a module called concurrent futures, right?
- 55:41And this module actually works with threads and processes under the covers.
- 55:48But it was the first module in the first package in the Python standard library
- 55:55that implemented this idea of futures,
- 55:58which is something that is gaining a lot of currency in other languages as well.
- 56:03So, futures are basically objects that represent a pending computation.
- 56:08So, the idea is that when you submit to some concurrent API a task to be done,
- 56:17usually you submit, please run this function, right?
- 56:21So, the API immediately and in a non-blocking way returns to you a future
- 56:28that represents that pending computation.
- 56:32And then you can query this future to know whether the thing that you asked to be done
- 56:37is actually done or not.
- 56:38And when it's done, you can actually get the results from the future as well.
- 56:43And another thing that the future does is,
- 56:46that is extremely useful whenever you are doing async work or threaded work,
- 56:53that it encapsulates some exception that was raised when the system was running that function that you asked it to run.
- 57:01And this is really important because what happens is,
- 57:04when you're running code asynchronously and there's an exception happens,
- 57:09it's happening in another context, right?
- 57:12So, how do you catch it?
- 57:14And it's really interesting.
- 57:16The way it works is, the future object catches it and stores it for you.
- 57:22And then later, when you actually go look at the future object,
- 57:25and for instance, if you request the result from the future,
- 57:28if instead of a result, an exception was raised,
- 57:32the exception is raised at this point in your program.
- 57:35So, it teleports, in a way, the exception that happened in this other context that you have,
- 57:42where you have no control,
- 57:43to a context where you actually either, you know,
- 57:46you want to look at the result of the computation,
- 57:48but actually there was an exception,
- 57:50and this is where the exception is raised,
- 57:52and you get to handle it.
- 57:54Yeah, that's excellent.
- 57:55Because normally you think of the exception as going up the call stack.
- 57:58Yeah, exactly.
- 57:59Until somebody catches it.
- 58:00But on the other thread that you didn't start or manage, right,
- 58:03that's not what you need.
- 58:05Exactly.
- 58:06Cool.
- 58:06Yeah.
- 58:07Awesome.
- 58:07And then the last section you have is on metaprogramming, right?
- 58:11Exactly.
- 58:12Yeah.
- 58:12So, the metaprogramming part is about how attributes actually work in Python.
- 58:19So, I start with some simple examples using the dundergetutter special method,
- 58:25for example, that allows you to emulate like a virtual attribute.
- 58:29And I use it, I have a real data stream from OSCON last year that had all the talks.
- 58:37So, it had a few thousand records in JSON format.
- 58:41And I use, I implement a few classes to make it easy to navigate through this OSCON data.
- 58:47And then, what else?
- 58:50Then I talk about properties.
- 58:53And then I go to, the next chapter is about descriptors, which is the infrastructure for properties.
- 58:58And then I developed an example that actually evolved from a talk that I gave at PyCon in 2013 in Santa Clara.
- 59:12It was called encapsulation with descriptors, the talk.
- 59:15But in the chapter, I can spend much more than 45 minutes with that.
- 59:19Of course.
- 59:20Or 30 minutes, actually, it was.
- 59:21Anyway.
- 59:22And the last chapter is about class metaprogramming.
- 59:25And I didn't call it metaclasses, because there's an area.
- 59:30Although metaclasses is the most famous way of doing metaprogramming with classes in Python and other languages that allow it.
- 59:40We also, we now have an easier way of doing metaprogramming with classes, which is using class decorators.
- 59:46So, then I show some examples with that and a real metaclass.
- 59:51And that wraps up the book.
- 59:54Yeah, that's really cool.
- 59:57There's a bunch of stuff that I'm looking forward to digging into if I have a few more days.
- 1:00:03You know, especially the concurrency looks really interesting.
- 1:00:05So, yeah, congratulations on your book.
- 1:00:09It's very well done, and people are going to get a lot out of it.
- 1:00:12Thanks a lot.
- 1:00:13Let me just mention one thing that I'm proud of in the concurrency part of the book, and actually the control flow part.
- 1:00:20In the cool routines chapter, I wanted to show people cool routines used in an interesting way, which was not linked to asynchronous programming, which is the main use case for them these days.
- 1:00:33And so, I decided to implement this discrete event simulation of taxi cabs.
- 1:00:39And so, this is something that, an example that I'm proud of, that I came up with this example that shows and gives you, when you run the simulation, if you actually run the script, it gives you an intuition of how actually you can use cool routines to manage many things happening at the same time.
- 1:00:56In the example, there are cab trips, different cabs that are on the street catching passengers and dropping off passengers.
- 1:01:05And so, but it doesn't involve an event loop, but it's a simulation.
- 1:01:11And so, there's a very simple scheduler implemented in about 10 lines of Python and makes all those cabs run.
- 1:01:19And actually, the cabs are implemented as co-routines.
- 1:01:24That sounds really cool.
- 1:01:25Definitely want to check that out.
- 1:01:27So, before I let you go, there's two questions I always ask my guests on the way out the door.
- 1:01:32First one is, what's your favorite code editor?
- 1:01:34I use Sublime Text.
- 1:01:37Sublime.
- 1:01:38That's a good one.
- 1:01:38Yeah.
- 1:01:39Although, I have a partner in my training company that uses PyCharm.
- 1:01:45And sometimes I get, I think I'm moving to PyCharm.
- 1:01:49You get sucked in there.
- 1:01:50Yeah.
- 1:01:51Yeah, exactly.
- 1:01:53I'm not an IDE person.
- 1:01:56Maybe someday I will be, but now I prefer simpler things and I'm very happy with Sublime Text.
- 1:02:02Yeah, excellent.
- 1:02:02And the other question is, what's your favorite PyPI package?
- 1:02:06You know, there's all these different things out there, 60,000 plus, whatever.
- 1:02:10Yeah.
- 1:02:11There's just so much to raise people's awareness of.
- 1:02:15So, what's your top list?
- 1:02:16Well, requests is probably a common answer.
- 1:02:21That's one of my favorites.
- 1:02:23Other people have maybe given you this answer.
- 1:02:26But I think it's really cool.
- 1:02:27And I actually use it in the book.
- 1:02:29It's one of the few external libraries that I use, even though there is a library that does pretty much the same in the standard library.
- 1:02:36And I use it because it's much simpler, but also because it runs the same on Python 2 and Python 3.
- 1:02:45But our HTTP client functionality in the standard library has always been not very good.
- 1:02:51And because it's not been very good, it's been changed a lot in Python 3 without getting much better, but enough to become incompatible.
- 1:03:03So, I think requests, if you want to write an HTTP client, is something that you really have to go for because then your code will be easier, nicer, and it will run in Python 2 and Python 3 at the same time.
- 1:03:22Yeah.
- 1:03:23The only thing that I wish requests had, and it doesn't, is support for asynchronous requests.
- 1:03:31One of the things that you learn in my book is that you can actually do very effective network code in Python, despite the GIL, with threads.
- 1:03:42Because of the fact that every single function in the standard library of Python that does blocking I.O. releases the GIL.
- 1:03:49So, if you test it, if you run it, you will see it.
- 1:03:53Actually, you know, except in very extreme cases, the performance of threaded applications and asynchronous applications in Python is pretty much the same.
- 1:04:04So, you can use requests with threads for doing very fast HTTP clients.
- 1:04:11Yeah, that's great.
- 1:04:12Because most of you are blocked waiting on the network anyway, right?
- 1:04:14Exactly.
- 1:04:15Exactly.
- 1:04:16Cool.
- 1:04:16Yeah, we had Kenneth Wright on the show, the sixth episode, and we talked a lot about requests.
- 1:04:22That was very cool.
- 1:04:23Yeah.
- 1:04:24I'm very, I was very glad.
- 1:04:26I think Kenneth, I don't know, Kenneth gave some manifestation that was positive about my book on Twitter.
- 1:04:34That maybe he favored somebody else's tweet that was positive about my book.
- 1:04:38But I was very proud of that because he's really an example of a guy who can do things in a very Pythonic way.
- 1:04:47If you want to learn what Pythonic is, study Kenneth's code.
- 1:04:51Yeah, he's very good at creating APIs.
- 1:04:53Yeah.
- 1:04:54Mm-hmm.
- 1:04:54All right.
- 1:04:56Any final shout-outs or call to actions before we go?
- 1:05:01People should go read your book.
- 1:05:02They should go check out your book, right?
- 1:05:04Yeah, sure.
- 1:05:05Yeah, it was a huge investment for me.
- 1:05:09I don't know if most of your readers know, but it's pretty impossible to make a living writing books, right?
- 1:05:15It's sort of like how to make a living playing guitar.
- 1:05:22Yes.
- 1:05:23There's about very few people in the world that do it, that manage that.
- 1:05:28Anyway, so I have to recover the investment in time that was huge.
- 1:05:33I spent 18 months writing this book.
- 1:05:36And the second half of that time was full-time doing it because my editor started complaining that it was, you know, I get, I was not feeling my...
- 1:05:48You weren't on the deadline.
- 1:05:50I was not in the schedule, yes.
- 1:05:52So there was a couple of postponements, but then she noticed that I was really writing a lot.
- 1:05:58The book grew.
- 1:05:59It started as a project for a 300 to 400 pages book.
- 1:06:04But I'm really happy with the result.
- 1:06:07I'm really proud.
- 1:06:08And I'm really happy with the chance that I had to work with Megan Blanchett, my editor, O'Reilly.
- 1:06:15She was awesome.
- 1:06:17And the support that I had from the technical editors, Alex Martelli, Anna Ravenscroft, Leonardo Rochael, and Leonard Regebru, and Victor Steiner, who was a special guest tech editor for the AsyncIO chapter.
- 1:06:32They were awesome.
- 1:06:34And it's really been a great experience.
- 1:06:36And I'm really happy to see how people are enjoying the book.
- 1:06:41Yeah.
- 1:06:42It's quite an investment, but hopefully it pays off for you because it's definitely good work.
- 1:06:46Yes.
- 1:06:47Thanks a lot.
- 1:06:48Yeah, you're welcome.
- 1:06:49Luciano, thanks for being on the show.
- 1:06:51Really appreciate it.
- 1:06:51Yeah.
- 1:06:52Thank you very much, Mike.
- 1:06:53You bet.
- 1:06:53Bye.
- 1:06:54Bye.
- 1:06:55This has been another episode of Talk Python To Me.
- 1:06:57Today's guest was Luciano Romalo, and this episode has been sponsored by Hired and Cochip.
- 1:07:02Thank you guys for supporting the show.
- 1:07:03Hired wants to help you find your next big thing.
- 1:07:06Visit Hired.com slash Talk Python To Me to get five or more offers with salary and equity presented right up front and a special listener signing bonus of $4,000.
- 1:07:14Cochip wants you to always keep shipping.
- 1:07:18Check them out at Cochip.com and thank them on Twitter via at Cochip.
- 1:07:21Don't forget the discount code for listeners.
- 1:07:23It's easy.
- 1:07:24Talk Python.
- 1:07:25All caps.
- 1:07:26No spaces.
- 1:07:26You can find the links from today's show at talkpython.fm/episodes slash show slash 24.
- 1:07:32Be sure to subscribe to the show.
- 1:07:34Open your favorite podcatcher and search for Python.
- 1:07:36We should be right at the top.
- 1:07:38You can also find the iTunes and direct RSS feeds in the footer of the website.
- 1:07:43Our theme music is Developers, Developers, Developers by Corey Smith, who goes by Smix.
- 1:07:48You can hear the entire song at talkpython.fm/home slash music.
- 1:07:52This is your host, Michael Kennedy.
- 1:07:54Thanks for listening.
- 1:07:55It's Mix.
- 1:07:56Take us out of here.
- 1:07:57He's dating with my voice.
- 1:07:59There's no norm that I can feel within.
- 1:08:01Haven't been sleeping.
- 1:08:02I've been using lots of rest.
- 1:08:03I'll pass the mic back to who rocked it best.
- 1:08:06I'll pass the mic back to who rocked it best.
- 1:08:19Thank you.