- Podcasts
- Python Bytes
- #135 macOS deprecates Python 2, will stop shipping it (eventually)
#135 macOS deprecates Python 2, will stop shipping it (eventually)
Python Bytes
- June 20, 2019
- 32 min
- Episode 135
About this episodeTopics covered in this episode: Why do Python lists let you += a tuple, when you can’t + a tuple? macOS deprecates Python 2, will stop…
- Why do Python lists let you += a tuple, when you can’t + a tuple?
- macOS deprecates Python 2, will stop shipping it (eventually)
- Pythonic Ways to Use Dictionaries
- Things you are probably not using in Python 3 But Should
- Have a time machine? C++ would get the Python 2 → 3 treatment too
- Extras
- Joke
Transcript
From the publisherThis transcript comes from the podcast’s own feed, shown as the publisher wrote it.
#135 macOS deprecates Python 2, will stop shipping it (eventually)
You're here. You've got your own podcast, and we're definitely going to talk a little bit about that at the end, and that'll be great. I do, before we kick things off, though, I also want to say thank you to DigitalOcean for sponsoring the show. Check them out at pythonbytes.fm/digitalocean. More about them later as well.
Okay, so Reuven Lerner, he's got an email list that I'm signed up to. One of the things that he pointed out in one of the recent, I can't remember if it was on his email list or if it was on one of his blog posts. So I'm linking to a blog post. But it's this idea that I didn't even know you could do this. It's titled, why do Python lists let you do plus equals or in place assign addition with a tuple? We can't do plus with a tuple. So here's the idea is you got you got a list, and you want to add, combine the elements of a list and a tuple end up with a list at the end. You can't just add them because they're different types. But you can do incremental add. And it's just weird. I was like, I didn't know you could do that. But it turns out it's a little surprising. But it just is because the plus equals is not, I always think of it as equivalent to like x equal x plus equals one is the same as x equals x plus one. That's what I thought. But it
turns out it's actually a different, it's a different call. It's implemented as a different call. I see like a different Dunder method, like a different magic method is being called. Yeah. And the Dunder method for list increment or in place add is what it does is it takes any iterable on the right hand side, and adds it one element at a time to the left side. So you can plus equals any iterable onto a list.
That's neat. I didn't know you could do that. That's cool. This is so weird. I mean, it's cool. And it's it's fine that plus equals will do that. And I actually just tried it. I created a list of a bunch of numbers. And then I plus equaled it with a string. And now I have a list that's full of numbers and single characters that made up that string, which is, which is, it's okay, if that's the behavior, and it seems actually like the most reasonable behavior. But then why can't you just plus them, right? Like, it seems odd that it's inconsistent. Plus, it seems like it should just create another list with that same behavior.
So yeah, not sure. Yeah, exactly. I assume there's a better way to turn a tuple into a list. But now that I'm looking at this, you could just create an empty list and then do plus equals your tuple. There you go. Your conversion. It's all it's all filled up. Yeah, it's definitely not the most obvious way. Yeah, probably the initializer constructor of the list is best just pass it the iterable. But yeah, this is interesting. Also, if the three of us didn't know you could do this, probably don't do this in your code, because nobody will understand it. But it's interesting.
Yeah. Also, please don't put this in a job interview. Yeah. Don't make this one of these things. Oh, one of those questions that you mean an interview question? Yeah, exactly. Like, what form of sort does a list use? What does a list plus a tuple mean? What does list plus equals? No, this belongs in a Watt talk, not in a job interview.
Yeah, definitely. I think this is interesting, because Python is mostly devoid of these like weird oddities, right? It's mostly a pretty polished, simple, clean language. But yeah, this is weird. Gary Bernhardt could incorporate this in a talk, I'm pretty sure. Nice. All right. Speaking of jobs and growth and things that you might actually want to pay attention to or decide on, Max, what's this next one you got?
So the article is about an index on the popularity of programming languages. And this organization, TIOB does one. I know that there could be, I imagine that there's some controversy around how to rank them. I'm just going to assume that they're doing a pretty good job. There's a few stories here, a few headlines. The first is that like Python hit an all time high. It's now language is at ranking number three on the chart. So it's just beats out, it beats out C++, I believe for the first time, only Java and C are more popular than Python. And the other story is that the statistical language R dipped to number 21. And so the speculation is that Python has sort of taken over as the preferred statistical language to R. And then personally, I got into Python because I kind of came at data science and ML from a software development perspective. I took CS, I learned C and Java. And so going over to Python was came a lot more naturally to me than going over to R.
If you were say a statistician, maybe, maybe coming from R makes more sense, right? Or from MATLAB or something like that, right? Yes, yes. And I have worked with and met people who do that. And they have probably extra skills that I don't have, like they could do certain things a lot faster. So again, I'm not like anti R, but I'm just, I think from the programmer's perspective, Python is a lot nicer for some people, people like me.
So personally, I'm going to stick with Python, because there's so many statistical libraries that even I have yet to learn, and it's served me very well thus far. And then the third thing I wanted to point out the language that I use most in the recent years in is Scala. And that's surprisingly down to 31, which I looking at companies around here in New York at like startups, there's a lot that use Scala. So I was surprised it was that low. But Well, I feel like Scala and those functional languages, they tend to be very popular in the financial space, right?
Maybe, yeah. You definitely are in the center of that and being in New York, right? Yes, but a lot of, you know, Foursquare, where I worked for a long time was Scala based, Twitter. So there's a lot of companies like that, social media, consumer app companies that use it. So why do you think this is? Why do you think Python is? I mean, you talked about why you were more comfortable with it, but it's a general trend. Even people who are scientific are taking up Python at much larger numbers. Why do you think that is?
When companies actually want to apply machine learning or statistical methods to their data, they already have a lot of engineers and developers. And if you already have people who are fluent in Python, then why not use Python? Python has a lot of great libraries, NumPy is really great. And then a lot of the more sophisticated machine learning libraries are really great. So there's too many benefits, I think.
Yeah, I definitely agree with that. I think another one has to do with the fact that Python is a full spectrum language. Like, I can come with a very partial understanding of what Python is and what it does. And I can still be productive. If I know how to pip install a thing, write code, I don't even know what a function or a class is. I can do amazing. I could spin up a machine learning model and do something with it, right?
Yeah. But at the same time, that piece can grow and grow into a fully distributed application or something like that, right? There's not many languages that start that simple. And yet, most of the simple languages, they top out. And you say, well, now you go do C++. I hope you like templates. Good luck.
Yeah. I'm still stumped as to why Java is at the top. That is a good question. You know what I think? Probably a lot of legacy. Yeah, I think that that's true. And I think the legacy is in two places. I think if you care about open source, but you want a compiled modern language, Java is a pretty good choice. Like, you know, C#, C++, those are also good. But C++ doesn't have garbage collection and C# is tied to Microsoft, right?
So, I can see that that's one of the things. I also think the academics, Stanford and Carnegie Mellon and all that stuff, very much for many, for a long time, taught Java, Java, Java as the way to do things. And so, the folks at the high end of decision making probably had that experience as well. So, I don't know. That's my theory.
Okay. I'm not sure if there's this article or another one related to this that predict, I don't know if you already covered this, that predicted that Python will be at the top within like four years. Yeah, that it's going to surpass Java and C. Yeah. I mean, just look at the growth curves, right? Some are either flat or downward and Python is like highly up. So, pretty awesome. Very cool to be working in the Python space. It's exciting.
Like every day is super exciting. If, like we said, Java is very popular because of, you know, there's a lot of legacy code written in Java, but new projects, I don't know if there's another score for new projects, but I assume that Python is way higher. Yeah, that's probably, it probably is. I don't know where to find that either, but it'd be cool to have that data.
Yeah. Speaking of exciting, Max, what OS do you use? I'm on macOS. Yeah. So, so am I. So is Brian. Even though the majority of Python developers are on Windows, we happen to all be there. So, we can all appreciate this next one that's coming here. So, I recently watched the WWDC and somehow I didn't notice this or they didn't announce it.
It was just in the fine print, but Dan Bader from RealPython sent this over and there was an announcement, at least in the release notes for the new macOS. What is it? Catalina? Something like that. Whatever their name is it for the new macOS, the one that replaces Mojave. It is deprecating Python too. So finally, you know, finally we have like a super old version of Python running in macOS.
And if you type Python, you get Python too. It doesn't ship with Python 3. Ideally, I would love to see Python 3 shipping. I kind of like what the Windows store and Microsoft is doing as an app that is auto updating and stuff like a really nice way to get it there. Instead, they're just saying, we've decided that Python and all the scripting runtimes, so Perl and Ruby and Python, those are not our problem anymore. We're washing our hands of them. That's the announcement.
We are no longer recommending them. It is deprecated. We have to leave Python 2 there because stuff is depending upon it, but we do not recommend you use it at all, period. Find another way to get it there. Yeah, that is interesting. Yeah. So on one hand, like that's good news. They're like, please don't, I know it has Python on it, but this is a broken old one. Don't use that. I feel like there's maybe some better way they could approach that. I don't know if there'll be some kind of warning when you run it.
So that's also, so like, if that's also not there, then how do you brew? And like, so there's a bunch of interesting changes here, but I do think it's interesting that Apple and the macOS team are addressing this Python story. What do you guys think? Yeah. Well, when did Python 3 come out? If you could remind me, was it?
2008. Wow. But in realistically, I feel like 3.4 is where it really started to get traction. And that's more like 2012-ish, something like that. Okay. Yeah. I'm just, now I'm thinking, oh God, I have some scripts in my GitHub that I may need, may want to, that I built like five years ago. I may want to upgrade.
I think it's a good thing, actually. I think it will be a good thing because people that just open up a terminal on Mac and type Python, they get 2.7 and that's not the right answer. So. Yeah. Yeah. And I've seen this where you always have two versions of Python on your machine. And there's always, did this the other day where you run the old version of Python and it doesn't work. And you're like, oh, I called the wrong command and it would be nice to kind of put an end to that. So it doesn't waste people's time.
Yeah, absolutely. I'm tempted to put an alias to alias Python to Python 3 inside my startup script. Yeah. That's a good idea. Interesting. Well, I would like a better outcome, but at least there's some kind of like Apple acknowledges how out of date the stuff they're shipping is. So that's pretty cool. Now, before we get onto the next one, I want to talk really quickly about DigitalOcean. They've been a huge supporter of the show and we really appreciate that. Our infrastructure runs on DigitalOcean, which is pretty cool. So we can definitely recommend it from someone who's using it. Right.
And I want to highlight their hosted database as a service right now. So if you want to use Postgres SQL, which is probably the most popular relational database these days in the Python space, you know, you look at the Stack Overflow survey and stuff like that. It's got a lot of love and people love it a lot. So they have hosted as a service. Just go there and say, I want Postgres. Turn it on. Here's the connection string. They'll do all the backups and scaling and failover and all that stuff for you. So super great. Check them out at pythonbytes.fm/DigitalOcean. And for new users, you get a $50 credit to get started, which is also cool. Brian, would you say that dictionaries are important in Python?
Yeah, definitely. The whole thing's built on dictionaries. It's turtles all the way down. Exactly. It's turtles all the way down. It's dictionaries all the way down. So what's up with this one? This is just a small article from L. Swigert, Pythonic ways to use dictionaries. And I thought it was a good reminder because dictionaries really are not, I mean, they're not hard. They're not hard to use. People coming from, I guess I came from Perl before Python. So I was used to, I guess, what were they called in Perl? Hashes.
Yeah. Hashes and hash maps and all these things, right? Yeah. Dictionaries are a lookup thing. And using them like that is not that hard. But there's some gotchas in there that some people try to do weird things to work around them. So this is just a highlight article that a few ways to use them smartly. One of them is to use the get function for, if you try to retrieve an element out of a dictionary and the element isn't there, that the key isn't isn't there, you get a, I don't know, a lookup error or something.
A key error. A key error. But if you know it might not be there, so you can check beforehand or you can just use the get with a default. And the default value will return something other than if it's not there, it returns something else. That's the primary way that I get stuff out of dictionaries these days. I used to do square brackets, but I don't do that anymore. It's always about .get.
Yeah, me too. I do that as well, especially since almost every dictionary is either, you know, something like a histogram or a sum of something where, you know, the default's always going to be zero. And I don't want to distinguish between that and it not being there. The key for me is almost always there's other kinds of validation you have to do anyway.
So I'm thinking of the web, right? Like I want to get something posted on a form. I want to get the, say the email they've submitted. I want to know that it's there, but I also got to make sure that it's not empty or it's not none. Like if I'm already doing the if test, I don't want to do the conditional test and a try except you might as well just do one.
Yeah, that's perfect. Yeah, it makes a lot of sense. So like their example, his example and the thing is perfect is if you're storing a counter or something like a number of hours and you just want to increment, if you're trying to increment something, you want to set it to zero to begin with, but you only want it to set it to zero if it isn't there already.
It uses a width block. So it uses the width block as the switch block, basically. And then you just have cases in there. Anyway, it's kind of funky, but I'll put a link into it. But I do think it's interesting that dictionaries can be used for switch statements. Yeah. I think it's good to comment because not everybody's going to understand what's going on.
So make a comment about what your code's doing. Yeah. Yeah, for sure. All right, Max, what's this next one you got? All right. Short article I found. Things you're probably not using in Python 3 but should. This is from datawhatnow.com. So this is particularly relevant for me because I used Python legacy at four square for many years.
Now I'm coming back to it, taking another look at V3 over the last couple of years. So just a couple of things it has that stood out for me. One that looks very useful is f-strings, where you can put the variable name in braces in a string and just have it replaced. So long as you remember to put the character F before the string.
That's very important. So I've seen things like this in other languages. Scala has it. PHP, most front-end scripts have things like this. It makes the code very readable. Except I know myself. I know I'm going to screw up by leaving out that straight F in front of the string. So I almost feel like it should be automatic.
So it's just we're just going to keep rolling. You know what I mean? Like it's one of these things that is really nice. But you got to be a little cognizant up front, right? Yeah. If you're correct, it looks very good. And actually, we did some things at Foursquare. And this was for Scala code where we had like GitHub hooks that looked to see, hey, this is a formatted string.
Oh, interesting. That's sort of a workaround. Yeah, it was helpful. But yeah, it'd be nice to have the language. But just having the formatted strings is always helpful. Another thing I didn't know that Python 3 had is enumerations. You know, again, with the functional programming, I've been using enums for years.
They tend to make code way more readable, in my opinion. And so that was good to know. I'll keep that in mind when developing in Python 3. Nice. I love the discoverability that enums give you, right? You can say enumy thing dot. And then there's a list of things. You don't have to go research like, well, what values can I pass for level?
Is it one, two, three? Is it a zero to one? And it's like low, medium, high. Pick one. Yeah, yeah. And they're reusable. And oftentimes, you know, I could look at an enum and figure out, oh, I know what this, you know, I know what this code is doing without having to really read tons of comments or really work through.
No, if I see your enums, I kind of know what you're doing. Yeah, I feel like that's underutilized in general. Brian, what do you think? Definitely underutilized by me. I mean, it's one of those things that I've, it's on my list to try to use more. I don't use magic numbers too much, but I mean, we do have cases where it's, I just use a string, but it really ought to be an enum instead of a set of possible strings.
Yeah, for sure. Cool. Well, I think, you know, one of the themes of the show today is that modern Python is definitely making its way past legacy Python, right? Python 3 is coming on strong. And if you look at what the real challenge, I think there's a lot of things that made this move a little bit difficult.
But if you look at the heart of it, really the heart of what made the Python 2 to 3 migration challenging were strings, right? So the fact that strings used to be the same as bytes and you could either treat it as like a byte list or actually an encoded string and you had to kind of know, that was a problem.
But it was also an opportunity to have simpler code like requests, for example, could talk to the network differently than it has to now because it doesn't have to like do the encoding and all these changes, right? But it's important that we have emojis and support other languages that have umlauts and things like that.
So having a Unicode is really important. But I do feel like if you look at all the stuff, like people say, oh, Python 3, I can't move to that. That's so, it sounds like a, you know, like a huge problem. It's going to be so hard. But then when you look at the code, like how hard is it to write Python 2 code that's compatible with 3?
It's like not very different. But one of the big differences is strings. So what's interesting is that I want to cover, this comes from James Small. He sent over an episode of the CppCast, which is a C++ podcast. I think one of the bigger ones. I don't really know all of them. But I definitely know of that one.
I feel like if that's really the essence of what is holding back people switching from 2 to 3, I don't know. It just doesn't seem like that's that big of a deal. I know if you're writing requests or you're writing stuff that literally is a web server and you're talking on the network, okay, that's kind of a bigger deal.
But in general, it doesn't seem like it's that big of a deal. Yeah, the big deal was that the Python, I think, from my perspective, that the Python strings used in 2 matched the idea of a character array in C. And so all of the DLLs that some packages interacted with could just interact with it directly.
And if you were interacting with bytes, you were already doing something funky anyway. But now you have to split that up. And sometimes, you know what it's like working with C. Sometimes you're using a character array, but it's really not characters. It's really bytes. And so separating that out of when they were really strings and when they were just byte arrays, yeah, it's messy.
Yeah, it's definitely messy. So I haven't used Python for the purpose of byte arrays before. Most of my Python codes have been scripts. I haven't used it for web services. So is this something that's very common? And I have used, you know, Cython. So is that does that make use of strings as byte arrays?
Or am I overthinking this? I think in Cython, they have their own types for these types of things. I'm not 100% sure, but I think so. Like you could have pointers in Cython and stuff like that. In terms of so you know, basically, now we have strings and we have lists, right? But lists are not at all like byte arrays, right?
I can't pass that off to C as a contiguous block of memory of like similar data, right? But you can actually create arrays that are typed. I can say I have like arrays that hold bytes or arrays that hold integers. And there's some... You could do that in Python. I forget exactly what module you got to use to do it.
But there's some way to say like I have this homogeneous thing that I'm creating. Like it's something I've extremely rarely have done. So I forgot the steps of it. But yeah, there's some mechanism in there to do that. Yeah. And you can prefix a string with the B character in it. And then you can put byte codes inside of it for constants.
But I'm just wondering like, you know, could the PSF... It just got me thinking about the whole transition from two to three. Like could the PSF been more proactive and strategical? Like could the PSF said, hey, it's super important we make this change. It looks like these seven libraries are the foundation of all the other libraries that are all stuck on Python 2 because none of these are changing.
Could we just raise $200,000, hire some people and just in three months upgrade all these libraries and just be done with it? Like how would the world look different if like there was a quick and focused response to like this challenge? Right. Because I feel like the actual problem that most people hit was the library I care about doesn't support Python 2.
Yeah, but it's on top of that, one of the hiccups wasn't just because of this. It was also because the way that Python libraries interact with DLLs, that changed completely also from 2 to 3. Yeah. Okay. Interesting. Yeah, the C extensions and stuff like that, huh? Yeah. So the library that I needed was...
That was just some complicated beast that a university in Germany finally had some people that converted it to Python 3. So... Okay. Well done. Well done, academia. Well done. Nice. All right. Well, that's our main topics today. I want to just throw out, see if you all have a couple of extras, real quick things to touch on.
And so he said, all right, well, let me see if I can create something that's kind of funny and amusing based on what we've been talking about. So he created a package called Mystery. Okay. This sounds like a Russian roulette type situation. Yes, it does to me too. So if you pip install Mystery, it will then go and randomly pick something else out of PyPI, out of the package index, and then install that.
Or it could be something that totally changes the language or something that takes 10 hours to install. You never know. Yeah. Maybe do it in a virtual machine that you can erase later. Yeah. Also, I just want to give a shout out to the book that I released, the Effective PyCharm book, and point out that if people are interested in the PyCharm course and or the PyCharm book, if you go to EffectivePyCharm.com, Matt Harrison and I are running a bundle deal so you can get the course and the book discounted together.
Yeah. Very cool. Well, it's a weekly tech podcast, and so I cover both the theoretical issues in probability theory, philosophy, and machine learning, but then I kind of apply it in a practical way to things like current events and product development and interviews. And I do some like opinion piece type stuff as I did this week.
So, for example, a few weeks ago, my co-host asked a question, how do you estimate the probability of an event that has never occurred? And so, you know, we dove into that question. And I started with, well, every event has never occurred, but what are you really asking me here? And some of the news that we've dived into recently is Apple's decision to break up iTunes, what that means in terms of product development, you know, how the internet is shaping up in places like Cuba.
I find that stuff. I find that stuff really interesting. And the controversy around YouTube's recommendation algorithm. I also talk a lot about data science careers. And I interview people like Hillary Mason here in New York and Mark Senadella, who's the CEO of the ladders. He critiqued my resume on my podcast.
And there's some pretty exciting stuff there. So, I'm excited to try it out. But at the bottom, it says, and now for something completely different. And then it has like a skit from, it looks like a little snippet of an interview from some of the Monty Python people. Yeah. And just, I suspect most people know, but for those of you who don't, Python has nothing to do with a snake in its naming.
Yeah. Rabbit. Killer rabbit. Oh. Get the holy hand grenade out there. All right. So, now for something completely different. Yeah. I thought we could read this skit out. So, are you guys ready? Let's do it. I'll try to do it with feeling, but I'm not very good with accents. So, we'll see what happens.
Yep. You bet. Brian, thanks as always. See you later. Yep. Thank you. Bye. Thank you for listening to Python Bytes. Follow the show on Twitter via at Python Bytes. That's Python Bytes as in B-Y-T-E-S. And get the full show notes at pythonbytes.fm. If you have a news item you want featured, just visit pythonbytes.fm and send it our way.
